[英]how to change order of factors in post hoc contrasts after GLM, categorical data with interaction, in R
I have a multiway contingency table with 2 predictor variables: tmt (2 levels) and year (4 levels) and a response variable, number (out of n plots) where a species is present (succ). 我有一个包含2个预测变量的多向列联表:tmt(2个级别)和年(4个级别),以及一个响应变量,存在某个物种的数量(n个地块中的个数)(成功)。
The data (testsumm): 数据(testsumm):
year tmt n succ
1 2012 1 72 27
2 2012 0 68 6
3 2013 1 71 37
4 2013 0 71 8
5 2014 1 72 13
6 2014 0 75 9
7 2015 1 64 20
8 2015 0 67 16
After creating a vector of successes and failures 创建成功和失败的载体之后
resp<-cbind(testsumm$succ, testsumm$n-testsumm$succ)
I analyzed the data (testsumm) in R using glm as follows: 我使用glm分析了R中的数据(testsumm),如下所示:
model<-glm(resp~year*tmt, family=binomial,data=testsumm)
The result told me that there is an interaction between year and tmt. 结果告诉我,year和tmt之间存在交互作用。 Now I am trying to use posthoc tests (with R's multcomp package) to determine whether the 2 levels of tmt differ significantly WITHIN EACH YEAR.
现在,我尝试使用事后测试(带有R的multcomp程序包)来确定tmt的2个级别在每年中是否存在显着差异。 Other questions on this site directed me to https://cran.r-project.org/web/packages/multcomp/vignettes/multcomp-examples.pdf These instructions worked perfectly for comparing years WITHIN TREATMENT LEVELS.
该网站上的其他问题将我定向到https://cran.r-project.org/web/packages/multcomp/vignettes/multcomp-examples.pdf这些说明非常适合比较治疗水平内的年份。
temp <- expand.grid(year = unique(testsumm$year),tmt = unique(testsumm$tmt))
X1 <- model.matrix(~ tmt * year, data = temp)
glht(model, linfct = X1)
Tukey <- contrMat(table(testsumm$year), "Tukey")
K1 <- cbind(Tukey, matrix(0, nrow = nrow(Tukey), ncol = ncol(Tukey)))
rownames(K1) <- paste(levels(testsumm$tmt)[1], rownames(K1), sep = ":")
K2 <- cbind(matrix(0, nrow = nrow(Tukey), ncol = ncol(Tukey)), Tukey)
rownames(K2) <- paste(levels(testsumm$tmt)[2], rownames(K2), sep = ":")
K <- rbind(K1, K2)
colnames(K) <- c(colnames(Tukey), colnames(Tukey))
summary(glht(modintILAQ, linfct = K %*% X1))
So since I wanted to compare treatment levels within years, I tried exchanging the positions of the 2 variables in the code: 因此,由于我想在几年内比较治疗水平,因此我尝试交换代码中两个变量的位置:
model2 <- glm(resp ~ tmt * year, family=binomial,data = testsumm)
summary(model2)
temp2 <- expand.grid(tmt = unique(testsumm$tmt),year =unique(testsumm$year))
X12 <- model.matrix(~ tmt * year, data = temp2)
glht(model2, linfct = X12)
Tukey <- contrMat(table(testsumm$tmt), "Tukey")
K1 <- cbind(Tukey, matrix(0, nrow = nrow(Tukey), ncol = ncol(Tukey)))
rownames(K1) <- paste(levels(testsumm$year)[1], rownames(K1), sep = ":")
K2 <- cbind(matrix(0, nrow = nrow(Tukey), ncol = ncol(Tukey)), Tukey)
rownames(K2) <- paste(levels(testsumm$year)[2], rownames(K2), sep = ":")
K <- rbind(K1, K2)
colnames(K) <- c(colnames(Tukey), colnames(Tukey))
summary(glht(model2, linfct = K %*% X1))
but I got this error message 但我收到此错误消息
Error in K %*% X1 : non-conformable arguments
It's pretty clear that these 2 matrices are not the same shape, so they can't be multiplied, but I can't figure out what they should look like instead. 很显然,这两个矩阵的形状不同,因此无法相乘,但是我无法弄清楚它们应该是什么样。 Can anyone help me to create contrasts between the 2 treatment levels within each year, instead of between years within each treatment level?
谁能帮助我在每年的两种治疗水平之间建立对比,而不是在每种治疗水平的几年之间进行对比?
multcomp does not make it very easy when there is more than one factor. 当存在多个因素时, multcomp并不十分容易。 However, the lsmeans package provides an alternative way to specify what you need.
但是, lsmeans软件包提供了另一种指定所需内容的方法。
library(lsmeans)
glht(model2, lsm(~ tmt | year))
Certainly saves a lot of typing! 当然可以节省很多打字!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.