#Stepwise example, first survey data

Wk11c <- read_sav("first_survey.sav")
names(Wk11c)
##  [1] "Gender"    "Health"    "Height"    "Weight"    "Aerobic"   "Strength" 
## [7] "Stretch" "Moderate" "Other" "Run" "Political" "Exercise"
## [13] "PRE_1" "RES_1" "PRE_2" "RES_2"
#First build a data set with just the needed variables and omit missing cases

Wk11c_nomiss<-na.omit(Wk11c[,c(1:11)])

#Now run a model that includes all possible predictors

steptotal<-lm(Health ~ Gender+Height+Weight+Aerobic+Strength+Stretch+
Moderate+Other+Run+Political, Wk11c_nomiss)


#install.packages('olsrr')
library(olsrr)
## Warning: package 'olsrr' was built under R version 4.0.3
## 
## Attaching package: 'olsrr'
## The following object is masked from 'package:datasets':
##
## rivers
ols_step_both_p(steptotal, pent = 0.10, prem = 0.06, details = TRUE)
## Stepwise Selection Method   
## ---------------------------
##
## Candidate Terms:
##
## 1. Gender
## 2. Height
## 3. Weight
## 4. Aerobic
## 5. Strength
## 6. Stretch
## 7. Moderate
## 8. Other
## 9. Run
## 10. Political
##
## We are selecting variables based on p value...
##
##
## Stepwise Selection: Step 1
##
## - Run added
##
## Model Summary
## --------------------------------------------------------------
## R 0.794 RMSE 0.660
## R-Squared 0.631 Coef. Var 30.515
## Adj. R-Squared 0.618 MSE 0.435
## Pred R-Squared 0.575 MAE 0.545
## --------------------------------------------------------------
## RMSE: Root Mean Square Error
## MSE: Mean Square Error
## MAE: Mean Absolute Error
##
## ANOVA
## -------------------------------------------------------------------
## Sum of
## Squares DF Mean Square F Sig.
## -------------------------------------------------------------------
## Regression 21.580 1 21.580 49.614 0.0000
## Residual 12.614 29 0.435
## Total 34.194 30
## -------------------------------------------------------------------
##
## Parameter Estimates
## ----------------------------------------------------------------------------------------
## model Beta Std. Error Std. Beta t Sig lower upper
## ----------------------------------------------------------------------------------------
## (Intercept) 5.818 0.533 10.926 0.000 4.729 6.907
## Run -0.659 0.094 -0.794 -7.044 0.000 -0.850 -0.468
## ----------------------------------------------------------------------------------------
##
##
##
## Stepwise Selection: Step 2
##
## - Political added
##
## Model Summary
## --------------------------------------------------------------
## R 0.832 RMSE 0.612
## R-Squared 0.693 Coef. Var 28.332
## Adj. R-Squared 0.671 MSE 0.375
## Pred R-Squared 0.630 MAE 0.481
## --------------------------------------------------------------
## RMSE: Root Mean Square Error
## MSE: Mean Square Error
## MAE: Mean Absolute Error
##
## ANOVA
## -------------------------------------------------------------------
## Sum of
## Squares DF Mean Square F Sig.
## -------------------------------------------------------------------
## Regression 23.695 2 11.847 31.597 0.0000
## Residual 10.499 28 0.375
## Total 34.194 30
## -------------------------------------------------------------------
##
## Parameter Estimates
## ----------------------------------------------------------------------------------------
## model Beta Std. Error Std. Beta t Sig lower upper
## ----------------------------------------------------------------------------------------
## (Intercept) 6.102 0.509 11.996 0.000 5.060 7.143
## Run -0.620 0.088 -0.748 -7.017 0.000 -0.801 -0.439
## Political -0.297 0.125 -0.253 -2.375 0.025 -0.554 -0.041
## ----------------------------------------------------------------------------------------
##
##
##
## Model Summary
## --------------------------------------------------------------
## R 0.832 RMSE 0.612
## R-Squared 0.693 Coef. Var 28.332
## Adj. R-Squared 0.671 MSE 0.375
## Pred R-Squared 0.630 MAE 0.481
## --------------------------------------------------------------
## RMSE: Root Mean Square Error
## MSE: Mean Square Error
## MAE: Mean Absolute Error
##
## ANOVA
## -------------------------------------------------------------------
## Sum of
## Squares DF Mean Square F Sig.
## -------------------------------------------------------------------
## Regression 23.695 2 11.847 31.597 0.0000
## Residual 10.499 28 0.375
## Total 34.194 30
## -------------------------------------------------------------------
##
## Parameter Estimates
## ----------------------------------------------------------------------------------------
## model Beta Std. Error Std. Beta t Sig lower upper
## ----------------------------------------------------------------------------------------
## (Intercept) 6.102 0.509 11.996 0.000 5.060 7.143
## Run -0.620 0.088 -0.748 -7.017 0.000 -0.801 -0.439
## Political -0.297 0.125 -0.253 -2.375 0.025 -0.554 -0.041
## ----------------------------------------------------------------------------------------
##
##
##
## Stepwise Selection: Step 3
##
## - Strength added
##
## Model Summary
## --------------------------------------------------------------
## R 0.864 RMSE 0.567
## R-Squared 0.746 Coef. Var 26.230
## Adj. R-Squared 0.718 MSE 0.321
## Pred R-Squared 0.669 MAE 0.453
## --------------------------------------------------------------
## RMSE: Root Mean Square Error
## MSE: Mean Square Error
## MAE: Mean Absolute Error
##
## ANOVA
## -------------------------------------------------------------------
## Sum of
## Squares DF Mean Square F Sig.
## -------------------------------------------------------------------
## Regression 25.516 3 8.505 26.466 0.0000
## Residual 8.677 27 0.321
## Total 34.194 30
## -------------------------------------------------------------------
##
## Parameter Estimates
## ----------------------------------------------------------------------------------------
## model Beta Std. Error Std. Beta t Sig lower upper
## ----------------------------------------------------------------------------------------
## (Intercept) 6.082 0.471 12.914 0.000 5.115 7.048
## Run -0.570 0.085 -0.687 -6.735 0.000 -0.743 -0.396
## Political -0.334 0.117 -0.284 -2.857 0.008 -0.574 -0.094
## Strength -0.516 0.217 -0.239 -2.381 0.025 -0.961 -0.071
## ----------------------------------------------------------------------------------------
##
##
##
## Model Summary
## --------------------------------------------------------------
## R 0.864 RMSE 0.567
## R-Squared 0.746 Coef. Var 26.230
## Adj. R-Squared 0.718 MSE 0.321
## Pred R-Squared 0.669 MAE 0.453
## --------------------------------------------------------------
## RMSE: Root Mean Square Error
## MSE: Mean Square Error
## MAE: Mean Absolute Error
##
## ANOVA
## -------------------------------------------------------------------
## Sum of
## Squares DF Mean Square F Sig.
## -------------------------------------------------------------------
## Regression 25.516 3 8.505 26.466 0.0000
## Residual 8.677 27 0.321
## Total 34.194 30
## -------------------------------------------------------------------
##
## Parameter Estimates
## ----------------------------------------------------------------------------------------
## model Beta Std. Error Std. Beta t Sig lower upper
## ----------------------------------------------------------------------------------------
## (Intercept) 6.082 0.471 12.914 0.000 5.115 7.048
## Run -0.570 0.085 -0.687 -6.735 0.000 -0.743 -0.396
## Political -0.334 0.117 -0.284 -2.857 0.008 -0.574 -0.094
## Strength -0.516 0.217 -0.239 -2.381 0.025 -0.961 -0.071
## ----------------------------------------------------------------------------------------
##
##
##
## No more variables to be added/removed.
##
##
## Final Model Output
## ------------------
##
## Model Summary
## --------------------------------------------------------------
## R 0.864 RMSE 0.567
## R-Squared 0.746 Coef. Var 26.230
## Adj. R-Squared 0.718 MSE 0.321
## Pred R-Squared 0.669 MAE 0.453
## --------------------------------------------------------------
## RMSE: Root Mean Square Error
## MSE: Mean Square Error
## MAE: Mean Absolute Error
##
## ANOVA
## -------------------------------------------------------------------
## Sum of
## Squares DF Mean Square F Sig.
## -------------------------------------------------------------------
## Regression 25.516 3 8.505 26.466 0.0000
## Residual 8.677 27 0.321
## Total 34.194 30
## -------------------------------------------------------------------
##
## Parameter Estimates
## ----------------------------------------------------------------------------------------
## model Beta Std. Error Std. Beta t Sig lower upper
## ----------------------------------------------------------------------------------------
## (Intercept) 6.082 0.471 12.914 0.000 5.115 7.048
## Run -0.570 0.085 -0.687 -6.735 0.000 -0.743 -0.396
## Political -0.334 0.117 -0.284 -2.857 0.008 -0.574 -0.094
## Strength -0.516 0.217 -0.239 -2.381 0.025 -0.961 -0.071
## ----------------------------------------------------------------------------------------
## 
## Stepwise Selection Summary
## -------------------------------------------------------------------------------------
## Added/ Adj.
## Step Variable Removed R-Square R-Square C(p) AIC RMSE
## -------------------------------------------------------------------------------------
## 1 Run addition 0.631 0.618 15.5340 66.0987 0.6595
## 2 Political addition 0.693 0.671 10.4030 62.4097 0.6123
## 3 Strength addition 0.746 0.718 6.2600 58.5022 0.5669
## -------------------------------------------------------------------------------------
#Now, with the olsrr package, an easy route to part and partial correlation

ols_correlations(steptotal)
##                 Correlations                 
## --------------------------------------------
## Variable Zero Order Partial Part
## --------------------------------------------
## Gender 0.301 0.071 0.029
## Height -0.119 0.445 0.207
## Weight -0.078 -0.102 -0.043
## Aerobic -0.492 -0.300 -0.131
## Strength -0.374 -0.450 -0.210
## Stretch -0.255 0.292 0.127
## Moderate -0.091 -0.216 -0.092
## Other -0.258 -0.248 -0.107
## Run -0.794 -0.817 -0.589
## Political -0.391 -0.221 -0.094
## --------------------------------------------