One-way ANCOVA
tags: #statistics/inferential/anova/one_way
Running One-Way ANCOVA
Option 1: pingouin
We can run a one-way ANCOVA using the ancova() function from pingouin module:
from pingouin import ancova
To run one-way ANCOVA with one covariate:
# Run ANCOVA analysis with one covariate
ancova(dv='dependent_variable', between='independent_variable', covar='covariate', data=df)
To run a one-way ANCOVA with more than one covariate:
`# Run ANCOVA analysis with multiple covariates
ancova(dv='dependent_variable', between='independent_variable', covar=['covariate1', 'covariate2'], data=df)`
This returns the following output:
Source SS DF F p-unc np2
0 group 3734.000000 1.0 15.226892 1.066263e-03 0.615174
1 covariate 500.502896 1.0 2.046160 1.563344e-01 0.137182
2 Residual 2322.000000 17.0 NaN NaN NaN
The output includes the source of variation (i.e., the independent variables), the sum of squares (SS), the degrees of freedom (DF), the F-statistic, the uncorrected p-value, and the effect size (np2).
Option 2: statsmodel
Alternatively, we can use the statsmodel.api:
import statsmodels.api as sm
from statsmodels.formula.api import ols
# Define the data for the ANCOVA
df = pd.DataFrame({'dependent_variable' : [8, 7, 9, 11, 10, 12, 14, 13, 15, 16],
'group' : ["A", "A", "A", "B", "B", "B", "C", "C", "C", "C"],
'covariate' : [20, 30, 40, 30, 40, 50, 40, 50, 60, 70]})
# Perform the ANCOVA
model = ols('dependent_variable ~ group + covariate', data=df).fit()
# Print the summary of the model
print(model.summary())
Sample Output:
OLS Regression Results
==============================================================================
Dep. Variable: dependent_variable R-squared: 0.864
Model: OLS Adj. R-squared: 0.820
Method: Least Squares F-statistic: 19.82
Date: Wed, 23 Mar 2023 Prob (F-statistic): 0.00216
Time: 14:00:00 Log-Likelihood: -14.011
No. Observations: 10 AIC: 34.02
Df Residuals: 7 BIC: 35.06
Df Model: 2
Covariance Type: nonrobust
===================================================================================
coef std err t P>|t| [0.025 0.975]
-----------------------------------------------------------------------------------
Intercept 3.0000 1.414 2.121 0.068 -0.286 6.286
group[T.B] 3.0000 1.734 1.730 0.129 -1.112 7.112
group[T.C] 5.0000 1.734 2.885 0.023 0.888 9.112
covariate 0.1000 0.025 3.964 0.004 0.042 0.158
==============================================================================
Omnibus: 0.106 Durbin-Watson: 2.045
Prob(Omnibus): 0.948 Jarque-Bera (JB): 0.315
Skew: 0.088 Prob(JB): 0.854
Kurtosis: 2.126 Cond. No. 254.
==============================================================================
Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
Interpreting the Output
- Group A is used as the reference level, against which the other groups (B and C) are compared.
- Coefficients for group B and group C are the differences in their means compared to the mean of group A.
- Significance of the overall model can be determined by the F-statistic and its corresponding probability.