【问题标题】:Error ploting Marginal Effects with cplot() in R在 R 中使用 cplot() 绘制边际效应时出错
【发布时间】:2020-09-21 01:35:23
【问题描述】:

我想用 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]

有什么建议吗?

【问题讨论】:

    标签: r plot effects marginal-effects


    【解决方案1】:

    您会遇到此问题,因为您尝试将 "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应该是一个名为“管理”的变量,它工作正常:

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

    【讨论】:

    • Error in `[.data.frame`(data, , c(varslist$nnames, varslist$fnames), drop = FALSE) : undefined columns selected
    • 现在这个错误发生了,我想不通(我不会编程)
    猜你喜欢
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-08
    • 2020-03-05
    • 2023-02-15
    • 2020-04-19
    • 1970-01-01
    相关资源
    最近更新 更多