Search code examples
rregressionsummary

Summary stat from regression loop in r


I have a data frame (FF_port_monthly_MAX) like this

date          1       2       3    mkreturn     SML      HML
2/1/2007     2.42   -0.11   4.41    0.41        0.06     0.61
3/1/2007     4.13    2.36   6.92    0.03        0.99    -0.38
4/1/2007    -0.13   -3.20   1.99    0.24       -3.20    -5.64
5/1/2007    11.50    9.13   8.96    0.14        1.13    -2.58

I use this formula to regress the first three column with three independent variables :

fit <- lapply(FF_port_monthly_MAX[,2:4], function(x) lm(x ~ mkreturn + SML + HML, data = FF_port_monthly_MAX))

Now I like to extract coefficients and t stat by using this code

summary(fit)

But I end up an output like this

Length Class  Mode
1 12     lm    list
2 12     lm    list
3 12     lm    list

Now could anybody help me why it is not showing coefficients and t valuses?


Solution

  • You can also try a tidyverse approach

    library(tidyverse)
    FF_port_monthly_MAX %>% 
      select(starts_with("X"), mkreturn, SML, HML) %>% 
      gather(key, value, -mkreturn, -SML, -HML) %>% 
      split(.$key) %>% 
       map(~lm(formula = value ~  mkreturn + SML + HML, data=.)) %>% 
       map(~broom::tidy(.)) %>% 
       bind_rows(.id = "run")
    # A tibble: 12 x 6
       run   term        estimate std.error statistic p.value
       <chr> <chr>          <dbl>     <dbl>     <dbl>   <dbl>
     1 X1    (Intercept)   -1.81        NaN       NaN     NaN
     2 X1    mkreturn      13.2         NaN       NaN     NaN
     3 X1    SML            4.68        NaN       NaN     NaN
     4 X1    HML           -2.39        NaN       NaN     NaN
     5 X2    (Intercept)   -3.45        NaN       NaN     NaN
     6 X2    mkreturn      10.8         NaN       NaN     NaN
     7 X2    SML            4.68        NaN       NaN     NaN
     8 X2    HML           -2.24        NaN       NaN     NaN
     9 X3    (Intercept)    4.47        NaN       NaN     NaN
    10 X3    mkreturn       0.653       NaN       NaN     NaN
    11 X3    SML            2.16        NaN       NaN     NaN
    12 X3    HML           -0.757       NaN       NaN     NaN