1

我想用 cplot() 绘制我的 logit 模型 (mod7) 的边际效应,但我不知道如何解决这个错误

这里的代码:

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]

有什么建议么 ?

4

1 回答 1

0

你得到这个问题是因为你试图传递"Crosssectionunique$management"给 cplot 这可能会很混乱。因此,使用示例数据集:

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]

你可以看到我得到了同样的错误。在做 lm 等时,尝试使用 data.frame 并从 data.frame 调用变量,例如:

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

现在我们调用 cplot,并指定 x 应该是一个名为“management”的变量,它工作正常:

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

在此处输入图像描述

于 2020-06-02T15:20:48.407 回答