Chapter 8 Linear model
8.1 Scatter plot
- To produce a scatter plot, use
plot(<x variable>, <y variable>)
.
plot(mtcars$mpg,mtcars$wt)
plot(mtcars$mpg,mtcars$wt, xlab="mpg", ylab="weight", main="Scatter plot of weight vs mpg")
8.2 Correlation coefficient
- To calculate the correlation coefficient, use
cor(<x variable>, <y variable>)
.
8.3 Regression line
- To produce the regression line, use
lm(<y variable>~<x variable>)
.
- Now you can add this line to the scatter plot.
8.4 Residual plot
The residual plot helps to detect any pattern not captured by the linear model.
- If it is a random scatter, then the linear model seems appropriate.
- If it shows a pattern (eg quadratic), then another model should be considered.
To produce a residual plot: