Creating a regression loop for a model and store results

Hi all,

I am have the following sample dataset that contains Mutual Funds risk premium and the Treynor-Mazuy factors mrp, and mrp2.

I am trying to perform the following regression:

lm(formula= "`SIEQWEE AV Equity-rf` ~ mrp + mrp2", data= bl ,na.action = na.omit)

Here is my approache for the loop of the regressions:
depVarList = setdiff(colnames(bl), c("Date", "rf", "Rm", "mrp", "mrp2"))

#Loop over them and create model for each
allModels = lapply(depVarList, function(x){
lm(formula=paste0("", x, " ~ mrp + mrp2"),
data=bl, na.action = na.omit)
})

Once the regression is completed, I need to have the following information:

  • The mean of:
    Intercept
    Intercept P-value
    β1
    β1 P-value
    β2
    β2 P-value
  • Number of funds that:
    rejects intercept=0 at 5%
    rejects β2=0 at 5%
    Intercept > 0
    β2 > 0

How can I extract this information?

Thank you very much in advance!

See the FAQ: How to do a minimal reproducible example reprex for beginners to get a more directed answer.

To begin, replace column names with labels that do not include blanks or other special characters. The time for long, descriptive labels is when preparing presentation tables.

The example below illustrates the process for extracting the mean of intercept terms of several models.

fit <- lm(mpg ~ hp + drat, data = mtcars)
summary(fit)
#> 
#> Call:
#> lm(formula = mpg ~ hp + drat, data = mtcars)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -5.0369 -2.3487 -0.6034  1.1897  7.7500 
#> 
#> Coefficients:
#>              Estimate Std. Error t value Pr(>|t|)    
#> (Intercept) 10.789861   5.077752   2.125 0.042238 *  
#> hp          -0.051787   0.009293  -5.573 5.17e-06 ***
#> drat         4.698158   1.191633   3.943 0.000467 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 3.17 on 29 degrees of freedom
#> Multiple R-squared:  0.7412, Adjusted R-squared:  0.7233 
#> F-statistic: 41.52 on 2 and 29 DF,  p-value: 3.081e-09
str(summary(fit))
#> List of 11
#>  $ call         : language lm(formula = mpg ~ hp + drat, data = mtcars)
#>  $ terms        :Classes 'terms', 'formula'  language mpg ~ hp + drat
#>   .. ..- attr(*, "variables")= language list(mpg, hp, drat)
#>   .. ..- attr(*, "factors")= int [1:3, 1:2] 0 1 0 0 0 1
#>   .. .. ..- attr(*, "dimnames")=List of 2
#>   .. .. .. ..$ : chr [1:3] "mpg" "hp" "drat"
#>   .. .. .. ..$ : chr [1:2] "hp" "drat"
#>   .. ..- attr(*, "term.labels")= chr [1:2] "hp" "drat"
#>   .. ..- attr(*, "order")= int [1:2] 1 1
#>   .. ..- attr(*, "intercept")= int 1
#>   .. ..- attr(*, "response")= int 1
#>   .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
#>   .. ..- attr(*, "predvars")= language list(mpg, hp, drat)
#>   .. ..- attr(*, "dataClasses")= Named chr [1:3] "numeric" "numeric" "numeric"
#>   .. .. ..- attr(*, "names")= chr [1:3] "mpg" "hp" "drat"
#>  $ residuals    : Named num [1:32] -2.42 -2.42 -1.26 1.84 2.17 ...
#>   ..- attr(*, "names")= chr [1:32] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" "Hornet 4 Drive" ...
#>  $ coefficients : num [1:3, 1:4] 10.78986 -0.05179 4.69816 5.07775 0.00929 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:3] "(Intercept)" "hp" "drat"
#>   .. ..$ : chr [1:4] "Estimate" "Std. Error" "t value" "Pr(>|t|)"
#>  $ aliased      : Named logi [1:3] FALSE FALSE FALSE
#>   ..- attr(*, "names")= chr [1:3] "(Intercept)" "hp" "drat"
#>  $ sigma        : num 3.17
#>  $ df           : int [1:3] 3 29 3
#>  $ r.squared    : num 0.741
#>  $ adj.r.squared: num 0.723
#>  $ fstatistic   : Named num [1:3] 41.5 2 29
#>   ..- attr(*, "names")= chr [1:3] "value" "numdf" "dendf"
#>  $ cov.unscaled : num [1:3, 1:3] 2.57 -3.04e-03 -5.81e-01 -3.04e-03 8.59e-06 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:3] "(Intercept)" "hp" "drat"
#>   .. ..$ : chr [1:3] "(Intercept)" "hp" "drat"
#>  - attr(*, "class")= chr "summary.lm"
coef(fit)
#> (Intercept)          hp        drat 
#> 10.78986122 -0.05178665  4.69815776
coef(fit)[1]
#> (Intercept) 
#>    10.78986

get_intercept <- function(x) coef(x)[[1]]
get_intercept(fit)
#> [1] 10.78986

make_fit <- function(x) lm(mpg ~ mtcars[,x], data = mtcars)

get_intercept(make_fit(4))
#> [1] 30.09886

do_both <- function(x) get_intercept(make_fit(x))

the_intercepts <- Map(do_both,2:11)
mean(unlist(the_intercepts))
#> [1] 18.74895

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.