# File:   Regression.R
# Course: ENG7218: Health Data Science

# INSTALL AND LOAD PACKAGES ################################

library(datasets)  # Load base packages manually

# Installs pacman ("package manager") if needed
if (!require("pacman")) install.packages("pacman")
## Loading required package: pacman
## Warning: package 'pacman' was built under R version 4.3.3
# Use pacman to load add-on packages as desired
pacman::p_load(pacman, caret, lars, tidyverse)

# LOAD DATA ################################################

?USJudgeRatings
## starting httpd help server ...
##  done
head(USJudgeRatings)
##                CONT INTG DMNR DILG CFMG DECI PREP FAMI ORAL WRIT PHYS RTEN
## AARONSON,L.H.   5.7  7.9  7.7  7.3  7.1  7.4  7.1  7.1  7.1  7.0  8.3  7.8
## ALEXANDER,J.M.  6.8  8.9  8.8  8.5  7.8  8.1  8.0  8.0  7.8  7.9  8.5  8.7
## ARMENTANO,A.J.  7.2  8.1  7.8  7.8  7.5  7.6  7.5  7.5  7.3  7.4  7.9  7.8
## BERDON,R.I.     6.8  8.8  8.5  8.8  8.3  8.5  8.7  8.7  8.4  8.5  8.8  8.7
## BRACKEN,J.J.    7.3  6.4  4.3  6.5  6.0  6.2  5.7  5.7  5.1  5.3  5.5  4.8
## BURNS,E.B.      6.2  8.8  8.7  8.5  7.9  8.0  8.1  8.0  8.0  8.0  8.6  8.6
data <- USJudgeRatings

# Define variable groups
x <- as.matrix(data[, -12])
y <- data[, 12]

# REGRESSION WITH SIMULTANEOUS ENTRY #######################

# Using variable groups
reg1 <- lm(y ~ x)

# Or specify variables individually
reg1 <- lm(RTEN ~ CONT + INTG + DMNR + DILG + CFMG +
           DECI + PREP + FAMI + ORAL + WRIT + PHYS,
           data = USJudgeRatings)

# Results
reg1           # Coefficients only
## 
## Call:
## lm(formula = RTEN ~ CONT + INTG + DMNR + DILG + CFMG + DECI + 
##     PREP + FAMI + ORAL + WRIT + PHYS, data = USJudgeRatings)
## 
## Coefficients:
## (Intercept)         CONT         INTG         DMNR         DILG         CFMG  
##    -2.11943      0.01280      0.36484      0.12540      0.06669     -0.19453  
##        DECI         PREP         FAMI         ORAL         WRIT         PHYS  
##     0.27829     -0.00196     -0.13579      0.54782     -0.06806      0.26881
summary(reg1)  # Inferential tests
## 
## Call:
## lm(formula = RTEN ~ CONT + INTG + DMNR + DILG + CFMG + DECI + 
##     PREP + FAMI + ORAL + WRIT + PHYS, data = USJudgeRatings)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.22123 -0.06155 -0.01055  0.05045  0.26079 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -2.11943    0.51904  -4.083 0.000290 ***
## CONT         0.01280    0.02586   0.495 0.624272    
## INTG         0.36484    0.12936   2.820 0.008291 ** 
## DMNR         0.12540    0.08971   1.398 0.172102    
## DILG         0.06669    0.14303   0.466 0.644293    
## CFMG        -0.19453    0.14779  -1.316 0.197735    
## DECI         0.27829    0.13826   2.013 0.052883 .  
## PREP        -0.00196    0.24001  -0.008 0.993536    
## FAMI        -0.13579    0.26725  -0.508 0.614972    
## ORAL         0.54782    0.27725   1.976 0.057121 .  
## WRIT        -0.06806    0.31485  -0.216 0.830269    
## PHYS         0.26881    0.06213   4.326 0.000146 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1174 on 31 degrees of freedom
## Multiple R-squared:  0.9916, Adjusted R-squared:  0.9886 
## F-statistic: 332.9 on 11 and 31 DF,  p-value: < 2.2e-16
# MORE SUMMARIES ###########################################

anova(reg1)            # Coefficients w/inferential tests
## Analysis of Variance Table
## 
## Response: RTEN
##           Df Sum Sq Mean Sq   F value    Pr(>F)    
## CONT       1  0.058   0.058    4.1794 0.0494903 *  
## INTG       1 45.096  45.096 3270.7650 < 2.2e-16 ***
## DMNR       1  1.300   1.300   94.3167 6.415e-11 ***
## DILG       1  2.346   2.346  170.1567 3.963e-14 ***
## CFMG       1  0.503   0.503   36.5172 1.086e-06 ***
## DECI       1  0.214   0.214   15.5296 0.0004306 ***
## PREP       1  0.164   0.164   11.9069 0.0016353 ** 
## FAMI       1  0.039   0.039    2.7997 0.1043449    
## ORAL       1  0.439   0.439   31.8608 3.385e-06 ***
## WRIT       1  0.065   0.065    4.7078 0.0378096 *  
## PHYS       1  0.258   0.258   18.7170 0.0001464 ***
## Residuals 31  0.427   0.014                        
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
coef(reg1)             # Coefficients (same as reg1)
##  (Intercept)         CONT         INTG         DMNR         DILG         CFMG 
## -2.119429682  0.012796377  0.364840272  0.125399138  0.066690976 -0.194527027 
##         DECI         PREP         FAMI         ORAL         WRIT         PHYS 
##  0.278292932 -0.001960111 -0.135790972  0.547817680 -0.068061595  0.268811919
confint(reg1)          # CI for coefficients
##                    2.5 %      97.5 %
## (Intercept) -3.178010347 -1.06084902
## CONT        -0.039955335  0.06554809
## INTG         0.101011150  0.62866939
## DMNR        -0.057571651  0.30836993
## DILG        -0.225031708  0.35841366
## CFMG        -0.495940888  0.10688683
## DECI        -0.003683181  0.56026904
## PREP        -0.491456059  0.48753584
## FAMI        -0.680844080  0.40926214
## ORAL        -0.017628284  1.11326364
## WRIT        -0.710196975  0.57407378
## PHYS         0.142088434  0.39553540
resid(reg1)            # Residuals case-by-case
##   AARONSON,L.H.  ALEXANDER,J.M.  ARMENTANO,A.J.     BERDON,R.I.    BRACKEN,J.J. 
##    0.1674282950    0.1599043028    0.1318188003   -0.0721243488   -0.1663513584 
##      BURNS,E.B.   CALLAHAN,R.J.      COHEN,S.S.       DALY,J.J.    DANNEHY,J.F. 
##    0.0344455088   -0.1228672774   -0.0359845065   -0.0414643393    0.1054849167 
##       DEAN,H.H.     DEVITA,H.J.   DRISCOLL,P.J.     GRILLO,A.E.  HADDEN,W.L.JR. 
##    0.0315661299    0.0279048490   -0.0066302844    0.1215116258   -0.0707169455 
##     HAMILL,E.C.     HEALEY.A.H.       HULL,T.C.       LEVINE,I.   LEVISTER,R.L. 
##    0.0963751277    0.0966781231    0.0587324090    0.2607914304   -0.0613783951 
##     MARTIN,L.F.    MCGRATH,J.F.    MIGNONE,A.F.     MISSAL,H.M.     MULVEY,H.M. 
##   -0.0105476010   -0.0926140135   -0.0964022149   -0.0479617600    0.0279999236 
##      NARUK,H.J.    O'BRIEN,F.J. O'SULLIVAN,T.J.       PASKEY,L.    RUBINOW,J.E. 
##   -0.0633662511   -0.0142423076   -0.1918226956    0.0253091922   -0.0179725262 
##      SADEN.G.A. SATANIELLO,A.G.       SHEA,D.M.    SHEA,J.F.JR.      SIDOR,W.J. 
##   -0.0144131915    0.1145104470   -0.0617147925   -0.0608608820    0.0421019215 
##   SPEZIALE,J.A.     SPONZO,M.J.  STAPLETON,J.F.      TESTO,R.J. TIERNEY,W.L.JR. 
##    0.1474606096    0.0421784997   -0.2212325911   -0.0375263260   -0.0007537799 
##       WALL,R.A.     WRIGHT,D.B.   ZARRILLI,K.J. 
##   -0.0024277845   -0.1204656347   -0.0603603048
hist(residuals(reg1))  # Histogram of residuals

# ADDITIONAL MODELS ########################################

# Conventional stepwise regression
stepwise <- lars(x, y, type = "stepwise")

# Stagewise: Like stepwise but with better generalizability
forward <- lars(x, y, type = "forward.stagewise")

# LAR: Least Angle Regression
lar <- lars(x, y, type = "lar")

# LASSO: Least Absolute Shrinkage and Selection Operator
lasso <- lars(x, y, type = "lasso")

# Comparison of R^2 for new models
r2comp <- c(stepwise$R2[6], forward$R2[6], 
            lar$R2[6], lasso$R2[6]) %>% 
            round(2)
names(r2comp) <- c("stepwise", "forward", "lar", "lasso") 
r2comp  # Show values of R^2
## stepwise  forward      lar    lasso 
##     0.99     0.99     0.99     0.99
# CLEAN UP #################################################

# Clear environment
rm(list = ls()) 

# Clear packages
p_unload(all)  # Remove all add-ons
## The following packages have been unloaded:
## lubridate, forcats, stringr, dplyr, purrr, readr, tidyr, tibble, tidyverse, lars, caret, lattice, ggplot2, pacman
detach("package:datasets", unload = TRUE)  # For base

# Clear plots
dev.off()  # But only if there IS a plot
## null device 
##           1
# Clear console
cat("\014")  # ctrl+L
# Clear mind :)