Regression Workflows
Regression Slope
Fit a Trend
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.
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 = "")
bonus ← 0
1bonus <- 02hours <- c(1, 2, 3, 4)values this step0bonushours ← 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, 4hoursscore ← 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, 79score0bonusfit ← 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, 4hoursslope ← 9
4fit <- lm(score ~ hours)5slope <- round(coef(fit)[["hours"]], 1)6label <- paste("slope", slope, sep = ":")values this step9slopelinear trendfitlabel ← slope:9
5slope <- round(coef(fit)[["hours"]], 1)6label <- paste("slope", slope, sep = ":")7cat(label, "\n", sep = "")values this stepslope:9label9slopecat(label, " ", sep = "")
6label <- paste("slope", slope, sep = ":")7cat(label, "\n", sep = "")outputslope:9values this stepslope:9label
bonus ← 5
1bonus <- 52hours <- c(1, 2, 3, 4)values this step5bonushours ← 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, 4hoursscore ← 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, 84score5bonusfit ← 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, 4hoursslope ← 10.5
4fit <- lm(score ~ hours)5slope <- round(coef(fit)[["hours"]], 1)6label <- paste("slope", slope, sep = ":")values this step10.5slopelinear trendfitlabel ← slope:10.5
5slope <- round(coef(fit)[["hours"]], 1)6label <- paste("slope", slope, sep = ":")7cat(label, "\n", sep = "")values this stepslope:10.5label10.5slopecat(label, " ", sep = "")
6label <- paste("slope", slope, sep = ":")7cat(label, "\n", sep = "")outputslope:10.5values this stepslope:10.5label
bonus ← 10
1bonus <- 102hours <- c(1, 2, 3, 4)values this step10bonushours ← 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, 4hoursscore ← 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, 89score10bonusfit ← 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, 4hoursslope ← 12
4fit <- lm(score ~ hours)5slope <- round(coef(fit)[["hours"]], 1)6label <- paste("slope", slope, sep = ":")values this step12slopelinear trendfitlabel ← slope:12
5slope <- round(coef(fit)[["hours"]], 1)6label <- paste("slope", slope, sep = ":")7cat(label, "\n", sep = "")values this stepslope:12label12slopecat(label, " ", sep = "")
6label <- paste("slope", slope, sep = ":")7cat(label, "\n", sep = "")outputslope:12values 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.