---
title: "ADA2: Homework 06, Ch 03 A Taste of Model Selection for Multiple Regression"
author: "Name Here"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
html_document:
toc: true
number_sections: true
toc_depth: 5
code_folding: show
#df_print: paged
#df_print: kable
#toc_float: true
#collapsed: false
#smooth_scroll: TRUE
theme: cosmo #spacelab #yeti #united #cosmo
highlight: tango
pdf_document:
df_print: kable
fontsize: 12pt
geometry: margin=0.25in
always_allow_html: yes
---
```{R, echo=FALSE}
# I set some GLOBAL R chunk options here.
# (to hide this message add "echo=FALSE" to the code chunk options)
knitr::opts_chunk$set(comment = NA, message = FALSE, warning = FALSE, width = 100)
knitr::opts_chunk$set(fig.align = "center", fig.height = 4, fig.width = 6)
```
# Prostate-specific antigen (PSA)
A university medical center urology group
([Stamey, _et al._, 1989](http://www.ncbi.nlm.nih.gov/pubmed/2468795))
was interested in the association between a prostate-specific antigen (PSA) and
a number of prognostic clinical measurements in men with advanced prostate
cancer. Data were collected on 97 men who were about to undergo radical
prostectomies.
We will look at a subset of this dataset for this assignment;
later in the semester we'll be able to analyze the full complex dataset.
column | Variable name | Description
-|-|-
1 | Identification number | 1-97
2* | PSA level | Serum prostate-specific antigen level (ng/ml)
3* | Cancer volume | Estimate of prostate cancer volume (cc)
4* | Weight | Prostate weight (gm)
5* | Age | Age of patient (years)
6 | Benign prostatic | Amount of benign prostatic hyperplasia (cm2) hyperplasia
7 | Seminal vesicle invasion | Presence or absence of seminal vesicle invasion: 1 if yes; 0 if no
8 | Capsular penetration | Degree of capsular penetration (cm)
9 | Gleason score | Pathologically determined grade of disease (6,7,8), higher indicates worse prognosis
__Background:__
Until recently, PSA was commonly recommended as a screening mechanism for
detecting prostate cancer. To be an efficient screening tool it is important
that we understand how PSA levels relate to factors that may determine
prognosis and outcome. The PSA test measures the blood level of
prostate-specific antigen, an enzyme produced by the prostate. PSA levels under
4 ng/mL (nanograms per milliliter) are generally considered normal, while
levels over 4 ng/mL are considered abnormal (although in men over 65 levels up
to 6.5 ng/mL may be acceptable, depending upon each laboratorys reference
ranges). PSA levels between 4 and 10 ng/mL indicate a risk of prostate cancer
higher than normal, but the risk does not seem to rise within this six-point
range. When the PSA level is above 10 ng/mL, the association with cancer
becomes stronger. However, PSA is not a perfect test. Some men with prostate
cancer do not have an elevated PSA, and most men with an elevated PSA do not
have prostate cancer. PSA levels can change for many reasons other than cancer.
Two common causes of high PSA levels are enlargement of the prostate (benign
prostatic hypertrophy (BPH)) and infection in the prostate (prostatitis).
# Rubric
A goal here is to build a multiple regression model to predict PSA level
`PSA` from the cancer volume `V`, Prostate weight `Wt`, and Age `Age`.
A reasonable strategy would be to:
1. Examine the relationship between the response and the potential predictors.
2. Decide whether any of the variables should be transformed.
3. Perform a backward elimination using the desired response and predictors.
4. Given the selected model, examine the residuals and check for influential cases.
5. Repeat the process, if necessary.
6. Interpret the model and discuss any model limitations.
## __(1 p)__ Looking at the data
_Describe any patterns you see in the data.
Are the ranges for each variable reasonable?
Extreme/unusual observations?
Strong nonlinear trends with the response suggesting a transformation?_
```{R}
library(tidyverse)
# load ada functions
source("ada_functions.R")
# First, download the data to your computer,
# save in the same folder as this Rmd file.
# read the data
dat_psa <- read_csv("ADA2_HW_06_psa.csv")
str(dat_psa)
#dat_psa
dat_psa <-
dat_psa %>%
# select variables we want to use for this assignment
select(
ID
, PSA
, cancer_volume
, prostate_weight
, patient_age
) %>%
# simplify column names
rename(
PSA = PSA
, V = cancer_volume
, Wt = prostate_weight
, Age = patient_age
)
str(dat_psa)
summary(dat_psa)
```
```{R, fig.height = 6, fig.width = 6}
library(ggplot2)
library(GGally)
#p <- ggpairs(dat_psa)
# put scatterplots on top so y axis is vertical
p <- ggpairs(dat_psa %>% select(PSA, V, Wt, Age)
#, upper = list(continuous = wrap("points", alpha = 0.2, size = 0.5))
, upper = list(continuous = "points")
, lower = list(continuous = "cor")
)
print(p)
```
```{R}
# correlation matrix and associated p-values testing "H0: rho == 0"
library(Hmisc)
dat_psa %>%
select(PSA, V, Wt, Age) %>%
as.matrix() %>%
rcorr()
```
Bivariate scatterplots with points labeled by ID number to help
characterize relationships and identify outliers.
```{R, fig.height = 4, fig.width = 10, echo=FALSE}
# ggplot: Plot the data with linear regression fit and confidence bands
library(ggplot2)
p1 <- ggplot(dat_psa, aes(x = V, y = PSA, label = ID))
p1 <- p1 + geom_point(alpha = 1/2, size=3)
# plot labels next to points
p1 <- p1 + geom_text(hjust = 0.5, vjust = -0.5, alpha = 1/2)
# plot regression line and confidence band
p1 <- p1 + geom_smooth(method = lm)
p1 <- p1 + labs(title="Selling PSA by V")
#print(p1)
library(ggplot2)
p2 <- ggplot(dat_psa, aes(x = Wt, y = PSA, label = ID))
p2 <- p2 + geom_point(alpha = 1/2, size=3)
# plot labels next to points
p2 <- p2 + geom_text(hjust = 0.5, vjust = -0.5, alpha = 1/2)
# plot regression line and confidence band
p2 <- p2 + geom_smooth(method = lm)
p2 <- p2 + labs(title="Selling PSA by Wt")
library(ggplot2)
p3 <- ggplot(dat_psa, aes(x = Age, y = PSA, label = ID))
p3 <- p3 + geom_point(alpha = 1/2, size=3)
# plot labels next to points
p3 <- p3 + geom_text(hjust = 0.5, vjust = -0.5, alpha = 1/2)
# plot regression line and confidence band
p3 <- p3 + geom_smooth(method = lm)
p3 <- p3 + labs(title="Selling PSA by Age")
#print(p3)
library(gridExtra)
grid.arrange(grobs = list(p1, p2, p3), nrow=1)
```
### Solution
[answer]
## __(2 p)__ Backward selection, diagnostics of reduced model
Fit an appropriate full model and perform backward selection using __BIC__.
Discuss the diagnostics in terms of influential observations or problematic structure in the residuals.
### Solution
[answer]
Below I'll get you started with the linear model with all the selected main effects
and a set of diagnostic plots.
```{R}
# fit full model
lm_psa_full <-
lm(
PSA ~ V + Wt + Age
, data = dat_psa
)
#library(car)
#Anova(aov(lm_psa_full), type=3)
summary(lm_psa_full)
```
```{R, fig.height = 3, fig.width = 10, echo=FALSE}
# plot diagnistics
lm_diag_plots(lm_psa_full, sw_plot_set = "simpleAV")
```
Model selection starts here.
## __(3 p)__ Address model fit
If the model doesn't fit well (diagnostics tell you this, not $R^2$ or significance tests),
then address the lack of model fit.
Transformations and removing influential points are two strategies.
The decisions you make should be based on what you observed in the residual plots.
If there's an influential observation, remove it and see how that affects
the backward selection (whether the same predictors are retained),
the model fit (diagnostics),
and regression coefficient estimates (betas).
If there's a pattern in the residuals that can be addressed by a transformation,
guess at the appropriate transformation and try it.
Repeat until you are satisfied with the diagnostics meeting the model assumptions.
Below, briefly outline what you did (no need to show all the output)
by (1) identifying what you observed in the diagostics
and (2) the strategy you took to address that issue.
Finally, show the final model and the diagnostics for that.
Describe how the final model is different from the original;
in particular discuss whether variables retained are different from backward selection
and whether the sign and magnitude of the regression coefficients are much different.
```
# In the diagnostic plots, R uses the row label as the "Obs. number"
# Thus, you need to remove observations by their ID number.
# (An ID doesn't change when a lower row is removed, but the Obs. number will.)
# Below is an example of how to do that.
# remove influential observations
dat_psa_sub <-
dat_psa %>%
filter(
!(ID %in% c( [ID numbers here] )
)
```
### Solution
[answer]
## __(0 p)__ 3D plot
You may want to use the 3D visualization for your original final model and
after making some decisions based on diagnostics.
Are you convinced it's a reasonable summary of the general relationship?
```{R}
## visualize in 3D with surface
# library(rgl)
# library(car)
# scatter3d(y ~ x1 + x2, data = dat)
```
## __(1 p)__ Predictive ability of the final model
What proportion of variation in the response does the model explain over the mean of the response?
(This quantity indicates how precisely this model will predict new observations.)
### Solution
[answer]
## __(2 p)__ Interpret the final model
Write the equation for the final model and interpret each model coefficient.
### Solution
[answer]
## __(1 p)__ Inference to whom
To which population of people does this model make inference to?
Go back to the abstract of the original study (first sentence) to see which
population this sample of men was drawn from.
### Solution
[answer]