简体   繁体   English

R中如何在GLM,具有交互作用的分类数据之后更改事后对比中的因素顺序

[英]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.

相关问题 R lm 如何选择与分类变量和连续变量之间的交互作用的对比? - How does R lm choose contrasts with interaction between a categorical and continuous variables? glmmTMB:因素交互的成对比较事后检验 - glmmTMB: Pairwise comparison post-hoc tests for factors interaction 当“对比度只能应用于两个或两个以上级别的因子”时,如何执行GLM? - How to do a GLM when “contrasts can be applied only to factors with 2 or more levels”? 如何在R h2o包中包括因子与h2o.interaction和h2o.glm的交互 - How to include interactions of factors with h2o.interaction and h2o.glm in R h2o package 具有分类数据的Poisson GLM - Poisson GLM with categorical data R:交互效应中的特殊对比 - R: Special contrasts within an interaction effect 如何通过 TOBIT model(censReg 包)的对比进行事后分析? - How to do post-hoc analysis with contrasts of a TOBIT model (censReg package)? 更改在lm()中用冒号指定的交互作用项的对比度 - change contrasts of interaction term specified with colon in lm() 即使在将我的变量转换为因子之后,在运行对比 function 时,它也会说“对比仅适用于因子” - Even after converting my variable into factors , upon running the contrasts function, it says “contrasts apply only to factors” 如何将 R 数字数据框更改为有序因子的数据框? - How to change a R numeric data frame into a data frame of ordered factors?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM