我在三个站点进行了一项BACI实验,该实验在2007年之前和2011年之后进行了测量。设置对比度时遇到了麻烦,因此我了解了三个站点中每个站点的年效果是否有所不同(参见对照)。
我尝试在vignette中扩展示例,但没有成功。也尝试过lsmeans软件包。
下面的示例df和代码,以及我要测试的显式对比。
library(nlme)
library(multcomp)
require(lsmeans)
# I have these versions:
# R version 3.2.4 Revised (2016-03-16 r70336)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows 7 x64 (build 7601) Service Pack 1
# nlme_3.1-126
# multcomp_1.4-4
exdf <- expand.grid(Site = c("Z", "Y", "X"),
Plot = 1:30,
Year = c("2007", "2011"),
Treatment = c("Control", "A", "B", "A+B"))
exdf$Site <- factor(exdf$Site)
exdf$Plot <- factor(exdf$Plot)
exdf$Year <- factor(exdf$Year)
exdf$Treatment <- factor(exdf$Treatment)
exdf$Treatment <- relevel(exdf$Treatment, ref = "Control")
exdf$Response <- rnorm(mean = 1150, sd = 130, n = nrow(exdf))
mod1 <- lme(data = exdf,
fixed = Response ~ Year * Treatment * Site,
random = ~1|Plot)
## would like to test (obviously that's not actual code)
## How do the treatments A, B, A+B differ in their interaction with
## year, compared to the control treatment (ie do the slopes differ)
SiteX:Year:TreatmentControl - SiteX:Year:TreatmentA == 0
SiteX:Year:TreatmentControl - SiteX:Year:TreatmentB == 0
SiteX:Year:TreatmentControl - SiteX:Year:TreatmentA+B == 0
SiteY:Year:TreatmentControl - SiteY:Year:TreatmentA == 0
SiteY:Year:TreatmentControl - SiteY:Year:TreatmentB == 0
SiteY:Year:TreatmentControl - SiteY:Year:TreatmentA+B == 0
SiteZ:Year:TreatmentControl - SiteZ:Year:TreatmentA == 0
SiteZ:Year:TreatmentControl - SiteZ:Year:TreatmentB == 0
SiteZ:Year:TreatmentControl - SiteZ:Year:TreatmentA+B == 0
## Also potentially - does the interaction between site and year
## differ across sites?
SiteX:Year:TreatmentA - SiteY:Year:TreatmentA == 0
SiteZ:Year:TreatmentA - SiteY:Year:TreatmentA == 0
SiteZ:Year:TreatmentA - SiteX:Year:TreatmentA == 0
SiteX:Year:TreatmentB - SiteY:Year:TreatmentB == 0
SiteZ:Year:TreatmentB - SiteY:Year:TreatmentB == 0
SiteZ:Year:TreatmentB - SiteX:Year:TreatmentB == 0
SiteX:Year:TreatmentA+B - SiteY:Year:TreatmentA+B == 0
SiteZ:Year:TreatmentA+B - SiteY:Year:TreatmentA+B == 0
SiteZ:Year:TreatmentA+B - SiteX:Year:TreatmentA+B == 0
## Started doing the example from
#https://cran.r-project.org/web/packages/multcomp/vignettes/multcomp-examples.pdf
# Got a bit stuck
tmp <- expand.grid(Year = levels(exdf$Year),
Treatment = levels(exdf$Treatment),
Site = levels(exdf$Site))
X <- model.matrix(~ Year * Treatment * Site, data = tmp)
glht(mod1, linfct = X)
## Gives all the interactions not in the form I want..
tt <- lsmeans(mod1, specs = ~Year:Treatment|Site)
comps <- pairs(tt, interaction = TRUE)
summary(comps, adjust = "holm")
最佳答案
让我们逐步构建它。首先,您希望获得其他因素的每种组合在两年中的差异。使用 lsmeans 可以轻松完成此操作
tt = lsmeans(mod1, specs = ~ Year | Treatment:Site)
dd = pairs(tt, reverse = TRUE) # (2011) - (2008) for each T*S comb
summary(dd, by = NULL)
现在,您可以根据需要比较这些差异:
pairs(dd, by = "Site")
pairs(dd, by = "Treatment")
...或略微比较它们:
pairs(lsmeans(dd, "Treatment"))
pairs(lsmeans(dd, "Site"))
关于r - 使用Multcomp设置三向交互的对比度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37126460/