Professional Documents
Culture Documents
Mohaimen Mansur
x <- 1:10
y <- c(3.9,7.1,12.6,14.5,16.7,19.2,23.1,27.2,27.8,31.1)
plot(y~x)
30
25
20
y
15
10
5
2 4 6 8 10
x-mean(x)
## [1] -4.5 -3.5 -2.5 -1.5 -0.5 0.5 1.5 2.5 3.5 4.5
ssxy=sum((x-mean(x))*(y-mean(y)))
ssx=sum((x-mean(x))ˆ2)
beta=ssxy/ssx
alpha=mean(y)-beta*mean(x)
alpha
1
## [1] 1.953333
beta
## [1] 2.975758
# Examples of functions
# https://www.w3schools.com/r/r_functions.asp#:~:text=A%20function%20is%20a%20block,return%20data%20as%2
#Examples of a function
F1 <- function(v){# here v is a 3-element vector
a <- v[1]# a takes the first element of v vector
b <- v[2]# b takes the second element of v vector
c <- v[3]# c takes the third element of v vector
return(a + bˆ2 + cˆ3)
}
F1(v=c(1,2,3))
## [1] 32
## x y
## 1 1 4
## 2 2 5
## 3 3 6
F2(z=m)
## [1] 5 7 9
## $par
## [1] 1.000260 1.000506
##
## $value
## [1] 8.825241e-08
##
2
## $counts
## function gradient
## 195 NA
##
## $convergence
## [1] 0
##
## $message
## NULL
## $par
## [1] 1.953334 2.975757
##
## $value
## [1] 8.887515
##
## $counts
## function gradient
## 32 9
##
## $convergence
## [1] 0
##
## $message
## NULL