简体   繁体   English

有没有办法使用扫帚捕捉系数和R平方?

[英]Is there a way to catch both Coefficient and R-squared using Broom?

I'm looping trough several regressions and aim to have a final result with different models, their respective coefficients and statistics, and some general results, as adjusted r squared, AIC and etc. This looping is done considering some subgroups in the database. 我通过一些回归进行循环,目的是使用不同的模型,其各自的系数和统计量以及一些常规结果(如调整后的r平方,AIC等)得出最终结果。此循环是考虑数据库中的某些子组完成的。

As I am using the plm to estimate the results, the broom package produce some nice results to package everything in a neat database. 当我使用plm估计结果时,broom软件包会产生一些不错的结果,以将所有内容打包到一个整洁的数据库中。 However, their options are kind of limiting. 但是,它们的选择是一种限制。 Or do you get the coefficients and their statistics (using tidy - provide p-values, t-statistics etc), or you get the overal model statistics (using glance - provide R-squared, adjusted R-squared, AIC etc). 或者,您是否获得系数及其统计信息(使用整洁的-提供p值,t统计等),或者您获得总体模型统计信息(使用一览表-提供R平方,调整后的R平方,AIC等)。

-Is there a way to have both data without recalculating the regression? -是否可以在不重新计算回归的情况下获得两个数据?

I know I can merge the final result, but this would involve double calculation of each regression, and this is computationally costly. 我知道我可以合并最终结果,但这将涉及到两次回归的双重计算,而且计算量大。 I know that the end result would repeat the aggregated statistics for each line of the coefficients, but I don't mind. 我知道最终结果将重复系数的每一行的汇总统计信息,但是我不介意。

-Also see that my code kind of repeat the estimations of each regression to provide robust estimations, does anyone know a workaround for this? -还看到我的代码有点重复每个回归的估计以提供可靠的估计,有人知道这种解决方法吗?

A MWE follows: MWE遵循:

library(dplyr)
library(broom)
library(plm)
library(lmtest)
library(magrittr)


data("Grunfeld")

#To generate coefficients by model
reg<- mutate(Grunfeld,
             group = ifelse(firm<6,1,2)) %>%
      group_by(., group) %>%
  do(
     tidy(
          coeftest(plm(as.formula(inv ~ value + capital)
                       ,data= .
                       ,model = "pooling"
                       )
                   ,vcov.= vcovHC(plm(as.formula(inv ~ value + capital)
                                      ,data= .
                                      ,model = "pooling"
                                      )
                                  ,method= "arellano"
                                  )
                  )
       )
  )

#To generate r-squared by model
reg<- mutate(Grunfeld,
             group = ifelse(firm<6,1,2)) %>%
  group_by(., group) %>%
  do(
    glance(
      plm(as.formula(inv ~ value + capital)
          ,data= .
          ,model = "pooling"
      )
    )
  )

Using the inputs from @Gregor I could create a satisfactory answer to my problem. 使用@Gregor的输入,我可以为我的问题创建令人满意的答案。

Here it is the MWE: 这是MWE:

library(dplyr)
library(broom)
library(plm)
library(lmtest)
library(magrittr)


data("Grunfeld")

plm_reg<- mutate(Grunfeld,
             group = ifelse(firm<6,1,2)) %>%
  group_by(., group) %>%
  do(reg=
      plm(as.formula(inv ~ value + capital)
          ,data= .
          ,model = "pooling"
      )
    )

robust_est <- function(x){
  return(tidy(coeftest(x, vcov.= vcovHC(x, method= "arellano"))))
}

robust_coef <- bind_rows(lapply(plm_reg[[2]], robust_est), .id = "group")
r_squared <-   bind_rows(lapply(plm_reg[[2]], glance), .id = "group")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM