简体   繁体   中英

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).

The data (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:

model<-glm(resp~year*tmt, family=binomial,data=testsumm)

The result told me that there is an interaction between year and 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. 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.

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. However, the lsmeans package provides an alternative way to specify what you need.

library(lsmeans)
glht(model2, lsm(~ tmt | year))

Certainly saves a lot of typing!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM