Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 28 ยท Practice and Review

Practice Exercises

Test your Statsmodels skills with eight hands-on exercises on OLS, formulas, diagnostics, logit, counts, tests and forecasting. Each task has a runnable solution to compare against.

The best way to learn modeling is to fit models. Each exercise below gives a task and a runnable solution in an editor. Try to solve the task yourself first: change the editor code, or write your own before running the provided one. The data are simulated with known answers, so you can check whether you recovered the truth.

Exercise 1: Fit and interpret

Simulate 150 points from y = 4 + 2.5 x + noise (noise standard deviation 3) with x uniform from 0 to 10. Fit OLS with the array API. Print the slope and its 95% confidence interval. Does the interval contain 2.5?

Python Solution 1: array API OLS

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 2: Formula with a category and an interaction

Build a DataFrame with dose (0-10), group ("drug" or "placebo") and a response where the drug has a steeper slope than the placebo. Fit response ~ dose * C(group) and decide whether the interaction is significant.

Python Solution 2: interaction

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The placebo-specific slope term should be negative and clearly significant: the drug slope is about 1.8 and the placebo about 1.2 lower.

Exercise 3: Find the problem

The data below hide a curved relationship. Fit a straight line, run the RESET test and a residual check, then fix the model and show that AIC improves.

Python Solution 3: diagnose and fix

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 4: Logistic regression and odds ratios

Simulate a loan default outcome that depends on debt_ratio and late_payments. Fit a logit, print odds ratios and the predicted default probability for a customer with debt ratio 0.5 and 2 late payments.

Python Solution 4: logit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 5: Counts with exposure

Customers file support tickets. Observation periods differ (months). Fit a Poisson GLM with an offset and report the rate ratio of premium plans.

Python Solution 5: Poisson with offset

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 6: Compare three groups

Three teaching methods are tried on 25 students each. Run a one-way ANOVA and, if it is significant, Tukey HSD.

Python Solution 6: ANOVA and Tukey

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 7: Forecast with a holdout

Simulate 5 years of monthly data with trend and seasonality, hold out the last 6 months, fit Holt-Winters and compare its RMSE with the seasonal naive forecast.

Python Solution 7: forecasting

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 8: Robust and multiple testing

You test 30 unrelated variables against a noisy outcome. How many look significant at 5% before and after a Holm correction? This repeats the lesson of ANOVA and Multiple Testing.

Python Solution 8: false positives

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Going further

Keep practicing with your own data. Re-do the projects with a different dataset, and look back at Pitfalls and Debugging whenever results look too good.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. In Exercise 5, what does the offset np.log(months) accomplish?

    The offset fixes the coefficient of log exposure at one, turning the model into a rate model.
  2. 2. A significant ANOVA is followed by what?

    The F-test only says that at least one mean differs.
  3. 3. What is the purpose of a holdout set in Exercise 7?

    Honest evaluation needs data that were not part of the fit.

Summary

  • You practiced OLS, formulas with interactions, diagnostics, logit, count models with offsets, ANOVA with Tukey, forecasting and multiple-testing control.
  • Simulated data with known truths is the best way to test whether your code and understanding are right.

Next: Statsmodels Cheat Sheet