【问题标题】:predicted probability plot with robust errors for logit modellogit 模型的具有稳健误差的预测概率图
【发布时间】:2018-03-07 21:06:35
【问题描述】:

我正在尝试使用聚集的稳健标准误差为 logit 模型制作预测概率图。假设 margins 包应该允许您使用 cplot() 执行此操作,但似乎存在一个错误,使得 cplot() 无法识别可选的 vcov 输入。下面是一个最小的工作示例。有谁知道如何修复错误或以其他方式执行此操作?

require("margins")
require("sandwich")

##Generating random numbers
set.seed(10)
y<-factor(rbinom(n=1000,size=1,prob=.5))
x <- rnorm(n=1000, mean=100,sd=1)
z<- rbinom(n=1000,size=3,prob=.5)
#creating a "dataset"
dta<-data.frame(x,y,z)

##Basic logit model
model <-glm(y~x,family="binomial"(link="logit"),data=dta)

##Creating variance-covariance matrix, clustered by z
vcov <- vcovCL(model, cluster=z)

##Making a plot
cplot(model,"x",vcov=vcov,what="prediction")

#can see below that vcov has no effect (if not obvious from plot)
print(cplot(model,"x",vcov=vcov,what="prediction",draw=FALSE))
print(cplot(model,"x",what="prediction",draw=FALSE))

【问题讨论】:

  • margins:::cplot.glm函数,vcov仅在what="effect"时使用,而不是what="prediction"
  • 嗯,这就解释了为什么它不起作用!谢谢。您是否碰巧知道这样做的另一种方法?
  • 老实说,我不知道你的情况到底是什么“这个”。如果您描述了您想要执行的计算(给出公式),我们也许可以将其转换为 R 代码。否则可以通过Cross Validated咨询统计学家。
  • 我所说的“这个”是指,我如何制作一个预测概率图来显示 logit 模型的稳健聚类标准误差。

标签: r


【解决方案1】:

您可以使用以下代码:

# Predict values
pred.dta <- ggeffects::ggpredict(
      model=model,
      terms="x [all]", 
      vcov.fun="vcovCL",
      vcov.type="HC1",
      vcov.args=list(cluster=z) 
      )    
# Plot predictions
ggplot2::ggplot(data=pred.dta, 
                ggplot2::aes(x=x, y=predicted))+
ggplot2::geom_line()+
ggplot2::geom_errorbar(ggplot2::aes(ymin=conf.low, ymax=conf.high), width=.1) 

为了比较,这是相同的代码,但没有聚集错误:

# Predict values
pred.dta <- ggeffects::ggpredict(
      model=model,
      terms="x [all]" )
      
# Plot predictions
ggplot2::ggplot(data=pred.dta, 
                ggplot2::aes(x=x, y=predicted))+
ggplot2::geom_line()+
ggplot2::geom_errorbar(ggplot2::aes(ymin=conf.low, ymax=conf.high), width=.1) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-14
    • 2012-10-26
    • 2012-06-18
    • 2022-01-07
    • 2015-02-06
    • 2012-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多