【问题标题】:Plotting Panel data Mixed Effect model with Random and Fixed models使用随机模型和固定模型绘制面板数据混合效应模型
【发布时间】:2020-03-18 07:21:30
【问题描述】:

我正在研究面板数据模型,我现在正在使用来自lme4 包的混合模型,我还使用了基于随机、固定、LSDV、Fisrt_diff 等的模型...

我有一个绘制所有模型系数的函数。在 ggplot 中,但是从 lme4 绘制系数是一个问题,我可以让它工作:

有没有办法让下面的代码适用于所有模型,包括模型mixed

library(plm)
library(lme4)
library(ggplot2)

mixed <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
fixed = plm(Reaction ~ Days, data = sleepstudy, index = c("Subject", "Days"), model = "within")
random = plm(Reaction ~ Days, data = sleepstudy, index = c("Subject", "Days"), model = "random")
pool = plm(Reaction ~ Days, data = sleepstudy, index = c("Subject", "Days"), model = "pooling")
first_diff = plm(Reaction ~ Days, data = sleepstudy, index = c("Subject", "Days"), model = "fd")
# Function to extract point estimates
ce <- function(model.obj) {
  extract <- summary(get(model.obj))$coefficients[2:nrow(summary(get(model.obj))$coefficients), 1:2]
  return(data.frame(extract, vars = row.names(extract), model = model.obj))
}

# Run function on the three models and bind into single data frame
coefs <- do.call(rbind, sapply(paste0(list(
  "fixed", "random", "pool", "first_diff"
)), ce, simplify = FALSE))

names(coefs)[2] <- "se"

gg_coef <- ggplot(coefs, aes(vars, Estimate)) +
  geom_hline(yintercept = 0, lty = 1, lwd = 0.5, colour = "red") +
  geom_errorbar(aes(ymin = Estimate - se, ymax = Estimate + se, colour = vars),
                lwd = 1, width = 0
  ) +
  geom_point(size = 3, aes(colour = vars)) +
  facet_grid(model ~ ., scales="free") +
  coord_flip() +
  guides(colour = FALSE) +
  labs(x = "Coefficient", y = "Value") +
  ggtitle("Raw models coefficients")

gg_coef

【问题讨论】:

    标签: r ggplot2 time-series rstudio panel-data


    【解决方案1】:

    当前代码的错误在于

    data(sleepstudy)
    mixed <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
    coefficients(summary(mixed))
                 Estimate Std. Error   t value
    (Intercept) 251.40510   6.823773 36.842535
    Days         10.46729   1.545958  6.770744
    

    Days 在 sleepstudy 数据集中是数字,并使用连续预测器。使用你的 ce 函数,这会返回一个错误,因为行名被删除,使用 2:nrow(..)。

    要获得与其他模型相似的估计值,请将天数设置为因子,将随机效应设置为 (1|天)。我认为(Days | Subject)没有意义。

    sleepstudy$Days = factor(sleepstudy$Days)
    mixed <- lmer(Reaction ~ Days + (1 | Subject), sleepstudy)
    

    我们会稍微修改您的 ce 代码,使用 drop=FALSE,以防止出现空的 row.names

    ce <- function(model.obj) {
      summ.model <- summary(get(model.obj))$coefficients
      extract <- summ.model[2:nrow(summ.model),drop=FALSE, 1:2]
      return(data.frame(extract, vars = row.names(extract), model = model.obj))
    }
    
    coefs <- do.call(rbind, sapply(paste0(list(
      "fixed", "random", "pool", "first_diff","mixed"
    )), ce, simplify = FALSE))
    
    names(coefs)[2] <- "se"
    

    运行剩下的:

    gg_coef <- ggplot(coefs, aes(vars, Estimate)) +
      geom_hline(yintercept = 0, lty = 1, lwd = 0.5, colour = "red") +
      geom_errorbar(aes(ymin = Estimate - se, ymax = Estimate + se, colour = vars),
                    lwd = 1, width = 0
      ) +
      geom_point(size = 3, aes(colour = vars)) +
      facet_grid(model ~ ., scales="free") +
      coord_flip() +
      guides(colour = FALSE) +
      labs(x = "Coefficient", y = "Value") +
      ggtitle("Raw models coefficients")
    
    gg_coef
    

    【讨论】:

    • 非常感谢,我同意上面的模型没有意义,它只是原始数据集和函数模型 - 另外你能推荐我一些关于面板数据混合效果的阅读吗?我仍然有一些问题完全理解它
    • 面板数据模型我用的不多。我正在阅读这篇关于交叉验证的帖子,stats.stackexchange.com/questions/34642/…,所以我认为您看到了固定效应估计值的差异。
    猜你喜欢
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 2015-09-26
    • 2022-11-27
    • 1970-01-01
    • 2018-05-10
    相关资源
    最近更新 更多