2 Linear Regression

2.1 Simple Linear Regression

lm <- lm(y ~ x, data = data)

predicting

predict(
  lm,
  data.frame(x = (c(1, 2, 3))), # for multiple values, you can just do one also
  interval = "confidence"
)

plotting

attach(data)
plot(x, y)
abline(lm)

2.2 Multiple Linear Regression

lm <- lm(y ~ x + z, data = data)

with interaction term

lm <- lm(y ~ x * z, data = data)

with all variables plus interaction term

lm <- lm(y ~ . + z:w, data = data)

2.3 Diagnostics

par(mfrow = c(2, 2))
plot(lm)

2.4 Transformations

can log transform, square root, or polynomial

2.4.1 Polynomial Transfomration

lm <- lm(y ~ poly(x, 6))

2.5 Important Information

RSE: want to minimize, but have to know how data is measured

R2: between 0 and 1, closer to 1 is better

VIF: shows colinearity in multiple regression (vif function in car package)

Leverage: outliers for predictor