Skip to main content
Module 7 · Lesson 1 of 314/18 in the course~22 min
Module lessons (1/3)

Simple Linear Regression with lm()

Lesson preview5 hands-on exercises readyCourse R · Module 7 — Regression and Linear Models

Write y ~ x formulas in lm(), inspect summary(fit), R², p-value coefficients, and generate predictions with predict().

What you'll learn in this lesson

  • Specifying statistical formulas in R (risposta ~ predittore) inside the lm() function.
  • Extract and interpret intercept and slope via coef(fit) and read their significance in summary(fit).
  • Quantify the amount of variance explained by reading R² (summary(fit)$r.squared).
  • Calculate predictions on new data with predict(fit, newdata = ...) avoiding extrapolation traps.

Overview

While cor() only tells us how much two variables move together, lm() (Linear Model) estimates the equation of the straight line that minimizes the sum of the squares of the vertical errors (OLS, Ordinary Least Squares). This allows both to interpret the unitary effect (di quanto cresce y per ogni +1 in x?) and to make predictions on…

Explore how the coefficients estimated by lm() and R² react to data variability in the simulator below:

Sections covered in this lesson

  1. 1Anatomy of lm() and the result object
  2. 2Test yourself: guided exercises
  3. 3Concept check
Pro Lesson · Module 7 — Regression and Linear Models

Unlock "Simple Linear Regression with lm()" and the full Course R curriculum

Write y ~ x formulas in lm(), inspect summary(fit), R², p-value coefficients, and generate predictions with predict().

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in Course R plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.