Linear models

Example 1: Linear regression with one explanatory variable

Definitions

# Amount of fertilizer - increases by 100 each field
x=seq(100,700,by=100) 
# Crop yield
y=c(40,45,50,65,70,70,80)

Simple model

We can analyse these with a linear model:$$E(y)=\beta_0 + \beta_1x$$
In this model, we are assuming that:

More complex model

Let's add the assumption that observation yi has independent and identically distributed (iid) Gaussian noise [1] (ϵi) in addition to it's mean value, representing Aleatory uncertainty:

yi=E[yi]+ϵiyi=β0+β1x+ϵi

where ϵi∼N(0,σ2)

Implementation in R

model=lm(y~x)
summary(model)

!lecture_notes1_week1, p.20
The sections of the output from R are:

Call

A reminder of the call that you used to generate the object - i.e. the code we used to generate the model

Residuals

This gives a summary of the distribution of residuals, i.e. the difference between our model line and each point. Since this only has 7 points it gives one for each, but for larger datasets it will print the quantiles. Residuals formula:

ri=yi−b^0−b^1x1=y−y^i

I think the hats here represent the model values, so this is another way of saying (real value - predicted value).

Coefficients

This summarizes the estimated parameters of the model in a table for each parameter. In our model there are only two, (Intercept) (β0) and x (slope for x, β1).

Signif. codes

A legend explaining the * and . symbols associated with the p-values in the coefficients table.

Residual standard error

The estimate for the standard deviation of the residuals. This is denoted with σ^ in maths.

σ^=∑i=1nri2n−p=∑i=1n(yi−b^0−b^1x1)2n−p

Multiple R-squared

Estimate of the proportion of the variance in the data explained by the regression. It's defined as:

r2=1−∑i=1nri2∑i=1n(yi−y¯)2

y¯ is the mean of all yi (sample mean).
I assume that the ri in the numerator is the residual at value i. Don't think we need to really understand this.

Note that this is a biased Estimator, it increases every time a new parameter is added to the data.

Adjusted R-squared

ANother estimator for the proportion of variance in the data explained by the regression. The problem with R-squared is that it always increases when you add a new predictor to the model, no matter if the new predictor is useful or not.

radj2=1−∑i=1nri2/(n−p)∑i=1n(yi−y¯)2/(n−1)

Where n is the number of observations and p is the number of parameters.

This is an unbiased estimator.

F-statistic

The final line gives the F-statistic and its p-value.

So this statistic tells us whether the model as a whole is useful or not.
It's based on the residual sum of squares or RSS:

The sum of all the residuals from each data point squared.

RSS=∑i=1nri2

This is used for calculating the F-statistic in a linear model.

F=RSS0−RSS1/(p−1)RSS1/(n−p)

The F-statistic follows a given distribution so we can get a p-value of how likely our F statistic is to be in the distribution.

Confidence Intervals

We can look at the Confidence intervals with the confint() function in R - by default the 95% ones.

!lecture_notes1_week1, p.21

Conclusion

Looks like our model is pretty good and amount of fertilizer is a significant explanatory variable.

Visualizing the regression

Pretty straightforward, you can plot the regression line with abline().
!lecture_notes1_week1, p.22

Confidence intervals and predictions

We can also use the predict() function to get confidence intervals, then visualize these upper and lower confidence intervals.

y_ci=predict(
	model,
	se.fit=TRUE,
	interval="confidence",
	level=0.95
)
{plot(x,y,xlab="Fertilizer ",ylab="Yield") 
abline(model,col="red")
points(x,y_ci$fit[,"fit"],col="red") 
lines(x,y_ci$fit[,"lwr"], col="green") 
lines(x,y_ci$fit[,"upr"], col="green")}

!lecture_notes1_week1, p.23
Then you can also add uncertainty estimate for new data that could be added (prediction) to the model with this line:

y_pi = predict(
	model, 
	interval="prediction", 
	level=0.95
)
matlines(x, y_pi[,c("lwr","upr")], col="black",lty=2)

!lecture_notes1_week1, p.23
Prediction intervals are wider than confidence intervals since uncertainty is higher with a single value, while confidence intervals work around the mean.

Residuals

You can also plot the residuals as histogram or scatterplot:

resid=residuals(model) 
{par(mfrow=c(1,2)) 
hist(resid,10) 
plot(fitted.values(model),resid) 
abline(h=0)}

A good model will have a random distribution of residuals centered around 0 with no discernible pattern. It seems it is debated whether them being normally distributed is important (?)

Example 2: Linear regression with multiple explanatory variables

This example looks at photosynthesis in small ponds treated with different amounts of nutients and amounts of plankton.

The lecture nots are pretty clear for this example. You start by plotting scatterplots of how all variables effect each other.

Next, looking at the summary() function can let you filter out non-significant explanatory variables. Removing them makes the model better (increases adj r squared)

An anova() analysis (ANOVA (Maths)) comparing the two models shows that keeping the extra variables didn't add anything else, but I'm not sure why exactly (we will learn about this next week I think).

Then looking at some summary plots, we see some problems (explained in the lecture notes). They address these by using a 2nd order polynomial to fit the sum of the two plankton variables (instead of first order).

Categorical covariates in a linear model

This was discussed in week 2:
Categorical covariates


  1. See Central Limit Theorem for an explanation of this and why we assume Gaussian distributions. ↩︎