gpt4 book ai didi

r - 使用multcomp设置三向交互的对比

转载 作者:行者123 更新时间:2023-12-02 14:21:58 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com