Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

ANOVA using ModelSpec

We illustrate how to run specific ANOVA analyses using ModelSpec, with statsmodels’ anova_lm.

We will carry out two simple ANOVA analyses of the Hitters data. We wish to predict a baseball player’s Salary on the basis of various statistics associated with performance in the previous year.

np.int64(59)

We see that Salary is missing for 59 players. The dropna() method of data frames removes all of the rows that have missing values in any variable (by default --- see Hitters.dropna?).

Index(['AtBat', 'Hits', 'HmRun', 'Runs', 'RBI', 'Walks', 'Years', 'CAtBat', 'CHits', 'CHmRun', 'CRuns', 'CRBI', 'CWalks', 'League', 'Division', 'PutOuts', 'Assists', 'Errors', 'Salary', 'NewLeague'], dtype='str')

Grouping variables

A look at the description of the data shows that there are both career and 1986 offensive stats, as well as some defensive stats.

Let’s group the offensive into recent and career offensive stats, as well as a group of defensive variables.

We’ll first do a sequential ANOVA where terms are added sequentially

Loading...

We’ll first produce the sequential, or Type I ANOVA results. This builds up a model sequentially and compares two successive models.

Loading...

We can similarly compute the Type II ANOVA results which drops each term and compares to the full model.

Loading...