Statsmodels
This reference explains the Statsmodels tools used in classes 1–4. Each entry gives a purpose, the arguments most useful in this course, an example, and a practical note. Follow Official documentation for the complete API.
Examples use the course datasets: df is the eruption table, df_traces the geochemical table, reading the reading-score table, and df_icecover the lake table. Samples such as dietA and dietB have already been selected. Variables such as data, ax, and mod1 refer to the array, axes, or fitted model described in the lesson. Snippets illustrate individual operations rather than forming a script to run from top to bottom.
Import statsmodels.formula.api as smf. Define a model with smf.ols(...), fit it, and inspect its residuals before interpreting coefficients or making predictions.
smf.ols()
Purpose: Define an ordinary least-squares linear model using a formula.
Main arguments: formula: response to the left of ~, predictors to the right; data: DataFrame.
Example:
model = smf.ols('score ~ group + age', data=reading)Remember: Use + for additive effects, : for an interaction, and * for main effects plus interaction.
model.fit()
Also used as: fit.
Purpose: Estimate the coefficients of a defined model.
Main arguments: Usually no arguments are needed for the course’s OLS models.
Example:
mod1 = model.fit()Remember: Fitting estimates parameters; it does not establish that the model is suitable.
model.summary()
Also used as: summary.
Purpose: Display coefficients, uncertainty and model statistics.
Main arguments: Usually no arguments are needed.
Example:
print(mod1.summary())Remember: Read coefficients in context and inspect residuals alongside the summary.
model.predict()
Also used as: predict.
Purpose: Predict the fitted mean response for new predictor values.
Main arguments: exog: DataFrame with the predictor names used in the formula.
Example:
predicted = mod1.predict(pd.DataFrame({'age': [8], 'group': ['Control']}))Remember: Predictions outside the observed range are extrapolations.
model.get_prediction()
Also used as: get_prediction.
Purpose: Compute predictions together with uncertainty information.
Main arguments: exog: new predictor values as a DataFrame.
Example:
prediction = mod3.get_prediction(data_control)Remember: Use summary_frame() to obtain a table of means and intervals.
model.summary_frame()
Also used as: summary_frame.
Purpose: Return predictions and interval bounds as a table.
Main arguments: alpha: excluded probability; 0.05 produces 95% intervals.
Example:
table = prediction.summary_frame(alpha=0.05)Remember: mean_ci_* describes the mean response; obs_ci_* describes a new observation.
model.params
Also used as: params.
Purpose: Read the fitted coefficients.
Main arguments: This is an attribute: do not add parentheses.
Example:
intercept = mod1.params['Intercept']Remember: Names depend on the formula and the reference category.
model.fittedvalues
Also used as: fittedvalues.
Purpose: Read the model’s fitted mean response for each observation.
Main arguments: This is an attribute: do not add parentheses.
Example:
predicted = mod1.fittedvaluesRemember: These predictions correspond to the observations used for fitting.
model.resid
Also used as: resid.
Purpose: Read observed minus fitted values.
Main arguments: This is an attribute: do not add parentheses.
Example:
residuals = mod1.residRemember: Look for trends, unequal spread and unusual observations.
model.aic
Also used as: aic.
Purpose: Read Akaike’s information criterion.
Main arguments: This is an attribute: do not add parentheses.
Example:
aic = mod1.aicRemember: Compare models fitted to the same response and observations; lower values favour the trade-off between fit and complexity.
model.rsquared
Also used as: rsquared.
Purpose: Read the proportion of response variation explained by the fitted model.
Main arguments: This is an attribute: do not add parentheses.
Example:
r_squared = mod1.rsquaredRemember: A high R-squared does not establish causation or validate assumptions.
anova_oneway()
Purpose: Compare independent group means using a classical or Welch one-way ANOVA.
Main arguments: data: groups; use_var="unequal": Welch-style unequal-variance test.
Example:
from statsmodels.stats.oneway import anova_oneway
result = anova_oneway([dietA, dietB, dietC], use_var='unequal')Remember: Specify the variance assumption deliberately.
wls_prediction_std()
Purpose: Calculate prediction uncertainty from a fitted least-squares model.
Main arguments: res: fitted results; exog: design matrix matching the model; alpha: interval level.
Example:
from statsmodels.sandbox.regression.predstd import wls_prediction_std
se, lower, upper = wls_prediction_std(mod1)Remember: Imported in the course; get_prediction().summary_frame() is the clearer formula-based workflow.