You are on page 1of 3

AST231_L4_LeastSquares

Mohaimen Mansur

Estimation (Least squares estimators)

## Least square estimates (LSE)

# Using analytic formula

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

F2 <- function(z){# here z is a matrix/dataframe


x <- z[,1]# x takes the first column of z
y <- z[,2]# y takes the first column of z
return(x+y)
}
m <- data.frame(x=1:3, y=4:6)
m

## x y
## 1 1 4
## 2 2 5
## 3 3 6

F2(z=m)

## [1] 5 7 9

# Example of optim function


fr <- function(x) { ## Rosenbrock Banana function
x1 <- x[1]
x2 <- x[2]
100 * (x2 - x1 * x1)ˆ2 + (1 - x1)ˆ2
}
optim(par=c(-1.2,1), fn=fr)

## $par
## [1] 1.000260 1.000506
##
## $value
## [1] 8.825241e-08
##

2
## $counts
## function gradient
## 195 NA
##
## $convergence
## [1] 0
##
## $message
## NULL

# LSE using numeric optimization

Erssq <- function(par,d){# here par is a 2-element vector and d is a dataframe


a <- par[1]# a takes the first element of par vector
b <- par[2]# b takes the second element of par vector
x <- d[,1]# x takes the first column of d dataframe
y <- d[,2]# y takes the second column of d dataframe
return(sum((y-a-b*x)ˆ2)) # compute error sum of squares
}
p <- data.frame(x=x,y=y)# create a data frame of x, y variables to match d
res <- optim(par=c(1,2),fn = Erssq, d=p, method="BFGS")
res

## $par
## [1] 1.953334 2.975757
##
## $value
## [1] 8.887515
##
## $counts
## function gradient
## 32 9
##
## $convergence
## [1] 0
##
## $message
## NULL

You might also like