A regression workflow starts by estimating how much the response changes with one predictor.

Program

Play the script to adjust the last score and watch the fitted slope change.

bonus
regression_slope.R
Replay: real traced execution (multi-file project)
bonus <- 0
hours <- c(1, 2, 3, 4)
score <- c(52, 61, 70, 79) + c(0, 0, 0, bonus)
fit <- lm(score ~ hours)
slope <- round(coef(fit)[["hours"]], 1)
label <- paste("slope", slope, sep = ":")
cat(label, "\n", sep = "")
bonus <- 5
hours <- c(1, 2, 3, 4)
score <- c(52, 61, 70, 79) + c(0, 0, 0, bonus)
fit <- lm(score ~ hours)
slope <- round(coef(fit)[["hours"]], 1)
label <- paste("slope", slope, sep = ":")
cat(label, "\n", sep = "")
bonus <- 10
hours <- c(1, 2, 3, 4)
score <- c(52, 61, 70, 79) + c(0, 0, 0, bonus)
fit <- lm(score ~ hours)
slope <- round(coef(fit)[["hours"]], 1)
label <- paste("slope", slope, sep = ":")
cat(label, "\n", sep = "")
  1. bonus ← 0

    1bonus <- 02hours <- c(1, 2, 3, 4)
    values this step0bonus
  2. hours ← 1, 2, 3, 4

    1bonus <- 02hours <- c(1, 2, 3, 4)3score <- c(52, 61, 70, 79) + c(0, 0, 0, bonus)
    values this step1, 2, 3, 4hours
  3. score ← 52, 61, 70, 79

    2hours <- c(1, 2, 3, 4)3score <- c(52, 61, 70, 79) + c(0, 0, 0, bonus)4fit <- lm(score ~ hours)
    values this step52, 61, 70, 79score0bonus
  4. fit ← lm(score ~ hours)

    3score <- c(52, 61, 70, 79) + c(0, 0, 0, bonus)4fit <- lm(score ~ hours)5slope <- round(coef(fit)[["hours"]], 1)
    values this steplm(score ~ hours)fit52, 61, 70, 79score1, 2, 3, 4hours
  5. slope ← 9

    4fit <- lm(score ~ hours)5slope <- round(coef(fit)[["hours"]], 1)6label <- paste("slope", slope, sep = ":")
    values this step9slopelinear trendfit
  6. label ← slope:9

    5slope <- round(coef(fit)[["hours"]], 1)6label <- paste("slope", slope, sep = ":")7cat(label, "\n", sep = "")
    values this stepslope:9label9slope
  7. cat(label, " ", sep = "")

    6label <- paste("slope", slope, sep = ":")7cat(label, "\n", sep = "")
    outputslope:9
    values this stepslope:9label
  1. bonus ← 5

    1bonus <- 52hours <- c(1, 2, 3, 4)
    values this step5bonus
  2. hours ← 1, 2, 3, 4

    1bonus <- 52hours <- c(1, 2, 3, 4)3score <- c(52, 61, 70, 79) + c(0, 0, 0, bonus)
    values this step1, 2, 3, 4hours
  3. score ← 52, 61, 70, 84

    2hours <- c(1, 2, 3, 4)3score <- c(52, 61, 70, 79) + c(0, 0, 0, bonus)4fit <- lm(score ~ hours)
    values this step52, 61, 70, 84score5bonus
  4. fit ← lm(score ~ hours)

    3score <- c(52, 61, 70, 79) + c(0, 0, 0, bonus)4fit <- lm(score ~ hours)5slope <- round(coef(fit)[["hours"]], 1)
    values this steplm(score ~ hours)fit52, 61, 70, 84score1, 2, 3, 4hours
  5. slope ← 10.5

    4fit <- lm(score ~ hours)5slope <- round(coef(fit)[["hours"]], 1)6label <- paste("slope", slope, sep = ":")
    values this step10.5slopelinear trendfit
  6. label ← slope:10.5

    5slope <- round(coef(fit)[["hours"]], 1)6label <- paste("slope", slope, sep = ":")7cat(label, "\n", sep = "")
    values this stepslope:10.5label10.5slope
  7. cat(label, " ", sep = "")

    6label <- paste("slope", slope, sep = ":")7cat(label, "\n", sep = "")
    outputslope:10.5
    values this stepslope:10.5label
  1. bonus ← 10

    1bonus <- 102hours <- c(1, 2, 3, 4)
    values this step10bonus
  2. hours ← 1, 2, 3, 4

    1bonus <- 102hours <- c(1, 2, 3, 4)3score <- c(52, 61, 70, 79) + c(0, 0, 0, bonus)
    values this step1, 2, 3, 4hours
  3. score ← 52, 61, 70, 89

    2hours <- c(1, 2, 3, 4)3score <- c(52, 61, 70, 79) + c(0, 0, 0, bonus)4fit <- lm(score ~ hours)
    values this step52, 61, 70, 89score10bonus
  4. fit ← lm(score ~ hours)

    3score <- c(52, 61, 70, 79) + c(0, 0, 0, bonus)4fit <- lm(score ~ hours)5slope <- round(coef(fit)[["hours"]], 1)
    values this steplm(score ~ hours)fit52, 61, 70, 89score1, 2, 3, 4hours
  5. slope ← 12

    4fit <- lm(score ~ hours)5slope <- round(coef(fit)[["hours"]], 1)6label <- paste("slope", slope, sep = ":")
    values this step12slopelinear trendfit
  6. label ← slope:12

    5slope <- round(coef(fit)[["hours"]], 1)6label <- paste("slope", slope, sep = ":")7cat(label, "\n", sep = "")
    values this stepslope:12label12slope
  7. cat(label, " ", sep = "")

    6label <- paste("slope", slope, sep = ":")7cat(label, "\n", sep = "")
    outputslope:12
    values this stepslope:12label
linear model `lm(score ~ hours)` estimates an intercept and a slope.
coefficient `coef(fit)[["hours"]]` reads the predictor slope.
sensitivity Changing one observation can change the fitted trend.