Skip to content

Instantly share code, notes, and snippets.

@guoguo12
Last active April 6, 2018 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guoguo12/c1158b03ae4f091b4abe5d79abe7c579 to your computer and use it in GitHub Desktop.
Save guoguo12/c1158b03ae4f091b4abe5d79abe7c579 to your computer and use it in GitHub Desktop.
R script for performing ANCOVA with post-hoc tests and showing adjusted group means.
# Based on https://stats.stackexchange.com/a/57254
# Before running, look at all comments starting with XXX below
library('car')
library('compute.es')
library('effects')
library('emmeans')
library('ggplot2')
library('multcomp')
library('pastecs')
library('WRS2')
# XXX Update this with the data file location
df <- read.csv('C:\\Users\\guogu_000\\Documents\\Research\\R\\data.csv')
# XXX Update this with the number of levels for the categorical factor
contrasts(df$factor) = contr.sum(2)
model = aov(dv ~ factor + covariate + factor:covariate, data=df)
# Using 'II' should give more-or-less the same output as statsmodels
Anova(model, type='III')
posth = glht(model, linfct=mcp(factor='Tukey'))
summary(posth)
emmeans(model, ~ factor:covariate)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment