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.

model_formula.R
training <- data.frame(hours = c(1, 2, 3, 4), effort = c(1, 1, 2, 3), score = c(55, 60, 70, 75))
predictor_index <- 
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 <- 
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 = "")
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.