【问题标题】:Visualise the relation between two variables in panel data可视化面板数据中两个变量之间的关系
【发布时间】:2021-01-02 08:17:32
【问题描述】:

我熟悉R,但不太熟悉绘图。我的面板数据如下:

library(plm)
library(dplyr)
data("EmplUK", package="plm")
EmplUK <- EmplUK %>%
group_by(firm, year) %>%
mutate(Vote = sample(c(0,1),1) ,
     Vote_won = ifelse(Vote==1, sample(c(0,1),1),0))

# EDIT: 

EmplUK <- pdata.frame(EmplUK , index=c("firm", "year"), drop.index = FALSE)

# A tibble: 1,031 x 9
# Groups:   firm, year [1,031]
    firm  year sector   emp  wage capital output  Vote Vote_won
   <dbl> <dbl>  <dbl> <dbl> <dbl>   <dbl>  <dbl> <dbl>    <dbl>
 1     1  1977      7  5.04  13.2   0.589   95.7     1        0
 2     1  1978      7  5.60  12.3   0.632   97.4     0        0
 3     1  1979      7  5.01  12.8   0.677   99.6     1        1
 4     1  1980      7  4.72  13.8   0.617  101.      1        1
 5     1  1981      7  4.09  14.3   0.508   99.6     0        0
 6     1  1982      7  3.17  14.9   0.423   98.6     0        0
 7     1  1983      7  2.94  13.8   0.392  100.      0        0
 8     2  1977      7 71.3   14.8  16.9     95.7     1        0
 9     2  1978      7 70.6   14.1  17.2     97.4     1        1
10     2  1979      7 70.9   15.0  17.5     99.6     1        1

toplot <- plm(output ~ wage, data=EmplUK, model="within")

Coefficients:
     Estimate Std. Error t-value   Pr(>|t|)    
wage   -0.707      0.143   -4.94 0.00000095 ***

我想通过可视化产出和工资之间的关系(也许拟合这种线性、二次、多项式)来评估面板数据中两个变量之间的最佳关系(线性、二次、多项式)。然而,我对绘图非常不熟悉。

我正在寻找这样的东西 (source)(我得到拟合线的公式):

我尝试如下开始:

plot(EmplUK$output,EmplUK$wage,type='l',col='red',main='Linear relationship')

但这给了我这个:

老实说,我几乎不知道自己在这里做什么。有没有人可以让我朝着正确的方向前进?

【问题讨论】:

    标签: r ggplot2 plot panel


    【解决方案1】:

    我可能会用贬义的数据来做。

    demeaned_data <- EmplUK %>% 
      group_by(firm) %>% 
      mutate(across(c(output, wage), function(x)x-mean(x)))
    
    ggplot(demeaned_data, aes(x=wage, y=output)) + 
      geom_point() + 
      geom_smooth(aes(colour="linear", fill="linear"), 
                  method="lm", 
                  formula=y ~ x, ) + 
      geom_smooth(aes(colour="quadratic", fill="quadratic"), 
                  method="lm", 
                  formula=y ~ x + I(x^2)) + 
      geom_smooth(aes(colour="cubic", fill="cubic"), 
                  method="lm", 
                  formula=y ~ x + I(x^2) + I(x^3)) + 
      scale_fill_brewer(palette="Set1") + 
      scale_colour_brewer(palette="Set1") + 
      theme_classic() + 
      labs(colour="Functional Form", fill="Functional Form")
    
    

    另一种方法是使用 OLS 和公司虚拟变量估计模型,然后您可以获得每个公司的预测并分别绘制它们。

    library(ggeffects)
    data("EmplUK", package="plm")
    EmplUK <- EmplUK %>% mutate(firm = as.factor(firm))
    m1 <- lm(output ~ wage + firm, data=EmplUK )
    m2 <- lm(output ~ wage + I(wage^2) + firm, data=EmplUK )
    m3 <- lm(output ~ wage + I(wage^2) + I(wage^3) + firm, data=EmplUK )
    
    p1 <- ggpredict(m1, terms=c("wage", "firm")) %>% 
      mutate(form="linear") %>% 
      rename("wage" = "x", 
             "firm" = "group", 
             "output" = "predicted")
    p2 <- ggpredict(m2, terms=c("wage", "firm")) %>% 
      mutate(form="quadratic") %>% 
      rename("wage" = "x", 
             "firm" = "group", 
             "output" = "predicted")
    p3 <- ggpredict(m3, terms=c("wage", "firm")) %>% 
      mutate(form="cubic") %>% 
      rename("wage" = "x", 
             "firm" = "group", 
             "output" = "predicted")
    
    ggplot() + 
      geom_line(data=p1, aes(x=wage, y=output, colour="linear")) + 
      geom_line(data=p2, aes(x=wage, y=output, colour="quadratic")) + 
      geom_line(data=p3, aes(x=wage, y=output, colour="cubic")) + 
      geom_point(data=EmplUK, aes(x=wage, y=output)) + 
      facet_wrap(~firm) + 
      theme_bw() + 
      labs(colour="Functional\nForm")
    
    

    【讨论】:

    • 哇,这真是太棒了。我最终也贬低了数据,但我不确定如何为其他关系建模。我会玩得很开心的!非常感谢!
    【解决方案2】:

    使用ggplot2 可能是这样的:

    library(ggplot2)
    
    ggplot(EmplUK, aes(output, wage)) + 
      geom_line(color = 'red') + 
      geom_smooth(size = 2) + 
      ggtitle('Linear relationship') + 
      theme_bw()
    

    【讨论】:

    • 非常感谢!那已经看起来好多了,哈哈。我有几个小问题..有没有办法解释它是面板数据的事实(至少从你的代码中我没有立即看到,但也许只是不明白)?此外,有没有办法拟合线性关系(并提取这些关系?)。很抱歉这些额外的问题,我只是对如何处理这些情节知之甚少。
    • 我看到我忘记将数据设为面板数据。现在,当我运行代码时,出现错误:geom_smooth() using method = 'gam' and formula 'y ~ s(x, bs = "cs")' Error in self$layout$PANEL == i : comparison of these types is not implemented In addition: Warning message: In [.data.frame(self$layout, self$layout$PANEL == i, ) : Incompatible methods ("Ops.factor", "Ops.pseries") for "=="
    • ggplot 更适用于数据帧/小标题,尝试将其转换为一个。 ggplot(data.frame(EmplUK), aes(output, wage)) + ....。从ggplot 代码中提取关系是很困难的。如果你想使用它,最好单独拟合这个模型。
    • 你也看到我的第一条评论了吗?关键是我想绘制面板关系。或者这只能通过创建一个完整的贬低数据集来实现?
    【解决方案3】:

    plm 有一个内置的plot 方法plm:::plot.plm,它也显示了固定的效果。对于多项式分析,您可以使用公司的loess 模型和colorize 的yhat。因此,将这两个图放在一起可以让您了解数据情况。

    EmplUK <- transform(EmplUK, yhat=predict(loess(output ~ wage)))
    
    op <- par(mfrow=c(1, 2), mar=c(4.5, 4, 3, 1))
    plot(toplot)  ## from `plm:::plot.plm`
    plot(output ~ wage, EmplUK, type="p", pch=20, cex=.5, col=firm, ylim=range(EmplUK$yhat))
    invisible(sapply(unique(EmplUK$firm), function(x)
           lines(yhat ~ wage, EmplUK[EmplUK$firm == x, ], col=x, lwd=1)))
    par(op)
    

    当然loess不能使用因子变量;在 Cross Validated 上,他们建议使用 Semiparametric Nonlinear Mixed Effects model 使用 nlme package 在混合模型上应用 LOESS。

    【讨论】:

    • 非常感谢!我实际上得到了第一个情节,但我真的不知道如何处理它(第二个情节看起来真的很有帮助!)。有没有办法让左边的情节更易读?还有什么方法可以从这些模型中提取建议的拟合?
    • @Tom 恐怕不行,因为cex= 选项不起作用,我相信它只是为快速诊断而设计的。但是你可以尝试使用step 逐步回归,类似于我在former answer 中所做的。但是,总的来说,我会避免过度使用数据驱动的方法,而是使用理论来考虑,例如为什么产出应该先下降,然后在工资上涨时再次上升。
    猜你喜欢
    • 2021-12-29
    • 2010-11-30
    • 2018-07-10
    • 2014-05-19
    • 2019-07-21
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多