2.10 Problems
For the homework questions, we will using part of the airquality
data set in R. Load that as
data(airquality, package="datasets")
#remove any rows with NAs omitted.
=na.omit(airquality)
airquality#make Month a factor (i.e., the Month number is a name rather than a number)
$Month=as.factor(airquality$Month)
airquality#add a region factor
$region = rep(c("north","south"),60)[1:111]
airquality#Only use 5 data points for the homework so you can show the matrices easily
= airquality[1:5,] homeworkdat
Using Form 1 \(\mathbf{y}=\mathbf{Z}\mathbf{x}+\mathbf{e}\), write out the model, showing the \(\mathbf{Z}\) and \(\mathbf{x}\) matrices, being fit by this command
= lm(Ozone ~ Wind + Temp, data = homeworkdat) fit
For the above model, write out the following R code.
- Create the \(\mathbf{y}\) and \(\mathbf{Z}\) matrices in R.
- Solve for \(\mathbf{x}\) (the parameters). Show that they match what you get from the first
lm()
call.
Add -1 to your
lm()
call in question 1:= lm(Ozone ~ -1 + Wind + Temp, data = homeworkdat) fit
- What changes in your model?
- Write out the in Form 1 as an equation. Show the new \(\mathbf{Z}\) and \(\mathbf{x}\) matrices.
- Solve for the parameters (\(\mathbf{x}\)) and show they match what is returned by
lm()
.
For the model for question 1,
A model of the ozone data with only a region (north/south) effect can be written:
= lm(Ozone ~ -1 + region, data = homeworkdat) fit
- Write this model in Form 1 as an equation.
- Solve for the parameter values and show that they match what you get from the
lm()
call.
Using the same model from question 5,
- Write the model in Form 2 as an equation.
- Write out the
Z
andx
in R code. - Solve for the parameter values and show that they match what you get from the
lm()
call. To do this, you adapt the code from subsection 2.9.0.1.
Write the model below in Form 2 as an equation. Show the \(\mathbf{Z}\), \(\mathbf{y}\) and \(\mathbf{x}\) matrices.
= lm(Ozone ~ Temp:region, data = homeworkdat) fit
Using the airquality dataset with 111 data points
- Write the model below in Form 2.
= lm(Ozone ~ -1 + Temp:region + Month, data = airquality) fit
- Solve for the parameters by adapting code from subsection 2.9.0.1.