Description
In this lab we will investigate more detail concerning the mathematical underpinning of Simple Linear
Regression.
Why is it called βSimpleβ? The reason is because we use only one independent variable (one x). When
more than one x is used we call this multiple regression.
As I will show you in class:
πππ₯π₯ = β(π₯ β π₯Μ
)
2
π
π=1
πππ₯π¦ = β(π₯π β
π
π=1
π₯Μ
)(π¦π β π¦Μ
)
π½Μ
1 =
πππ₯π¦
πππ₯π₯
π¦Μ
= π½Μ
0 + π½Μ
1π₯Μ
Tasks
All output should be made through RMD. Please upload the following files:
β’ HTML
β’ RMD
All plots should be made through RMD and knitted into suitable
formats.
You are expected to adjust the functions as needed to answer the
questions within the tasks below.
β’ Task 1
o Make a folder LAB14
o Download the file βlab14.rβ
o Place this file with the others in LAB14.
o Start Rstudio
o Open βlab14.rβ from within Rstudio.
o Go to the βsessionβ menu within Rstudio and βset working directoryβ to where the source
files are located.
o Issue the function getwd() and copy the output here.
o Create your own R file and record the R code you used to complete the lab.
β’ Task 2
o Make a function (mylsq) that will calculate estimates of the slope and intercept under
least squares regression. The function will operate on two vectors of the same length, x and
y, where x is the independent variable and y the dependent variable. It is partially made
below. Hint: Use the formulae above.
mylsq=function(x,y){
ssxx=sum((x-mean(x))^2 )
ssxy=sum() ## fill in the missing portion
b1hat=ssxy/ssxx
b0hat= ## fill in the missing portion
return(list(b0hat=b0hat,b1hat= )) #fill in the missing portion
}
o Suppose x=1:20 and set.seed(29);y=4+6*x + rnorm(20,0,5)
o Use mylsq() to calculate the least squares estimates of parameters π½0, πππ π½1.
o Plot the points and the least squares line, with a heading and appropriate x and y labels.
Also make the line have lwd=2 and be blue in colour. Hint: You can use abline()
o Check your calculations using slr=lm(y~x); summary(slr).
β’ Task 3
o Now make a function that will predict the average y value from a given xnew. The function
is mypred() will take three arguments, the x value, b0hat and b1hat.
mypred=function(x,b0,b1){
ym=b0+ ## fill in the gap
ym
}
o Use the same data in Task 2 and predict a new mean y value (π¦Μ) when xnew=15.5
o Plot this point (xnew,ym) with the previous data and least squares line. Hint: use
points(), cex=3,col=βGreenβ,pch=19
o Use the functions you have created so far and answer 10.12 page 498 in MS 6
th edition
βͺ a)
βͺ b)
βͺ c)
o Use the functions you have created so far and answer 10.80 page 553 in MS 6
th edition
βͺ a)
βͺ b)
βͺ c)
βͺ d)
β’ Task 4
o On page 501 MS proves that the least squares estimator π½Μ
1 is an unbiased estimator of π½1.
o On page 503 MS shows that an unbiased estimator of π
2
is
π
2 =
πππ
π β 2
, π€βπππ πππ
= β(π¦π β π¦Μπ
)
2
o Complete the following function that calculates π
2
mysq=function(x,y){
n=length(x)
ssxx=sum((x-mean(x))^2 )
ssxy=sum() ## fill in the missing portion
b1hat=ssxy/ssxx
b0hat= ## fill
yhat=b0hat+ ## fill
ssr=sum((y-##)^2) # fill
sq= ## fill
return(list(ssr=ssr,sq=sq))
}
o Using x and y from Task 2 estimate π
2
. How close did you get?
o Now answer MS page 506 10.25 below
βͺ a)
βͺ b)
βͺ c)
βͺ d)
################### LAB FINISHES HERE ###############################




