Learn R Programming

mariposa (version 0.7.3)

ancova: Analysis of Covariance: ANCOVA

Description

ancova() tests whether group means differ after controlling for one or more continuous covariates. It performs a factorial ANCOVA using Type III Sum of Squares, matching SPSS UNIANOVA output with the WITH keyword.

Think of it as:

  • An ANOVA that removes the effect of confounding variables first

  • Testing group differences on "adjusted" means

  • Combining regression (covariates) and ANOVA (factors) in one model

The test tells you:

  • Whether each factor has a significant effect AFTER controlling for covariates

  • The relationship between each covariate and the outcome

  • Effect sizes for factors and covariates (partial eta squared)

  • Estimated marginal means (group means adjusted for covariates)

Usage

ancova(data, dv, between, covariate, weights = NULL, ss_type = 3)

Value

An object of class "ancova" containing:

anova_table

Tibble with Source, SS, df, MS, F, p, Partial Eta Squared

parameter_estimates

Tibble with regression coefficients (B, SE, t, p)

descriptives

Tibble with unadjusted cell means, SDs, and Ns

estimated_marginal_means

Tibble with adjusted means (covariates at grand mean)

levene_test

Tibble with Levene's test results

r_squared

R-squared and Adjusted R-squared

model

The underlying lm model object

call_info

List with metadata (dv, factors, covariates, weighted, etc.)

Use summary() for the full SPSS-style output with toggleable sections.

Arguments

data

Your survey data (a data frame or tibble)

dv

The numeric dependent variable to analyze (unquoted)

between

Character vector or unquoted variable names specifying the between-subjects factors (1-3 factors). These must be categorical variables (factor, character, or labelled numeric).

covariate

Unquoted variable names of the continuous covariates to control for. Use c(age, income) for multiple covariates.

weights

Optional survey weights for population-representative results (unquoted variable name)

ss_type

Deprecated; only 3 (Type III, the SPSS default) is implemented. Passing 2 issues a warning and computes Type III.

Details

Understanding the Results

Adjusted Means (Estimated Marginal Means): These are the group means after statistically removing the effect of the covariate(s). They answer: "What would the group means be if all groups had the same covariate values?"

Covariate Effects: The covariate row in the ANOVA table shows whether the covariate significantly predicts the DV after adjusting for the factors.

Factor Effects: These show whether the factor affects the DV after controlling for the covariate. This is the primary test of interest.

Partial Eta Squared (Effect Size):

  • Less than 0.01: Negligible

  • 0.01 to 0.06: Small

  • 0.06 to 0.14: Medium

  • 0.14 or greater: Large

When to Use This

Use ANCOVA when:

  • You have group comparisons (ANOVA) but want to control for a confound

  • Your covariate is continuous and linearly related to the DV

  • You want to increase statistical power by removing known variance sources

  • The covariate's relationship with the DV is the same across groups (homogeneity of regression slopes assumption)

References

Huitema, B. E. (2011). The Analysis of Covariance and Alternatives (2nd ed.). Wiley.

IBM Corp. (2023). IBM SPSS Statistics 29 Algorithms. IBM Corporation.

See Also

factorial_anova for ANOVA without covariates.

linear_regression for regression analysis.

oneway_anova for single-factor ANOVA.

summary.ancova for detailed output with toggleable sections.

Other hypothesis_tests: binomial_test(), chi_square(), chisq_gof(), factorial_anova(), fisher_test(), friedman_test(), kruskal_wallis(), mann_whitney(), mcnemar_test(), oneway_anova(), t_test(), wilcoxon_test()

Examples

Run this code
# Load required packages and data
library(dplyr)
data(survey_data)

# One-way ANCOVA: income by education, controlling for age
survey_data %>%
  ancova(dv = income, between = c(education), covariate = c(age))

# Two-way ANCOVA with weights
survey_data %>%
  ancova(dv = income, between = c(gender, education),
         covariate = c(age), weights = sampling_weight)

# Multiple covariates
survey_data %>%
  ancova(dv = income, between = c(education),
         covariate = c(age, political_orientation))

# --- Three-layer output ---
result <- ancova(survey_data, dv = income, between = c(education),
                 covariate = c(age))
result              # compact overview
summary(result)     # full detailed output with all sections
summary(result, marginal_means = FALSE)  # hide estimated marginal means

Run the code above in your browser using DataLab