#loading libraries

## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
## Type 'citation("pROC")' for a citation.
## 
## Attaching package: 'pROC'
## The following objects are masked from 'package:stats':
## 
##     cov, smooth, var
## 
## Attaching package: 'dlookr'
## The following object is masked from 'package:tidyr':
## 
##     extract
## The following object is masked from 'package:base':
## 
##     transform
## 
## Attaching package: 'naniar'
## The following object is masked from 'package:skimr':
## 
##     n_complete

R Markdown

This is an R Markdown presentation. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.

Slide with Bullets

Slide with R Output

## Rows: 4238 Columns: 16
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## dbl (16): male, age, education, currentSmoker, cigsPerDay, BPMeds, prevalent...
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
Data summary
Name framingham
Number of rows 4238
Number of columns 16
_______________________
Column type frequency:
numeric 16
________________________
Group variables None

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
male 0 1.00 0.43 0.50 0.00 0.00 0.0 1.00 1.0 <U+2587><U+2581><U+2581><U+2581><U+2586>
age 0 1.00 49.58 8.57 32.00 42.00 49.0 56.00 70.0 <U+2583><U+2587><U+2586><U+2586><U+2582>
education 105 0.98 1.98 1.02 1.00 1.00 2.0 3.00 4.0 <U+2587><U+2586><U+2581><U+2583><U+2582>
currentSmoker 0 1.00 0.49 0.50 0.00 0.00 0.0 1.00 1.0 <U+2587><U+2581><U+2581><U+2581><U+2587>
cigsPerDay 29 0.99 9.00 11.92 0.00 0.00 0.0 20.00 70.0 <U+2587><U+2583><U+2581><U+2581><U+2581>
BPMeds 53 0.99 0.03 0.17 0.00 0.00 0.0 0.00 1.0 <U+2587><U+2581><U+2581><U+2581><U+2581>
prevalentStroke 0 1.00 0.01 0.08 0.00 0.00 0.0 0.00 1.0 <U+2587><U+2581><U+2581><U+2581><U+2581>
prevalentHyp 0 1.00 0.31 0.46 0.00 0.00 0.0 1.00 1.0 <U+2587><U+2581><U+2581><U+2581><U+2583>
diabetes 0 1.00 0.03 0.16 0.00 0.00 0.0 0.00 1.0 <U+2587><U+2581><U+2581><U+2581><U+2581>
totChol 50 0.99 236.72 44.59 107.00 206.00 234.0 263.00 696.0 <U+2586><U+2587><U+2581><U+2581><U+2581>
sysBP 0 1.00 132.35 22.04 83.50 117.00 128.0 144.00 295.0 <U+2587><U+2587><U+2581><U+2581><U+2581>
diaBP 0 1.00 82.89 11.91 48.00 75.00 82.0 89.88 142.5 <U+2581><U+2587><U+2585><U+2581><U+2581>
BMI 19 1.00 25.80 4.08 15.54 23.07 25.4 28.04 56.8 <U+2585><U+2587><U+2581><U+2581><U+2581>
heartRate 1 1.00 75.88 12.03 44.00 68.00 75.0 83.00 143.0 <U+2582><U+2587><U+2583><U+2581><U+2581>
glucose 388 0.91 81.97 23.96 40.00 71.00 78.0 87.00 394.0 <U+2587><U+2581><U+2581><U+2581><U+2581>
TenYearCHD 0 1.00 0.15 0.36 0.00 0.00 0.0 0.00 1.0 <U+2587><U+2581><U+2581><U+2581><U+2582>
##            male             age       education   currentSmoker      cigsPerDay 
##               0               0             105               0              29 
##          BPMeds prevalentStroke    prevalentHyp        diabetes         totChol 
##              53               0               0               0              50 
##           sysBP           diaBP             BMI       heartRate         glucose 
##               0               0              19               1             388 
##      TenYearCHD 
##               0
## Warning: `gather_()` was deprecated in tidyr 1.2.0.
## Please use `gather()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.

Since missing data is only 1% so we will be dropping the mising values directly

## tibble [3,656 x 16] (S3: tbl_df/tbl/data.frame)
##  $ male           : num [1:3656] 1 0 1 0 0 0 0 0 1 1 ...
##  $ age            : num [1:3656] 39 46 48 61 46 43 63 45 52 43 ...
##  $ education      : num [1:3656] 4 2 1 3 3 2 1 2 1 1 ...
##  $ currentSmoker  : num [1:3656] 0 0 1 1 1 0 0 1 0 1 ...
##  $ cigsPerDay     : num [1:3656] 0 0 20 30 23 0 0 20 0 30 ...
##  $ BPMeds         : num [1:3656] 0 0 0 0 0 0 0 0 0 0 ...
##  $ prevalentStroke: num [1:3656] 0 0 0 0 0 0 0 0 0 0 ...
##  $ prevalentHyp   : num [1:3656] 0 0 0 1 0 1 0 0 1 1 ...
##  $ diabetes       : num [1:3656] 0 0 0 0 0 0 0 0 0 0 ...
##  $ totChol        : num [1:3656] 195 250 245 225 285 228 205 313 260 225 ...
##  $ sysBP          : num [1:3656] 106 121 128 150 130 ...
##  $ diaBP          : num [1:3656] 70 81 80 95 84 110 71 71 89 107 ...
##  $ BMI            : num [1:3656] 27 28.7 25.3 28.6 23.1 ...
##  $ heartRate      : num [1:3656] 80 95 75 65 85 77 60 79 76 93 ...
##  $ glucose        : num [1:3656] 77 76 70 103 85 99 85 78 79 88 ...
##  $ TenYearCHD     : num [1:3656] 0 0 0 1 0 0 1 0 0 0 ...
##  - attr(*, "na.action")= 'omit' Named int [1:582] 15 22 27 34 37 43 50 55 71 73 ...
##   ..- attr(*, "names")= chr [1:582] "15" "22" "27" "34" ...

converting the required numerical variables to categorcial

## tibble [3,656 x 16] (S3: tbl_df/tbl/data.frame)
##  $ male           : Factor w/ 2 levels "0","1": 2 1 2 1 1 1 1 1 2 2 ...
##  $ age            : num [1:3656] 39 46 48 61 46 43 63 45 52 43 ...
##  $ education      : Factor w/ 4 levels "1","2","3","4": 4 2 1 3 3 2 1 2 1 1 ...
##  $ currentSmoker  : Factor w/ 2 levels "0","1": 1 1 2 2 2 1 1 2 1 2 ...
##  $ cigsPerDay     : num [1:3656] 0 0 20 30 23 0 0 20 0 30 ...
##  $ BPMeds         : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
##  $ prevalentStroke: Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
##  $ prevalentHyp   : Factor w/ 2 levels "0","1": 1 1 1 2 1 2 1 1 2 2 ...
##  $ diabetes       : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
##  $ totChol        : num [1:3656] 195 250 245 225 285 228 205 313 260 225 ...
##  $ sysBP          : num [1:3656] 106 121 128 150 130 ...
##  $ diaBP          : num [1:3656] 70 81 80 95 84 110 71 71 89 107 ...
##  $ BMI            : num [1:3656] 27 28.7 25.3 28.6 23.1 ...
##  $ heartRate      : num [1:3656] 80 95 75 65 85 77 60 79 76 93 ...
##  $ glucose        : num [1:3656] 77 76 70 103 85 99 85 78 79 88 ...
##  $ TenYearCHD     : Factor w/ 2 levels "0","1": 1 1 1 2 1 1 2 1 1 1 ...
##  - attr(*, "na.action")= 'omit' Named int [1:582] 15 22 27 34 37 43 50 55 71 73 ...
##   ..- attr(*, "names")= chr [1:582] "15" "22" "27" "34" ...

Splitting the data in Training and Testing Set

## [1] 2559   16
## [1] 1097   16
## 
## Call:
## glm(formula = TenYearCHD ~ ., family = binomial, data = df_training)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.7978  -0.5987  -0.4261  -0.2810   2.8563  
## 
## Coefficients:
##                   Estimate Std. Error z value Pr(>|z|)    
## (Intercept)      -8.488135   0.834536 -10.171  < 2e-16 ***
## male1             0.476958   0.131823   3.618 0.000297 ***
## age               0.064610   0.008087   7.990 1.35e-15 ***
## education2       -0.155368   0.146202  -1.063 0.287920    
## education3       -0.269287   0.185687  -1.450 0.146998    
## education4       -0.079915   0.198345  -0.403 0.687016    
## currentSmoker1   -0.031323   0.187502  -0.167 0.867328    
## cigsPerDay        0.021476   0.007474   2.873 0.004061 ** 
## BPMeds1           0.249901   0.287112   0.870 0.384084    
## prevalentStroke1  0.806975   0.552401   1.461 0.144057    
## prevalentHyp1     0.239806   0.162874   1.472 0.140928    
## diabetes1         0.070759   0.378918   0.187 0.851864    
## totChol           0.001801   0.001345   1.340 0.180392    
## sysBP             0.010112   0.004500   2.247 0.024628 *  
## diaBP             0.006064   0.007692   0.788 0.430511    
## BMI               0.010058   0.015002   0.670 0.502596    
## heartRate        -0.002742   0.004936  -0.556 0.578510    
## glucose           0.007123   0.002691   2.647 0.008125 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 2181.2  on 2558  degrees of freedom
## Residual deviance: 1928.2  on 2541  degrees of freedom
## AIC: 1964.2
## 
## Number of Fisher Scoring iterations: 5

The variables that significantly effect the probability of a patient having CHD are: Gender, Age, CigsPerDay, prevalentHyp1, ## sysBP, glucose

##          TenYearCHD
## log.preds    0    1
##    CHD      19   27
##    No CHD 2151  362