Back to Course
Predictive Modeling

Predictive Modeling

Regression models are the foundation of predictive analytics in R. This lesson covers linear regression for continuous outcomes and logistic regression for binary classification, using R's lm() and glm() functions.

1. Simple Linear Regression with lm()

model <- lm(salary ~ experience, data = df)
summary(model)

The formula salary ~ experience reads as "salary predicted by experience." summary() reveals coefficients, p-values, and model fit.

2. Reading lm() Output

FieldMeaning
EstimateThe coefficient — change in Y per unit change in X
Std. ErrorUncertainty around the estimate
t value / Pr(>|t|)Test statistic and p-value for that coefficient
Multiple R-squaredProportion of variance explained by the model
Adjusted R-squaredR-squared penalized for number of predictors

3. Multiple Linear Regression

model <- lm(salary ~ experience + age + education_years, data = df)
summary(model)

# Coefficients with confidence intervals
confint(model)

4. Making Predictions

new_employee <- data.frame(experience = 5, age = 30, education_years = 16)
predict(model, newdata = new_employee)
predict(model, newdata = new_employee, interval = "confidence")

5. Checking Model Assumptions

par(mfrow = c(2, 2))
plot(model)   # residuals vs fitted, Q-Q plot, scale-location, leverage
par(mfrow = c(1, 1))
Production Reality: Never trust a linear model's coefficients without checking the diagnostic plots — non-random residual patterns signal a poor model fit.

6. Logistic Regression with glm()

For binary outcomes (yes/no, churn/retain, pass/fail), use glm() with a binomial family:

model_logit <- glm(churned ~ tenure + monthly_charges + support_calls,
                   data = df, family = binomial)
summary(model_logit)

7. Interpreting Logistic Coefficients — Odds Ratios

exp(coef(model_logit))   # convert log-odds coefficients to odds ratios
exp(confint(model_logit)) # confidence intervals for odds ratios

An odds ratio above 1 means the predictor increases the probability of the outcome; below 1 means it decreases it.

8. Making Classification Predictions

predicted_probs <- predict(model_logit, type = "response")
predicted_class <- ifelse(predicted_probs > 0.5, "Yes", "No")

table(Predicted = predicted_class, Actual = df$churned)  # confusion matrix

9. Linear vs. Logistic — When to Use Which

ScenarioModel
Predicting a continuous number (price, salary)lm() — linear regression
Predicting a binary outcome (yes/no)glm(family = binomial) — logistic regression
Predicting counts (e.g., number of visits)glm(family = poisson) — Poisson regression

10. Production-Ready Checklist

  • ✅ Use lm() for continuous outcomes — always inspect summary() and diagnostic plots.
  • ✅ Use glm(family = binomial) for classification — interpret via odds ratios.
  • ✅ Check assumptions — linearity, normal residuals, no severe multicollinearity.
  • ✅ Validate on new data — training accuracy alone can be misleading.
Pro Tip: Regression is as much about interpretation as prediction. A model with a lower R-squared but interpretable, statistically sound coefficients is often more valuable than a black-box alternative.

Ready to master R Programming?

Build real-world data analysis and visualization projects with hands-on training, mentor-led sessions, and placement support.

Explore Course