Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

Extracting Coefficients, Std Errors, R2 etc from multiple regressions

$
0
0

I have the following regression model;

models <- lapply(1:25, function(x) lm(Y_df[,x] ~ X1))

Which runs 25 regressions on 25 columns in the Y_df dataframe.

One of the outputs can be shown as;

models[15] # Gives me the coefficients for model 15

Call:
lm(formula = Y_df[, x] ~ X1)

Coefficients:
(Intercept)         X1 
  0.1296812    1.0585835  

Which I can store in a separate df. The problem I am running into is regarding Std. Error, R2, residules etc.

I would like to store these also into a separate dataframe.

I can run individual regressions and extract the summaries as a normal R regression output would look like.

ls_1 <- summary(models[[1]])
ls_1
ls_1$sigma

However I am hoping to take the values directly from the line of code which runs the 25 regressions.

This code works

> (models[[15]]$coefficients)
  (Intercept)          X1 
-0.3643446787  1.0789369642

However; this code does not.

> (models[[15]]$sigma)
NULL

I have tried a variety of different combinations to try and extract these results with no luck.

The following did exactly what I wanted perfectly. I had hoped there was a way to replace the word coef with Std Error or R2 etc. but this does not work.

models <- lapply(1:25, function(x) lm(Y_df[,x] ~ X1))
# extract just coefficients
coefficients <- sapply(Y_df, coef)

Ideally I would like to store the Std Error from the above model


Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>