1

I want to plot the marginal effects of my logit model (mod7) with cplot(), but I dont know how to solve this error

Here the Code:

mod7 <- glm(Crosssectionunique$ACQYes ~ Crosssectionunique$management + controls + Crosssectionunique$emp_firm)
mod8 <- glm(Crosssectionunique$ACQYes3 ~ Crosssectionunique$management + controls + Crosssectionunique$emp_firm)

summary(mod7)
install.packages("margins")
library("margins")
cplot(mod7, x = "Crosssectionunique$management", se.type = "shade")
Error in names(classes) <- clean_terms(names(classes)) : 
'names' attribute [3] must be the same length as the vector [2]

Any suggestions ?

1 Answers1

0

You get this issue because you try to pass "Crosssectionunique$management" to cplot which can be pretty confusing. So using an example dataset:

library(margins)

Crosssectionunique = data.frame(
ACQYes = runif(50),
management = sample(letters[1:3],50,replace=TRUE),
emp_firm = factor(sample(1:3,50,replace=TRUE))
)
controls = runif(50)

mod <- glm(Crosssectionunique$ACQYes ~ Crosssectionunique$management + controls + Crosssectionunique$emp_firm)

cplot(mod, x = "Crosssectionunique$management", se.type = "shade")

Error in names(classes) <- clean_terms(names(classes)) : 
  'names' attribute [5] must be the same length as the vector [3]

You can see I get the same error. When doing lm etc, try to use a data.frame and call the variables from the data.frame, for example:

Crosssectionunique$controls = controls
mod <- glm(ACQYes ~ management + controls + emp_firm,data=Crosssectionunique)

Now we call cplot, and specify the x should be a variable called "management", and it worksfine:

cplot(mod, x = "management", se.type = "shade")

enter image description here

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • ```Error in `[.data.frame`(data, , c(varslist$nnames, varslist$fnames), drop = FALSE) : undefined columns selected``` – Jonas Kiral Jun 05 '20 at 10:08
  • now this error occurs and I could not figure it out (I am ne to programming) – Jonas Kiral Jun 05 '20 at 10:08
  • I have just started to use the margins library and I am getting the exact same error as JonasKiral. I have used the model construction and variable names as suggested by @StupidWolf. Does anyone know of a solution to this? – user2888990 Mar 15 '22 at 23:59