1

Does ANCOVA require homogeneity of regression slopes? In other words, do the slopes of the lines need to be the same in order to use this method?

Per this blog post ANCOVA can be used to discriminate between models, with different slopes, but this blog post says the slopes must be parallel for ANCOVA to be used. Please explain the discrepancy in plain language if possible. I have some simulated data with Python code below as an example for context.

Example Data:

## Module Imports
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

import statsmodels.api as sm import statsmodels.formula.api as smf

Generate sample data for Z statistic method

g1_count = 100 # group 1 number of datapoints g2_count = 100 # group 2 number of datapoints

x1 = np.arange(5, 15, 10/g1_count) # array of x values for group 1 y1 = x1 + 3 + np.random.normal(0, 1, g1_count) # array of x values for group 2

x2 = np.arange(0, 10, 10/g2_count) # array of y values for group 1 y2 = 2*x2 + 3 + np.random.normal(0, 1, g2_count) # array of y values for group 2

df = pd.DataFrame({"x1": x1, "x2": x2, "y1": y1, "y2": y2}) # create dataframe

Plot Data

fig, ax = plt.subplots()

ax.scatter(x1,y1) ax.scatter(x2,y2)

x-y_scatter

## Sample data for ANCOVA, combines x and y values to a single column each and adds a categorical column
x_c = np.concatenate((x1, x2))
y_c = np.concatenate((y1, y2))
group_list = np.concatenate((np.array((len(x1)*["A"])),np.array((len(x2)*["B"]))))

df_c = pd.DataFrame({"x": x_c, "y": y_c, "group": group_list}) df_c

df_c

Fit Model and See Summary Statistics

lm_ancova = smf.ols('y ~ group + x', data=df_c).fit()
lm_ancova.summary()

summary_stats

Null hypothesis is rejected. The two linear models are likely not the same given the low p-values.

However, the assumption that there is no interaction between the group and covariate (x) fails according to an ANOVA test as described here.

inter_lm = smf.ols('y ~ group * x', data=df_c).fit()  # fit linear interaction model
sm.stats.anova_lm(inter_lm, typ=3)  # ANOVA test on interaction model

anova_interaction_test

If you have any suggestions for a more appropriate test implemented in Python it would be much appreciated.

2 Answers2

0

The formal answer to this question is "yes", the ANCOVA test requires homogeneity of slopes (equal slopes for the groups). But with regards to the underlying motivation for the question, the answer is "no", equal slope is NOT a requirement...if you are asking if the groups are different (in a broad sense).

Briefly, the question driving the null hypothesis statistical test (NHST) for the ANCOVA (analysis of covariance) is this: ¿Are my groups different on the dependent variable AFTER having controlled for a covariate that comparably impacts that dependent variable? And the power of this test is that it allows you to "correct" for variation across the groups if the covariates are not the same from group to group. For this analysis, you do indeed need a common slope (particularly if you intend to calculate the adjusted means for the groups).

However, if the core of your question is this: ¿are my groups different from each other? ...then the requirement for equal slope is less pressing. If the slopes are not equal, then you have a situation of moderation (where the covariate moderates the relationship between the groups and the dependent variable). Thus, your groups are REALLY different from each other...it is not just the intercepts that are (may be) different, but the slopes ALSO are different.

Hope this helps clarify.

Gregg H
  • 5,474
  • @Gegg H, thanks this helps! In the second case, if the intercept p value is significant, but the group is not, what does that mean? My end use case is to determine if the slopes of linear fits to two groups are actually different from each other (the difference in slopes is not as drastic as in my example dataset, but is otherwise similar). Is there another model besides ANCOVA you would recommend for this? – Random Engineer May 22 '23 at 15:18
  • To test to see if the slopes are the same or not, I recommend running the ANCOVA model (the additive moderation model...no interaction term) and the multiplicative moderation model (the ANCOVA model with the interaction terms added). Then you can run a model comparison test (an F-ratio test) to determine if the models are statistically different. If so, then the slopes are different; if not, then you can proceed with the assumption of equal slopes. – Gregg H May 22 '23 at 15:26
-1

The "ANCOVA model" usually refers to a model in which the slopes for the covariates are assumed equal across groups. The first post is using an unusual form of ANCOVA. Allowing the sloeps to vary across groups is sometimes called "the full model in every cell". See the following quote from Muller and Fetterman (2003, Ch 16):

The full model in every cell is a generalization of ANCOVA that allows interactions between continuous and categorical predictors. [...] The traditional analysis of covariance (ANCOVA) model is a special case of the full model in every cell. Adding the restriction of equal slopes reduces the full model to ANCOVA.

The blog also describes an invalid methodology of testing whether the slopes are different and then fitting a model assuming they are the same if nonsignificant. The absence of statistical significance doesn't mean an interaction is absent, and building a model by backward selection invalidates p-values computed in the final step.

What happens if the true data-generating model has different slopes for the two groups and you fit a model that assumest hey have the same slope? This is described in detail by Słoczyński (2022) and Chattopadhyay and Zubizirreta (2022). Basically, the effect of the grouping variable will be estimated for a population other than the one under study, and the papers describe the exact nature of that population, which tends to resemble the smaller group. In contrast, allowing the slopes to differ ensures the estimated effect generalizes to the population from which the sample was drawn. Schafer and Kang (2008) also describe why ANCOVA is problematic and why you should allow the slopes to vary. In the context of experiments, Lin (2013) explains why you not only need to allow the slopes to vary, you need to use a special "robust" standard error to correctly calculate the p-values.

I disagree with Gregg H that the choice should depend on what hypothesis you are testing. Even if you are testing the simple hypothesis of whether the groups differ after adjusting for covariates and have no substantive interest in the interaction, you should still include the interaction between the grouping variable and the centered covariates. You must center the covariates at their mean in the sample in order to interpret the coefficient on the grouping variable as the adjusted difference in means. You should not use a hypothesis test of whether an interaction is present to decide how to fit the model; just fit a single model and interpret it. There is very little cost to including an interaction when there is none in the data-generating model.

Noah
  • 33,180
  • 3
  • 47
  • 105
  • Thank you for helpful reply! How would you physically interpret a model with the interaction term? My end goal is to determine if the slopes of my linear models between groups are significantly different from each other. How can I determine significance is using a model with interaction, but no hypothesis test? – Random Engineer May 22 '23 at 15:22
  • You can interpret the test of the interaction as a test of whether the slopes differ. My answer doesn't recommend otherwise. But if you want a single estimate of the average slope whether an interaction is present or not, you can use the strategy described in my answer (centering the predictor). That is, when you fit an interaction model, you can test whether an interaction is present, and even if it isn't you can interpret the estimated slope as the slope, just as you would in a regular ANCOVA with no interaction. A single model gives you both. – Noah May 22 '23 at 18:27
  • 1
    I would love an explanation for the downvote given that my answer is supported by quotes and citations and directly answers the OP's questions. @RandomEngineer, if you feel my answer was helpful, please upvote it (and do the same for all answers you find helpful). – Noah May 22 '23 at 18:29
  • Thanks for the additional explanation, I am slowly understanding a lot more. I already upvoted your answer (as well as Greg's), but I don't think it registers as a visible upvote because I am a new user on Cross Validated – Random Engineer May 22 '23 at 21:06