0

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.

As I am using the plm to estimate the results, the broom package produce some nice results to package everything in a neat database. 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).

-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:

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"
      )
    )
  )
4

1 回答 1

1

使用来自@Gregor 的输入,我可以为我的问题创建一个令人满意的答案。

这是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")
于 2017-07-28T13:38:46.280 回答