Linear Models (lm)

A linear model fits the best straight-line relationship between a response and one or more predictors — R's lm() function makes regression a single, readable line of code.

Learn Linear Models (lm) in our free R course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free R course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll fit a model with lm(), read its coefficients and R-squared, make predictions on new data, and build models with multiple predictors.

What You'll Learn in This Lesson

1️⃣ Fitting a Model

lm(response ~ predictor, data = df) fits the model. coef() pulls out the intercept and slope, which define the best-fit line.

2️⃣ Reading the Model with summary()

summary(model) reports the full results — coefficient estimates, their p-values, and R-squared , the share of variation the model explains.

3️⃣ Predicting and Multiple Predictors

predict(model, newdata) applies the fitted rule to new cases. Add predictors with + in the formula — mpg ~ wt + hp uses both weight and horsepower.

Your turn. Fill in the # TODO blank, run it, and compare with the expected output.

Write it from the outline, run it, and check it against the example output. Fitting, reporting R-squared, and predicting is the full regression workflow in miniature.

📋 Quick Reference — Linear Models

Practice quiz

How do you read the formula in lm(score ~ hours)?

  • hours predicted from score
  • score predicted from hours
  • score equals hours
  • hours times score

Answer: score predicted from hours. The form is response ~ predictor, so score is modelled from hours.

Which function pulls out a model's intercept and slope?

  • coef(model)
  • slope(model)
  • params(model)
  • lm(model)

Answer: coef(model). coef() returns the fitted coefficients (intercept and slope per predictor).

What does R-squared measure?

  • The number of predictors
  • The slope of the line
  • The p-value
  • The fraction of variation the model explains

Answer: The fraction of variation the model explains. R-squared, from 0 to 1, is the share of the response's variation explained.

How do you get R-squared from a fitted model?

  • coef(model)$r2
  • summary(model)$r.squared
  • rsq(model)
  • model$r.squared

Answer: summary(model)$r.squared. summary(model)$r.squared returns the R-squared value.

Which function applies a fitted model to new data?

  • forecast()
  • apply()
  • predict()
  • fit()

Answer: predict(). predict(model, newdata) computes predictions for new cases.

How do you add a second predictor in a formula?

  • mpg ~ wt + hp
  • mpg ~ wt, hp
  • mpg ~ wt & hp
  • mpg ~ wt * hp only

Answer: mpg ~ wt + hp. Add predictors with +, e.g. mpg ~ wt + hp uses both.

What must newdata in predict() contain?

  • The response column
  • Random values
  • Nothing
  • The same predictor column names as the formula

Answer: The same predictor column names as the formula. newdata must use the same predictor names the model was trained on.

If the slope for hours is 9.1, what does it mean?

  • Each extra hour adds about 9.1 to the response
  • The intercept is 9.1
  • R-squared is 9.1
  • There are 9.1 predictors

Answer: Each extra hour adds about 9.1 to the response. A slope is the change in response per one-unit increase in that predictor.

What does the intercept represent?

  • The maximum response
  • The predicted response when all predictors are zero
  • The error term
  • The number of rows

Answer: The predicted response when all predictors are zero. The intercept is the model's predicted value when predictors equal zero.

Why is predicting far outside the training range risky?

  • It is always exact
  • predict() refuses to run
  • Extrapolation may not hold beyond the data
  • It changes the coefficients

Answer: Extrapolation may not hold beyond the data. The linear relationship that held within your data may not continue beyond it.