The summary
function is not the best method to get post-hoc results. It is better to use something made for the task, like the emmeans
package.
The following is a toy example. It uses the glm.nb
function from the MASS
package. MASS::glm.nb
is supported by emmeans
. I don't know if pscl::glm.nb
would work as well.
The model in this example throws some errors. I'm ignoring them for this example.
To get an anova table you can use the anova
function. Instead, I'm using car::Anova
here. glm.nb
isn't explicitly supported by car::Anova
, but it appears to work okay.
Note that with emmeans
you can compare treatments for a main effect or an interaction effect from the model. You can get estimates and p-values for individual contrasts (pairs
) or have the results displayed as a compact letter display (cld
).
if(!require(MASS)){install.packages("MASS")}
if(!require(emmeans)){install.packages("emmeans")}
if(!require(car)){install.packages("car")}
State = c(rep("state1",10), rep("state2",10), rep("state3",10), rep("state4",10))
Year = rep(c("year1","year2"),20)
Value = c(1,3,3,5,9,11,2,7,4,5,6,7,8,9,9,10,7,8,8,10,
3,2,3,2,4,2, 5,3,5,3,8,6,8,4,9,4, 8,5,8, 5)
Data = data.frame(State, Year, Value)
library(MASS)
model = glm.nb(Value ~ Year + State + Year:State, data = Data)
# anova(model)
library(car)
Anova(model)
library(emmeans)
marginal = emmeans(model, ~ State)
pairs(marginal)
### contrast estimate SE df z.ratio p.value
### state1 - state2 -0.5216748 0.1829872 Inf -2.851 0.0226
### state1 - state3 0.4488936 0.2335741 Inf 1.922 0.2188
### state1 - state4 -0.2565999 0.1942621 Inf -1.321 0.5495
### state2 - state3 0.9705684 0.2135304 Inf 4.545 <.0001
### state2 - state4 0.2650749 0.1696353 Inf 1.563 0.4002
### state3 - state4 -0.7054935 0.2232682 Inf -3.160 0.0086
###
### Results are averaged over the levels of: Year
### Results are given on the log (not the response) scale.
### P value adjustment: tukey method for comparing a family of 4 estimates
cld(marginal, Letters=letters)
### State emmean SE df asymp.LCL asymp.UCL .group
### state3 1.130882 0.1825757 Inf 0.7730397 1.488723 a
### state1 1.579775 0.1456811 Inf 1.2942455 1.865305 ab
### state4 1.836375 0.1285099 Inf 1.5845002 2.088250 bc
### state2 2.101450 0.1107309 Inf 1.8844214 2.318479 c
### Results are averaged over the levels of: Year
### Results are given on the log (not the response) scale.
### Confidence level used: 0.95
### P value adjustment: tukey method for comparing a family of 4 estimates
### significance level used: alpha = 0.05
marginal = emmeans(model, ~ State:Year)
# pairs(marginal)
cld(marginal, Letters=letters)