Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save beta coefficients for each independent variable in "ols_all_subset()" #41

Closed
TimmyKarl opened this issue Oct 9, 2017 · 1 comment

Comments

Projects
None yet
2 participants
@TimmyKarl
Copy link

commented Oct 9, 2017

When running the 'ols_all_subset'-function, it would be nice to be able to save the beta-coefficients for each independent variable in each regression.

What I want to achieve is to be able to average the coefficients for every independent variable. So lets say I have 9 independent variables. In total, there will be 511 different regression models. Each variable was included in 256 models. I want to average these 256 beta-coefficients.

@aravindhebbali aravindhebbali self-assigned this Oct 10, 2017

aravindhebbali added a commit that referenced this issue Oct 10, 2017

@aravindhebbali

This comment has been minimized.

Copy link
Member

commented Oct 11, 2017

Hi @TimmyKarl , I have added a new function ols_all_subset_betas() which will return the coefficient for each independent variable from each regression.

library(olsrr)
library(dplyr)

# model
model <- lm(mpg ~ disp + hp + wt, data = mtcars)

# all possible regression 
ols_all_subset(model)
# A tibble: 7 x 6
  Index     N Predictors `R-Square` `Adj. R-Square` `Mallow's Cp`
  <int> <int>      <chr>      <chr>           <chr>         <chr>
1     1     1         wt    0.75283         0.74459      11.96609
2     2     1       disp    0.71834         0.70895      17.54291
3     3     1         hp    0.60244         0.58919      36.28451
4     4     2      hp wt    0.82679         0.81484       2.00820
5     5     2    disp wt    0.78093         0.76582       9.42277
6     6     2    disp hp    0.74824         0.73088      14.70870
7     7     3 disp hp wt    0.82684         0.80828       4.00000

# beta-coefficients for each independent variable in each regression
betas <- ols_all_subset_betas(model)
betas
# A tibble: 19 x 3
   model   predictor          beta
   <int>       <chr>         <dbl>
 1     1 (Intercept) 29.5998547562
 2     1        disp -0.0412151200
 3     2 (Intercept) 30.0988605396
 4     2          hp -0.0682282781
 5     3 (Intercept) 37.2851261673
 6     3          wt -5.3444715727
 7     4 (Intercept) 30.7359042483
 8     4        disp -0.0303462766
 9     4          hp -0.0248400812
10     5 (Intercept) 34.9605540362
11     5        disp -0.0177247443
12     5          wt -3.3508253333
13     6 (Intercept) 37.2272701164
14     6          hp -0.0317729470
15     6          wt -3.8778307424
16     7 (Intercept) 37.1055052690
17     7        disp -0.0009370091
18     7          hp -0.0311565508
19     7          wt -3.8008905826

#  average the coefficients for every independent variable
betas %>%
  select(predictor, beta) %>%
  group_by(predictor) %>%
  summarise_all(mean)
# A tibble: 4 x 2
    predictor        beta
        <chr>       <dbl>
1 (Intercept) 33.85901073
2        disp -0.02255579
3          hp -0.03899946
4          wt -4.09350456
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.