I am trying to plot interaction effects in R for a negative binomial regression model (glm.nb). The dependent variable meetings is numeric. Variable EU is dichotomous and variable "type" is categorical 1/2/3.
glmnbmodel <- glm.nb(meetings ~ EU + type + EU*type, data = data)
I tried plotting using this command: interplot(m = glm.nb, var1 = "meetings", var2 = "type")
Which gives this error:
> interplot(m = glmnbmodel, var1 = "EU", var2 = "type")
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘sim’ for signature ‘"negbin"’
I tried the same with a glm model instead, wherein R does not recognize the interaction in the model:
> glmmodel <- glm(meetings ~ EU + type + EU*type, data = data)
> interplot(m = glmmodel, var1 = "EU", var2 = "type")
Error in interplot.default(m = glmmodel, var1 = "EU", var2 = "type") :
Model does not include the interaction of EU and type .
What am I doing wrong?
I have been stuck on this for quite some time, so any help is most appreciated!
You are not specifying the formula correctly. ~EU*type
would mean ~ EU + type + EU:type
and R will correct this, but for the package interplot
most likely it is looking for the interaction term, so you can do:
set.seed(111)
data = data.frame(meetings = rnbinom(100,mu=10,size=1),
EU=rbinom(100,1,0.5),
type = factor(sample(1:3,100,replace=TRUE)))
glmmodel <- glm(meetings ~ EU + type + EU:type, data = data,family="poisson")
interplot(m = glmmodel, var1 = "EU", var2 = "type")
For negative binomial, you can fit a model first to get the theta estimate and call it within glm
like this:
fit <- glm.nb(meetings ~ EU + type + EU:type, data = data)
glmnbmodel <- glm(meetings ~ EU + type + EU:type, data = data,
family = negative.binomial(fit$theta))
interplot(m = glmnbmodel, var1 = "EU", var2 = "type")
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.