title: how to change order of factors in post hoc contrasts after GLM, categorical data with interaction, in R
body: <p>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). </p>
  
  <p>The data (testsumm):</p>
  
  <pre><code>  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
  </code></pre>
  
  <p>After creating a vector of successes and failures </p>
  
  <pre><code>resp&lt;-cbind(testsumm$succ, testsumm$n-testsumm$succ)
  </code></pre>
  
  <p>I analyzed the data (testsumm) in R using glm as follows: </p>
  
  <pre><code>model&lt;-glm(resp~year*tmt, family=binomial,data=testsumm)
  </code></pre>
  
  <p>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 
  <a href="https://cran.r-project.org/web/packages/multcomp/vignettes/multcomp-examples.pdf" rel="nofollow">https://cran.r-project.org/web/packages/multcomp/vignettes/multcomp-examples.pdf</a>
  These instructions worked perfectly for comparing years WITHIN TREATMENT LEVELS. </p>
  
  <pre><code>temp &lt;- expand.grid(year = unique(testsumm$year),tmt = unique(testsumm$tmt))
  X1 &lt;- model.matrix(~ tmt * year, data = temp)
  glht(model, linfct = X1)
  Tukey &lt;- contrMat(table(testsumm$year), "Tukey")
  K1 &lt;- cbind(Tukey, matrix(0, nrow = nrow(Tukey), ncol = ncol(Tukey)))
  rownames(K1) &lt;- paste(levels(testsumm$tmt)[1], rownames(K1), sep = ":")
  K2 &lt;- cbind(matrix(0, nrow = nrow(Tukey), ncol = ncol(Tukey)), Tukey)
  rownames(K2) &lt;- paste(levels(testsumm$tmt)[2], rownames(K2), sep = ":")
  K &lt;- rbind(K1, K2)
  colnames(K) &lt;- c(colnames(Tukey), colnames(Tukey))
  summary(glht(modintILAQ, linfct = K %*% X1))
  </code></pre>
  
  <p>So since I wanted to compare treatment levels within years, I tried exchanging the positions of the 2 variables in the code:</p>
  
  <pre><code>model2 &lt;- glm(resp ~ tmt * year, family=binomial,data = testsumm)
  summary(model2)
  temp2 &lt;- expand.grid(tmt = unique(testsumm$tmt),year =unique(testsumm$year))
  X12 &lt;- model.matrix(~ tmt * year, data = temp2)
  glht(model2, linfct = X12)
  Tukey &lt;- contrMat(table(testsumm$tmt), "Tukey")
      K1 &lt;- cbind(Tukey, matrix(0, nrow = nrow(Tukey), ncol = ncol(Tukey)))
      rownames(K1) &lt;- paste(levels(testsumm$year)[1], rownames(K1), sep = ":")
  K2 &lt;- cbind(matrix(0, nrow = nrow(Tukey), ncol = ncol(Tukey)), Tukey)
  rownames(K2) &lt;- paste(levels(testsumm$year)[2], rownames(K2), sep = ":")
  K &lt;- rbind(K1, K2)
  colnames(K) &lt;- c(colnames(Tukey), colnames(Tukey))
  summary(glht(model2, linfct = K %*% X1))
  </code></pre>
  
  <p>but I got this error message</p>
  
  <pre><code>    Error in K %*% X1 : non-conformable arguments
  </code></pre>
  
  <p>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?  </p>