0
$\begingroup$

I have a dataset that I am analyzing in R. In this data set a value of 0 is meaningful. Therefore, I would like to compare each group mean to 0 rather than comparing them to each other as is typically done. The data has two factors (3 levels and 11 levels, but for the sake of brevity we can consider both to have 3 levels), a covariate (continuous), and a random variable (factor with 5 levels).

fit <- lme(value ~ factorA + factorB + covariate, dat, random= ~ 1|randomVar)

tuk <- glht(fit,linfct = mcp(factorA = "Tukey"))

With this code I get all of the comparisons of the different levels of factorA to each other. I want comparisons of each level of factorA and each level of factorB to zero, and importantly that needs to include the reference levels of both factors. I know that I need to set up the contrasts different, but I don't know how.

Edit: I tried to set the contrasts manually with e.g c(1,0,0,...) for factorA_level1 and c(1,1,0,...) for factorA_level2 but it appears to not give the overall group means but rather the group means for the reference level of factorB or something similar.

$\endgroup$

1 Answer 1

1
$\begingroup$

A way with the emmeans package:

library(emmeans)
library(multcomp)
library(nlme)

dat <- iris
dat$factor <- as.factor(rep(1:3,50))

fit <- lme(Sepal.Length ~ factor + Sepal.Width, dat, random = ~ 1 | Species)

mm <- emmeans(fit, ~factor)
mm # individual confidence intervals
# factor emmean   SE df lower.CL upper.CL
# 1        5.87 0.59  2     3.33     8.41
# 2        5.77 0.59  2     3.23     8.31
# 3        5.89 0.59  2     3.35     8.43
# 
# Degrees-of-freedom method: containment 
# Confidence level used: 0.95 

# simultaneous tests and confidence intervals ####
mult <- as.glht(mm)
summary(mult)
# Simultaneous Tests for General Linear Hypotheses
# 
# Linear Hypotheses:
#          Estimate Std. Error t value Pr(>|t|)  
#   1 == 0   5.8692     0.5898   9.951   0.0115 *
#   2 == 0   5.7689     0.5899   9.780   0.0120 *
#   3 == 0   5.8920     0.5899   9.989   0.0113 *
#   ---
#   Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# (Adjusted p values reported -- single-step method)

confint(mult) 
# Simultaneous Confidence Intervals
# 
# 95% family-wise confidence level
# 
# Linear Hypotheses:
#        Estimate lwr    upr   
# 1 == 0 5.8692   3.1193 8.6190
# 2 == 0 5.7689   3.0189 8.5189
# 3 == 0 5.8920   3.1420 8.6419
$\endgroup$
3
  • $\begingroup$ Excellent, thanks. I am going to try this out as soon as possible. I will let you know how it works $\endgroup$
    – Dale
    Feb 26, 2019 at 23:22
  • $\begingroup$ This was exactly what I needed. It worked perfectly, thank you. I have accepted this answer. $\endgroup$
    – Dale
    Feb 27, 2019 at 5:19
  • 1
    $\begingroup$ You can also do this in fewer steps: test(mm, null = 0, adjust = “mvt")` $\endgroup$
    – Russ Lenth
    Feb 27, 2019 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.