#ccar for continuous variables ccar<-mtcars[,c("mpg","disp","hp","drat","wt")] attach(ccar) hp2<-hp^2 wt2<-wt^2 # We have seen in our previous analysis the these two squared terms give better disgnostics. model <- lm(mpg ~ disp + hp + wt + drat+ hp2+ wt2) install.packages("olsrr") library(olsrr) allm<-ols_step_all_possible(model) cp<-allm[["result"]]$cp-allm[["result"]]$n mcp<-min(cp) which(cp==mcp) adjr<-allm[["result"]]$adjr madjr=max(adjr) which(adjr==madjr) aic<-allm[["result"]]$aic maic<-min(aic) which(aic==maic) sbc<-allm[["result"]]$sbc msbc<-min(sbc) which(sbc==msbc) # Seems like we have obtained the the Answer to the Ultimate # Question of Life, the Universe, and Everything. best<-ols_step_best_subset(model) best[["metrics"]][,c("predictors","adjr","cp","aic","sbc")] plot(best[["metrics"]]$n,best[["metrics"]]$cp) abline(0,1) forw<-ols_step_forward_aic(model) forw$model$terms[[3]] back<-ols_step_backward_aic(model) back$model$terms[[3]] plot(forw) ## Partial correlation ols_correlations(model) mpgr1<-lm(mpg~wt+hp+drat+hp2+wt2)$residuals disr1<-lm(disp~wt+hp+drat+hp2+wt2)$residuals cor(mpgr1, disr1) #partial cor(mpg, disr1) #part