##1. Loading Libraries

library(caret)
## Warning: package 'caret' was built under R version 4.3.3
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.3.3
## Loading required package: lattice
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(pROC)
## Warning: package 'pROC' was built under R version 4.3.3
## Type 'citation("pROC")' for a citation.
## 
## Attaching package: 'pROC'
## The following objects are masked from 'package:stats':
## 
##     cov, smooth, var
library(readxl)
## Warning: package 'readxl' was built under R version 4.3.3
library(car)
## Warning: package 'car' was built under R version 4.3.3
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.3.3
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode

##2. Setting random seed for reproducible results

set.seed(1234)

##3. Importing data The original dataset is saved into the dataframe called sdLDL_data

sdLDL_data <- read_excel("C:/Users/odokg/OneDrive/Desktop/UCH Ibadan Part-2/Data Analysis/Data Set for Software - R (2).xlsx") 

##4. Exploring data This file contains 170 rows. Each row corresponds to a different study participant. Outcome variable is High_sdLDL. The goal is predict sdLDL-C levels using certain input variables. There are various clinical and demographic characteristics associated with each study participant, as well as a unique numerical identifier (Study_No)

head(sdLDL_data)
## # A tibble: 6 × 29
##   Study_No Is_Test   Age Is_Male Is_Christian `Educational Status` `Weight (Kg)`
##      <dbl>   <dbl> <dbl>   <dbl>        <dbl>                <dbl>         <dbl>
## 1        1       1    55       1            1                    3           121
## 2        2       1    63       0            1                    3            62
## 3        3       1    46       0            0                    2            49
## 4        4       1    57       0            0                    1            66
## 5        5       1    56       0            1                    2            64
## 6        6       1    63       0            0                    3            68
## # ℹ 22 more variables: `Height (m)` <dbl>, BMI <dbl>, SBP <dbl>, DBP <dbl>,
## #   `family_history _htn` <dbl>, Duration_of_Diabetes_in_months <dbl>,
## #   Duration_of_Diabetes_years <dbl>, family_history_MI <dbl>,
## #   family_history_stroke <dbl>, family_history_sudden_death <dbl>,
## #   HbA1c <dbl>, TC <dbl>, HDL <dbl>, LDL <dbl>, TG <dbl>, sdLDL <dbl>,
## #   High_sdLDL <dbl>, Non_HDL <dbl>, TG_LDL_Ratio <dbl>, TG_HDL <dbl>,
## #   NonHDL_HDL <dbl>, LDL_HDL <dbl>
nrow(sdLDL_data) ##Shows number of rows in data frame
## [1] 170

##5. Extracting held-out test set Before training the model. A held-out validation test set is random sampled. The validation set will be 20% of the entire dataset and represents data the model hasn’t seen, while the remainder 80% will be used to train the model.

sample <- sample.int(n = nrow(sdLDL_data), size = nrow(sdLDL_data)*0.2, replace = F)
sdLDL_test <- sdLDL_data[sample, ] ##Yields test dataset that is the test percentage %

sdLDL_training <- sdLDL_data[-sample, ] ##Remainder of data is here

##6. Exploring Data-2 This is to confirm if the dataset is appropraitely segmented into 20% held-out test set and 80% training.

nrow(sdLDL_training)
## [1] 136
nrow(sdLDL_test)
## [1] 34

##7. Train MLR model The model is trained on the training dataset ###7a. Base Model (Bm1)

MLR_model_a <- glm( formula = sdLDL ~ Age + Is_Male + BMI + SBP + DBP, data=sdLDL_training, family="gaussian") 
summary(MLR_model_a) ##Outputs summary of model & coefficients
## 
## Call:
## glm(formula = sdLDL ~ Age + Is_Male + BMI + SBP + DBP, family = "gaussian", 
##     data = sdLDL_training)
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 36.39771   10.31615   3.528 0.000579 ***
## Age          0.10804    0.10914   0.990 0.324029    
## Is_Male     -2.91294    2.54594  -1.144 0.254662    
## BMI          0.57422    0.19252   2.983 0.003414 ** 
## SBP          0.01827    0.08899   0.205 0.837652    
## DBP          0.02770    0.12517   0.221 0.825218    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 133.9873)
## 
##     Null deviance: 19313  on 135  degrees of freedom
## Residual deviance: 17418  on 130  degrees of freedom
## AIC: 1059.9
## 
## Number of Fisher Scoring iterations: 2