This lab introduces simple logistic regression, a model for the association of a binary response variable with a single predictor variable. Logistic regression generalizes methods for two-way tables and allows for the use of a numerical predictor.

The material in this lab corresponds to Section 9.xx in *OpenIntro Statistics*.

### Introduction

*Odds and probabilities*

If the probability of an event $A$ is $p$, the odds of the event are 
\[\dfrac{p}{1 - p}. \]

Given the odds of an event $A$, the probability of the event is
\[\dfrac{\text{odds}}{1 + \text{odds}}. \]

*Simple logistic regression*

Suppose that $Y$ is a binary response variable and $X$ is a predictor variable, where $Y = 1$ represents the particular outcome of interest.

The model for a single variable logistic regression, where $p(x) = P(Y = 1 | X = x)$, is
\[\text{log} \left[ \dfrac{p(x)}{1 - p(x)} \right] = \beta_0 + \beta_1 x. \]

The estimated model equation has the form
\[\text{log} \left[ \dfrac{\hat{p}(x)}{1 - \hat{p}(x)} \right] = b_0 + b_1 x, \]
where $b_0$ and $b_1$ are estimates of the model parameters $\beta_0$ and $\beta_1$.


### Background Information

Patients admitted to an intensive care unit (ICU) are either extremely ill or considered to be at great risk of serious complications. There are no widely accepted criteria for distinguishing between patients who should be admitted to an ICU and those for whom admission to other hospital units would be more appropriate. Thus, among different ICUs, there are wide ranges in a patient's chance of survival. When studies are done to compare effectiveness of ICU care, it is critical to have a reliable means of assessing the comparability of the different patient populations.

One such strategy for doing so involves the use of statistical modeling to relate empirical data for many patient variables to outcomes of interest. The following dataset consists of a sample of 200 subjects who were part of a much larger study on survival of patients following admission to an adult ICU.\footnote{From Hosmer D.W., Lemeshow, S., and Sturdivant, R.X. \textit{Applied Logistic Regression}. 3$^{rd}$ ed., 2013.} The major goal of the study was to develop a logistic regression model to predict the probability of survival to hospital discharge.\footnote{Lemeshow S., et al. Predicting the outcome of intensive care unit patients. \textit{Journal of the American Statistical Association} 83.402 (1988): 348-356.}

The following table provides a list of the variables in the dataset and their description. The data are accessible as the \texttt{icu} dataset in the \texttt{aplore3} package.

\begin{center}
\begin{tabular}{r|l}
\textbf{Variable} & \textbf{Description} \\
\hline
\texttt{id} & patient ID number \\
\texttt{sta} & patient status at discharge, either \texttt{Lived} or \texttt{Died} \\
\texttt{age} & age in years (when admitted) \\
\texttt{gender} & gender, either \texttt{Male} or \texttt{Female} \\
\texttt{can} & cancer part of current issue, either \texttt{No} or \texttt{Yes} \\
\texttt{crn} & history of chronic renal failure, either \texttt{No} or \texttt{Yes}\\
\texttt{inf} & infection probable at admission, either \texttt{No} or \texttt{Yes} \\
\texttt{cpr} & CPR prior to admission, either \texttt{No} or \texttt{Yes} \\
\texttt{sys} & systolic blood pressure at admission, in mm Hg \\
\texttt{hra} & heart rate at admission, in beats per minute \\
\texttt{pre} & previous admission to an ICU within 6 months, either \texttt{No} or \texttt{Yes} \\
\texttt{type} & type of admission, either \texttt{Elective} or \texttt{Emergency} \\
\texttt{fra} & long bone, multiple, neck, single area, or hip fracture, either \texttt{No} or \texttt{Yes} \\
\texttt{po2} & $PO_2$ from initial blood gases, either \texttt{60} or \texttt{<=60}, in mm Hg\\
\texttt{ph} & $pH$ from initial blood gases, either \texttt{>= 7.25} or \texttt{< 7.25} \\
\texttt{pco} & $PCO_2$ from initial blood gases, either \texttt{<= 45} or \texttt{>45}, in mm Hg \\
\texttt{bic} & $HCO_3$ (bicarbonate) from initial blood gases, either \texttt{>= 18} or \texttt{<18}, in mm Hg \\
\texttt{cre} & creatinine from initial blood gases, either \texttt{<= 2.0} or \texttt{> 2.0}, in mg/dL \\
\texttt{loc} & level of consciousness at admission, either \texttt{Nothing}, \texttt{Stupor}, or \texttt{Coma}
\end{tabular}
\end{center}


### Odds and probabilities

1. Create a two-way table of survival to discharge by whether CPR was administered prior to admission. The template provides code for re-leveling the \texttt{sta} variable such that \texttt{0} corresponds to \texttt{Died} and \texttt{1} corresponds to \texttt{Lived}.

```{r, eval = FALSE}
#install the package (only needs to be done once)
install.packages("aplore3")
```


```{r, warning = FALSE}
#load the data
library(aplore3)
data("icu")

#relevel survival so that 1 corresponds to surviving to discharge
icu$sta = factor(icu$sta, levels = rev(levels(icu$sta)))

#create two-way table
addmargins(table(icu$sta, icu$cpr,
                 dnn = c("Survival", "Prior CPR")))
```


a) Calculate the odds of survival to discharge for those who did not receive CPR prior to ICU admission. Is someone who did not receive CPR prior to admission more likely to survive to discharge than to not survive to discharge?
    
    
```{r}
#use r as a calculator
```
    
    
b) Calculate the odds of survival to discharge for those who received CPR prior to ICU admission. Is someone who received CPR prior to admission more likely to survive to discharge than not?
    
    
```{r}
#use r as a calculator

```
    
    
c) Calculate the odds ratio of survival to discharge, comparing patients who receive CPR prior to admission to those who do not receive CPR prior to admission.
    
    
```{r}
#use r as a calculator
    
```
    

2. Creatinine level in the data are recorded as being either less than or equal to 2.0 mg/dL or greater than 2.0 mg/dL. A typical creatinine level is between 0.5 - 1.0 mg/dL, and elevated creatinine may be a sign of renal failure.

```{r}
#create two-way table

```


  a) Calculate the odds of survival to discharge for patients who have a creatinine level less than or equal to 2.0 mg/dL. From the odds, calculate the probability of survival to discharge for these patients.

  
```{r}
#use r as a calculator

```

  
  b) Calculate the probability of survival to discharge for patients who have a creatinine level greater than 2.0 mg/dL. From the probability, calculate the odds of survival to discharge for these patients.
  
  
```{r}
#use r as a calculator

```

  
  c) Compute and interpret the odds ratio of survival to discharge, comparing patients with creatinine $> 2.0$ mg/dL to those with creatinine $\leq$ 2.0 mg/dL.

  
```{r}
#use r as a calculator

```

### Simple logistic regression

3. Fit a logistic regression model to predict survival to discharge from prior CPR.

```{r}
#fit a model
glm(sta ~ cpr, data = icu, family = binomial(link = "logit"))
```


  a) Write the model equation estimated from the data.
  
  
  
    
  b) Interpret the intercept. Confirm that the interpretation coheres with the answer to Question 1, part a).
  
  
  
    
  c) Interpret the slope coefficient. Compute the exponential of the slope coefficient and confirm that this matches the answer to Question 1, part c).
  
  
  
    
  d) Compute and interpret an odds ratio that summarizes the association between survival to discharge and prior CPR.
  
  
  
```{r}
#compute odds ratio

```
  
    
  e) Is there evidence of a statistically significant association between survival to discharge and prior CPR at $\alpha = 0.05$?
  
  
  
```{r}
#use summary(glm( ))

```
  
  
4. Fit a logistic regression model to predict survival to discharge from an indicator of elevated creatinine.

```{r}
#fit the model

```

  a) Write the model equation estimated from the data.
    
  
  
  
  b) Interpret the intercept and slope coefficient.
    
  
  
  
  c) Compute and interpret an odds ratio that summarizes the association between survival to discharge and an indicator of elevated creatinine.
    
  
```{r}
#compute odds ratio

```
  
  
d) Is there evidence of a statistically significant association between survival to discharge and an indicator of elevated creatinine at $\alpha = 0.05$?
  
  
```{r}

```
  

5. Fit a logistic regression model to predict survival to discharge from age.

```{r}
#fit the model

```


  a) Write the model equation estimated from the data.
  
    
    
  b) Does the intercept have a meaningful interpretation in the context of the data?
    
  
  
  
  
  c) Interpret the slope coefficient. 
    
  
  
  
  d) Calculate the odds of survival to discharge for a 70-year-old individual. Is a 70-year-old individual more likely to survive than not?

  
```{r}
#use r as a calculator


#alternatively, use predict()...

```
  
  
  
  e) Calculate the odds ratio of survival to discharge comparing a 45-year-old individual to a 70-year-old individual.
  
  
```{r}
#use r as a calculator

#alternatively, use predict()...

```
  
  
