## 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.
airquality=na.omit(airquality)
#make Month a factor (i.e., the Month number is a name rather than a number)
airquality$Month=as.factor(airquality$Month)
airquality\$region = rep(c("north","south"),60)[1:111]
#Only use 5 data points for the homework so you can show the matrices easily
homeworkdat = airquality[1:5,]
1. 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

fit = lm(Ozone ~ Wind + Temp, data = homeworkdat)
2. For the above model, write out the following R code.

1. Create the $$\mathbf{y}$$ and $$\mathbf{Z}$$ matrices in R.
2. Solve for $$\mathbf{x}$$ (the parameters). Show that they match what you get from the first lm() call.
3. Add -1 to your lm() call in question 1:

fit = lm(Ozone ~ -1 + Wind + Temp, data = homeworkdat)
1. What changes in your model?
2. Write out the in Form 1 as an equation. Show the new $$\mathbf{Z}$$ and $$\mathbf{x}$$ matrices.
3. Solve for the parameters ($$\mathbf{x}$$) and show they match what is returned by lm().
4. For the model for question 1,

1. Write in Form 2 as an equation.
2. Adapt the code from subsection 2.9.0.1 and construct new Z, y and x in R code.
3. Solve for the parameters using the code from subsection 2.9.0.1.
5. A model of the ozone data with only a region (north/south) effect can be written:

fit = lm(Ozone ~ -1 + region, data = homeworkdat)
1. Write this model in Form 1 as an equation.
2. Solve for the parameter values and show that they match what you get from the lm() call.
6. Using the same model from question 5,

1. Write the model in Form 2 as an equation.
2. Write out the Z and x in R code.
3. 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.
7. Write the model below in Form 2 as an equation. Show the $$\mathbf{Z}$$, $$\mathbf{y}$$ and $$\mathbf{x}$$ matrices.

fit = lm(Ozone ~ Temp:region, data = homeworkdat)
8. Using the airquality dataset with 111 data points

1. Write the model below in Form 2.
fit = lm(Ozone ~ -1 + Temp:region + Month, data = airquality)
1. Solve for the parameters by adapting code from subsection 2.9.0.1.