Chapter 2 Linear regression in matrix form

This chapter shows how to write linear regression models in matrix form. The purpose is to get you comfortable writing multivariate linear models in different matrix forms before we start working with time series versions of these models. Each matrix form is an equivalent model for the data, but written in different forms. You do not need to worry which form is better or worse at this point. Simply get comfortable writing multivariate linear models in different matrix forms.

A script with all the R code in the chapter can be downloaded here. The Rmd file of this chapter can be downloaded here.

Data and packages

This chapter uses the stats, MARSS and datasets packages. Install those packages, if needed, and load:

library(stats)
library(MARSS)
library(datasets)

We will work with the stackloss dataset available in the datasets package. The dataset consists of 21 observations on the efficiency of a plant that produces nitric acid as a function of three explanatory variables: air flow, water temperature and acid concentration. We are going to use just the first 4 datapoints so that it is easier to write the matrices, but the concepts extend to as many datapoints as you have.

data(stackloss, package = "datasets")
dat = stackloss[1:4, ]  #subsetted first 4 rows
dat
  Air.Flow Water.Temp Acid.Conc. stack.loss
1       80         27         89         42
2       80         27         88         37
3       75         25         90         37
4       62         24         87         28