lm uses a formula to describe the response and predictor variables for a linear model.

Program

Play the script to choose which predictor appears on the right side of the model formula.

predictor_index
model_formula.R
Replay: real traced execution (multi-file project)
training <- data.frame(hours = c(1, 2, 3, 4), effort = c(1, 1, 2, 3), score = c(55, 60, 70, 75))
predictor_index <- 1
predictor <- c("hours", "effort")[predictor_index]
model_formula <- as.formula(paste("score ~", predictor))
fit <- lm(model_formula, data = training)
slope <- round(coef(fit)[[predictor]], 1)
cat(slope, "\n", sep = "")
training <- data.frame(hours = c(1, 2, 3, 4), effort = c(1, 1, 2, 3), score = c(55, 60, 70, 75))
predictor_index <- 2
predictor <- c("hours", "effort")[predictor_index]
model_formula <- as.formula(paste("score ~", predictor))
fit <- lm(model_formula, data = training)
slope <- round(coef(fit)[[predictor]], 1)
cat(slope, "\n", sep = "")
  1. training ← 4 rows x 3 cols

    1training <- data.frame(hours = c(1, 2, 3, 4), effort = c(1, 1, 2, 3), score = c(55, 60, 70, 75))2predictor_index <- 1
    values this step4 rows x 3 colstraining
  2. predictor_index ← 1

    1training <- data.frame(hours = c(1, 2, 3, 4), effort = c(1, 1, 2, 3), score = c(55, 60, 70, 75))2predictor_index <- 13predictor <- c("hours", "effort")[predictor_index]
    values this step1predictor_index
  3. predictor ← hours

    2predictor_index <- 13predictor <- c("hours", "effort")[predictor_index]4model_formula <- as.formula(paste("score ~", predictor))
    values this stephourspredictor1predictor_index
  4. model_formula ← score ~ hours

    3predictor <- c("hours", "effort")[predictor_index]4model_formula <- as.formula(paste("score ~", predictor))5fit <- lm(model_formula, data = training)
    values this stepscore ~ hoursmodel_formulahourspredictor
  5. fit ← lm(score ~ hours)

    4model_formula <- as.formula(paste("score ~", predictor))5fit <- lm(model_formula, data = training)6slope <- round(coef(fit)[[predictor]], 1)
    values this steplm(score ~ hours)fitscore ~ hoursmodel_formula
  6. slope ← 7

    5fit <- lm(model_formula, data = training)6slope <- round(coef(fit)[[predictor]], 1)7cat(slope, "\n", sep = "")
    values this step7slopelm(score ~ hours)fit
  7. cat(slope, " ", sep = "")

    6slope <- round(coef(fit)[[predictor]], 1)7cat(slope, "\n", sep = "")
    output7
    values this step7slope
  1. training ← 4 rows x 3 cols

    1training <- data.frame(hours = c(1, 2, 3, 4), effort = c(1, 1, 2, 3), score = c(55, 60, 70, 75))2predictor_index <- 2
    values this step4 rows x 3 colstraining
  2. predictor_index ← 2

    1training <- data.frame(hours = c(1, 2, 3, 4), effort = c(1, 1, 2, 3), score = c(55, 60, 70, 75))2predictor_index <- 23predictor <- c("hours", "effort")[predictor_index]
    values this step2predictor_index
  3. predictor ← effort

    2predictor_index <- 23predictor <- c("hours", "effort")[predictor_index]4model_formula <- as.formula(paste("score ~", predictor))
    values this stepeffortpredictor2predictor_index
  4. model_formula ← score ~ effort

    3predictor <- c("hours", "effort")[predictor_index]4model_formula <- as.formula(paste("score ~", predictor))5fit <- lm(model_formula, data = training)
    values this stepscore ~ effortmodel_formulaeffortpredictor
  5. fit ← lm(score ~ effort)

    4model_formula <- as.formula(paste("score ~", predictor))5fit <- lm(model_formula, data = training)6slope <- round(coef(fit)[[predictor]], 1)
    values this steplm(score ~ effort)fitscore ~ effortmodel_formula
  6. slope ← 9.1

    5fit <- lm(model_formula, data = training)6slope <- round(coef(fit)[[predictor]], 1)7cat(slope, "\n", sep = "")
    values this step9.1slopelm(score ~ effort)fit
  7. cat(slope, " ", sep = "")

    6slope <- round(coef(fit)[[predictor]], 1)7cat(slope, "\n", sep = "")
    output9.1
    values this step9.1slope
lm `lm(formula, data)` fits a linear model using names from `data`.
as.formula `as.formula` turns text into a formula object.
coef `coef(fit)` returns named model coefficients.