I am trying to run TukeyHSD for a data set (40 observations). The outcome is 'mbvs' and the two factors are sex (0 or 1) and treatment (0 or 1) ?
I am trying to find out what is the pairwise difference in the mean outcome between treatment and control groups in men (sex=0)?
Could someone help me with the code for this ? Thank you.
CodePudding user response:
Look, I do not whether this answer can help you, but I've tried to semplify the dataset you provided with a smallest one, just to make you understand the logic presiding over it.
library(multcomp)
library(dplyr)
mbvs <- c(6.4, 6.2, 6.9, 6.9, 5.4, 8.4)
sex <- c(0,0,0,0,0,1)
tmt <- c(0,1,0,0,0,1)
#let's make this as your dataset
df1 <- data.frame(mbvs, sex, tmt, stringsAsFactors = FALSE)
postHocs = df1 %>%
mutate(sex = factor(sex, levels = c('0', '1')),
tmt = factor(tmt, levels = c('0', '1'))) %>%
filter(sex == '0') %>%
aov(mbvs ~ tmt, data = .) %>%
glht(., linfct = mcp(tmt = 'Tukey'))
The final result I've obtained is
General Linear Hypotheses
Multiple Comparisons of Means: Tukey Contrasts
Linear Hypotheses:
Estimate
1 - 0 == 0 -0.2
Alternatively, with the TukeyHSD function you can run this code
postHocs = df1 %>%
mutate(sex = factor(sex, levels = c('0', '1')),
tmt = factor(tmt, levels = c('0', '1'))) %>%
filter(sex == '0') %>%
aov(mbvs ~ tmt, data = .) %>%
TukeyHSD(., conf.level=.95)
Tukey multiple comparisons of means
95% family-wise confidence level
Fit: aov(formula = mbvs ~ tmt, data = .)
$tmt
diff lwr upr p adj
1-0 -0.2 -2.715945 2.315945 0.8166266