I'd like to do a pairwise comparison post-hoc test on Levene's test in R. I know how to do it in SAS using PROC GLM but I can't seem to figure out how to do it in R. Does anyone have any idea? In the example below I'd like to be able to test the homogeneity of the variance between all levels of "cat" i.e. A-B, A-C, A-D, B-C, B-D, C-D. The best way I've found is to subset my data to each of those pairs, then run a Levene's test for each subset, then do a Bonferroni correction at the end. However, this isn't a practical solution when my number of factors becomes large.
library(car)
dat <- rnorm(100, mean=50, sd=10)
cat <- rep(c("A", "B", "C","D"), each=25)
df <- data.frame(cat,dat)
df$cat <- as.factor(df$cat)
LT <- leveneTest(dat ~ cat, data = df)
Because a Levene test is simply an ANOVA conducted on sample variance (residuals) instead of sample means, you can calculate the residuals manually, then run the ANOVA with a TukeyHSD test as a post-hoc.
First, multiple comparisons as the title suggests: Using your example, with an extra factor (cat2) so we can do an interaction as well:
df <- df %>% group_by(cat, cat2) %>%
mutate(dat.med = ifelse(dat,median(Ctmax, na.rm=TRUE), ifelse(dat==NA, NA)))
The code above skips NA values and calculates sample medians for each factor combination, putting them in a new column (dat.med) in the dataset.
Then we calculate the residuals, changing them to absolute values and putting them in another column:
df$dat.med.res<-abs(df$dat-df$dat.med)
# Then we run an ANOVA, and post-hoc if necessary:
levene.dat.aov<-aov(dat.med.res~cat*cat2,df)
summary(levene.dat.aov)
TukeyHSD(levene.dat.aov)
To add in repeated measures, change the anova to:
aov(dat.med.res~cat+Error(Subject/(cat)),df)
For a pairwise comparison with a two-level factor (using package PairedData):
levene.var.test(df$dat[df$cat=="A"], df$dat[df$cat=="B"],location=c("median"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With