install.packages("aplore3")
install.packages("forcats")
install.packages("dplyr")
library(aplore3)
# dataset
library(forcats)
# relevel
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
# relevel
icu
# Relevel
# When releveled, Died=0, Lived=1

icurlvl<-icu%>%mutate(sta=fct_relevel(sta,"Died","Lived"))
levels(icurlvl$sta)
## [1] "Died"  "Lived"

1.

a. Write the model equation

fitted model–> Pr(yi=1)=logit^-1(3.3-0.028(age)-1.67(cprYes)-1.13(cre>2.0))

mdl<-glm(formula= sta~age+cpr+cre, family="binomial", data=icurlvl)
summary(mdl)
## 
## Call:
## glm(formula = sta ~ age + cpr + cre, family = "binomial", data = icurlvl)
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  3.32901    0.74884   4.446 8.77e-06 ***
## age         -0.02814    0.01125  -2.502  0.01235 *  
## cprYes      -1.69680    0.62145  -2.730  0.00633 ** 
## cre> 2.0    -1.13328    0.70191  -1.615  0.10641    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 200.16  on 199  degrees of freedom
## Residual deviance: 181.47  on 196  degrees of freedom
## AIC: 189.47
## 
## Number of Fisher Scoring iterations: 5

b. Interpret the slope coefficients

age: -0.02814 cpr: -1.69680 cre: -1.13328

(-0.02814/4)*100
## [1] -0.7035
(-1.69680/4)*100
## [1] -42.42
(-1.13328/4)*100
## [1] -28.332

1 unit increase in age, 0.02814 decrease in log odds or 0.685% decrease in probability of discharge

1 unit increase in cpr, 1.7 decrease in log odds or 42% decrease in probability of discharge

1 unit increase in cre, 1.13 decrease in log odds or 28% decrease in probability of discharge

c. Make predictions

i. Compare the odds of survival for those who did receive CPR prior to admission to those who did not receive CPR prior to admission

The reference is not recieving CPR, exponentiating the reciprocal of the logistic regression coefficient gives us the OR of surving to discharge for those who did not recieve CPR. The odds of surviving for those who did not recieve CPR are 5 times as much as those who did receive cpr.

exp(-1.69680)
## [1] 0.183269
exp(1.69680)
## [1] 5.456459

ii. Compare the odds of survival for those who had elevated creatine level beyond 2.0 mg/dL to those who did not.

Exponentiating the reciprocal of the coefficient gives us the odds of survivng for patients that did not have elevated creatine levels beyond 2.0 mg/dL.The odds of survival for patients who did not have creatine levels above 2.0 mg/dL are 3 times as much as those who did have elevated creatine levels.

exp(-1.13328)
## [1] 0.3219754
exp(1.13328 )
## [1] 3.105827

iii. Calculate the odds of survival for a 65-year-old individual who did not receive CPR prior to admission and had a creatine level of 1.1 mg/dL. Is this individual more likely to survive than not survive?

The odds of survival to discharge for an individual who is 65 years old and did not recieve CPR prior to admission and had a creatine level of 1.1 mg/dL is 4.480. probability = 4.48/1+4.48= 0.818 or 82%. This individual is more likely to survive than not survive.

exp(3.329-0.02814*(65)-1.69680*(0)-1.13328*(0))
## [1] 4.481241
4.481241/(1+4.481241)
## [1] 0.8175596

or

log.oddsa=predict(mdl,newdata=data.frame(age=65,cpr="No",cre="<= 2.0"))
exp(log.oddsa)
##        1 
## 4.479995

iv. Compare the odds of survival for a 30-year-old individual who received CPR prior to admission and had a creatine level of 1.5 mg/dL to the odds of survival for the individual described in part iii.

The odds of survival for an individual who is 30 years old and did recieve CPR prior to admission and had a creatine level of 1.5 mg/dL is 2.2. probability = 2.2/1+2.2= 0.68 or 68%. This individual is more likely to survive than not survive.

exp(3.329-0.02814*(30)-1.69680*(1)-1.13328*(0))
## [1] 2.198994
2.198994/(1+2.198994)
## [1] 0.6874017

or

log.oddsb=predict(mdl,newdata = data.frame(age=30,cpr="Yes",cre="<= 2.0"))
exp(log.oddsb)
##        1 
## 2.198713

The odds of survival to discharge for the 65 year old is 2.04 times the odds for the 30 year old.

4.481241/2.198994
## [1] 2.03786

or

exp(log.oddsa)/exp(log.oddsb)
##        1 
## 2.037554

d. Identify the significant predictors of survival to discharge from this model

Pr(>|z|) values less than 0.005 –> age, cprYes

summary(mdl)
## 
## Call:
## glm(formula = sta ~ age + cpr + cre, family = "binomial", data = icurlvl)
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  3.32901    0.74884   4.446 8.77e-06 ***
## age         -0.02814    0.01125  -2.502  0.01235 *  
## cprYes      -1.69680    0.62145  -2.730  0.00633 ** 
## cre> 2.0    -1.13328    0.70191  -1.615  0.10641    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 200.16  on 199  degrees of freedom
## Residual deviance: 181.47  on 196  degrees of freedom
## AIC: 189.47
## 
## Number of Fisher Scoring iterations: 5