简体   繁体   中英

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.

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

Using the inputs from @Gregor I could create a satisfactory answer to my problem.

Here it is the 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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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