【问题标题】:Plot confidence interval for regression with clustered standard error in ggplot在 ggplot 中绘制具有聚集标准误差的回归的置信区间
【发布时间】:2019-07-31 21:20:39
【问题描述】:

我想在ggplot 中绘制一个置信区间的回归模型。在我的模型中,我想使用聚集在变量上的稳健标准误差。但是,我找不到可以找到聚类错误变量的位置。

我已经用lm_robust 方法尝试了geom_smooth 函数,但找不到在哪里添加集群错误变量。

p1 +geom_smooth(data=data,aes(y=y,x=x),method='lm_robust',se = T)

我需要ggplot 中以下行的等价物用于情节:

lm_robust(y~ x, data = data, clusters = z)

【问题讨论】:

  • 添加method.args = list(clusters = z)?
  • 我添加了method.args=list(clusters=z) 并返回了以下错误:“找不到对象“z””然后尝试method.args=list(clusters="z") 并收到此错误“stat_smooth() 中的计算失败:可变长度不同(找到(簇))"
  • 我不知道lm_robust来自什么包,所以我不确定通常如何使用clusters参数,我无法尝试。
  • 它来自estimatr 包。这里是其文档中手动使用的集群的示例:lmclust <- lm_robust(y ~ x + z, data = dat, clusters = clusterID)

标签: r ggplot2


【解决方案1】:

似乎非标准评估会带来问题,但您可以像这样传递整个 data.frame 列:

library(ggplot2)
library(estimatr)

ggplot(mtcars, aes(hp, qsec)) + 
  geom_smooth(method = 'lm_robust', method.args = list(cluster = mtcars$cyl))

请注意,在绘制多条线(例如使用颜色)或使用构面时,这将不起作用。

【讨论】:

  • 谢谢,但运行确切代码时出现以下错误“geom_smooth 中的错误(method = “lm_robust”,method.args = list(cluster = cyl)):object 'cyl' not找到”
猜你喜欢
  • 1970-01-01
  • 2021-12-25
  • 1970-01-01
  • 2013-05-06
  • 1970-01-01
  • 2019-06-12
  • 1970-01-01
  • 2014-02-19
  • 2017-04-01
相关资源
最近更新 更多