|R| Experts
1.08K subscribers
375 photos
35 videos
58 files
204 links
@R_Experts
🔴آمار علم جان بخشیدن به داده‌هاست.
🔷ارتباط با ما
@iamrezaei
لینک یوتیوب و اینستاگرام و ویرگول:
https://zil.ink/expertstv
Download Telegram
#rgl
#surface3D

install.packages("rgl")

library(rgl)
data(volcano)
z <- 2 * volcano # Exaggerate the relief
x <- 10 * (1:nrow(z)) # 10 meter spacing (S to N)
y <- 10 * (1:ncol(z)) # 10 meter spacing (E to W)
zlim <- range(z)
zlen <- zlim[2] - zlim[1] + 1
colorlut <- terrain.colors(zlen,alpha=0) # height color lookup table
col <- colorlut[ z-zlim[1]+1 ] # assign colors to heights for each point
open3d()
rgl.surface(x, y, z, color=col, alpha=0.75, back="lines")



@R_Experts
#نیوتون_رافسون:

یکی از الگوریتم های تکراری جهت ریشه یابی معادلات خطی و غیر خطی ،

تابع مورد استفاده بایستی
مشتق پذیر


با حدس اولیه ریشه شروع میشود،

فرض میکنیم ریشه واقعی
 R


حدسی:
X0


و
h

در رابطه زیر قرار میگیرد

R=x0+h

📝📝

و روند ریشه یابی در زیر میبینیم 👇👇👇


@R_Experts
به تابع زیر توجه فرمایید

f<- function(x) {
exp(2 * x) - x - 6
}


curve(f, col = 'blue', lty = 2, lwd = 2, xlim=c(-5,5), ylim=c(-5,5), ylab='f(x)')
abline(h=0)
abline(v=0)


@R_Experts
حال طبق توضیحات بالا داریم :

newton <- function(f, tol=1E-12,x0=1,N=20) {

        h <- 0.001

        i <- 1; x1 <- x0

        p <- numeric(N)

        while (i<=N) {

                df.dx <- (f(x0+h)-f(x0))/h

                x1 <- (x0 - (f(x0)/df.dx))

                p[i] <- x1

                i <- i + 1

                if (abs(x1-x0) < tol) break

                x0 <- x1

        }

        return(p[1:(i-1)])

}


f <- function(x) {

  exp(2 * x) - x - 6

}



p <- newton(f, x0=1, N=20)

 p



> p <- newton(f, x0=1, N=10)

>  p

[1] 0.9717930 0.9708719 0.9708700 0.9708700 0.9708700 0.9708700

> 

> p <- newton(f, x0=1, N=20)

>  p

[1] 0.9717930 0.9708719 0.9708700 0.9708700 0.9708700 0.9708700

> 


@R_Experts
در الگوریتم دیگری از این روش در زیر داریم :




newton.raphson <- function(f, a, b, tol = 1e-5, n = 1000) {
require(numDeriv) # Package for computing f'(x)

x0 <- a # Set start value to supplied lower bound
k <- n # Initialize for iteration results

# Check the upper and lower bounds to see if approximations result in 0
fa <- f(a)
if (fa == 0.0) {
return(a)
}

fb <- f(b)
if (fb == 0.0) {
return(b)
}

for (i in 1:n) {
dx <- genD(func = f, x = x0)$D[1] # First-order derivative f'(x0)
x1 <- x0 - (f(x0) / dx) # Calculate next value x1
k[i] <- x1 # Store x1
# Once the difference between x0 and x1 becomes sufficiently small, output the results.
if (abs(x1 - x0) < tol) {
root.approx <- tail(k, n=1)
res <- list('root approximation' = root.approx, 'iterations' = k)
return(res)
}
# If Newton-Raphson has not yet reached convergence set x1 as x0 and continue
x0 <- x1
}
print('Too many iterations in method')
}

f<- function(x) {
exp(2 * x) - x - 6
}

uniroot(f, c(.5, 1.5))





> uniroot(f, c(.5, 1.5))
$root
[1] 0.9708565

$f.root
[1] -0.000175188

$iter
[1] 6

$init.it
[1] NA

$estim.prec
[1] 6.103516e-05

>



@R_Experts
#کرنل

یکی از دستورات دیگر رسم چگالی کرنل استفاده از دستور

density


میباشد.
مثال :

d <- density(mtcars$mpg)

plot(d, main="Kernel Density of Miles Per Gallon")

polygon(d, col="red", border="blue") 


@R_Experts
#Correlations

با تابع

cor


قادر به محاسبه ی همبستگی و

با تابع

cov


قادر به محاسبه کواریانس خواهیم بود

@R_Experts
cor(x, use=, method= )

@R_Experts
> x <- mtcars[1:3]
> y <- mtcars[4:6]
> cor(x, y)
hp drat wt
mpg -0.7761684 0.6811719 -0.8676594
cyl 0.8324475 -0.6999381 0.7824958
disp 0.7909486 -0.7102139 0.8879799
>


@R_Experts
وقتی دو سری داده داریم و بر روی اون ها یکی از ضرایب همبستگی پیرسن یا اسپیرمن یا کندال میخواهیم به دست اوریم ،

از دستور

cor.test 


در بسته ی

 stats


استفاده میکنیم ،

ولی وقتی یک ساختار به مانند ماتریس یا دیتا فریم داریم

در به دست اوردن پیرسن و اسپیرمن

دستور و بسته معرفی شده در بالا میتونه به ما کمک کنه به مثال زیر توجه کنین 👇👇👇👇


@R_Experts
library("Hmisc")

x <- c(-2, -1, 0, 1, 2)

y <- c(4,   1, 0, 1, 4)

z <- c(1,   2, 3, 4, NA)

v <- c(1,   2, 3, 4, 5)

rcorr(cbind(x,y,z,v))

cor.test(x,y)

cor.test(x,y,v)


وقتی این مثال رو اجرا کنیم

متوجه میشیم که

این بسته ضرایب همبستگی و تعداد عناصری که شرکت دارن و همچنین سطح معنی داری رو نیز در اختیار ما میگذاره

@R_Experts
اماره مربوطه
@R_Experts