Validate that the prostate data frame contains 97 observations.
nrow(prostate)
## [1] 97
Calculate descriptive statistics of each of the variables.
summary(prostate)
## lcavol lweight age lbph
## Min. :-1.3471 Min. :2.375 Min. :41.00 Min. :-1.3863
## 1st Qu.: 0.5128 1st Qu.:3.376 1st Qu.:60.00 1st Qu.:-1.3863
## Median : 1.4469 Median :3.623 Median :65.00 Median : 0.3001
## Mean : 1.3500 Mean :3.653 Mean :63.87 Mean : 0.1004
## 3rd Qu.: 2.1270 3rd Qu.:3.878 3rd Qu.:68.00 3rd Qu.: 1.5581
## Max. : 3.8210 Max. :6.108 Max. :79.00 Max. : 2.3263
## svi lcp gleason pgg45
## Min. :0.0000 Min. :-1.3863 Min. :6.000 Min. : 0.00
## 1st Qu.:0.0000 1st Qu.:-1.3863 1st Qu.:6.000 1st Qu.: 0.00
## Median :0.0000 Median :-0.7985 Median :7.000 Median : 15.00
## Mean :0.2165 Mean :-0.1794 Mean :6.753 Mean : 24.38
## 3rd Qu.:0.0000 3rd Qu.: 1.1786 3rd Qu.:7.000 3rd Qu.: 40.00
## Max. :1.0000 Max. : 2.9042 Max. :9.000 Max. :100.00
## lpsa
## Min. :-0.4308
## 1st Qu.: 1.7317
## Median : 2.5915
## Mean : 2.4784
## 3rd Qu.: 3.0564
## Max. : 5.5829
Create a new data frame that includes the following variables: lcavol, lweight, age and lpsa. Use this new data frame for all questions below.
newprostatedf <-prostate[c("lcavol", "lweight", "age", "lpsa")]
Calculate descriptive statistics of each of the variables using the new data frame.
summary(newprostatedf)
## lcavol lweight age lpsa
## Min. :-1.3471 Min. :2.375 Min. :41.00 Min. :-0.4308
## 1st Qu.: 0.5128 1st Qu.:3.376 1st Qu.:60.00 1st Qu.: 1.7317
## Median : 1.4469 Median :3.623 Median :65.00 Median : 2.5915
## Mean : 1.3500 Mean :3.653 Mean :63.87 Mean : 2.4784
## 3rd Qu.: 2.1270 3rd Qu.:3.878 3rd Qu.:68.00 3rd Qu.: 3.0564
## Max. : 3.8210 Max. :6.108 Max. :79.00 Max. : 5.5829
Create a scatter plot matrix for all the variables using the new data frame.
pairs(as.matrix(newprostatedf))
Create a (Pearson) correlation matrix for all the variables.
cor(newprostatedf)
## lcavol lweight age lpsa
## lcavol 1.0000000 0.1941284 0.2249999 0.7344603
## lweight 0.1941284 1.0000000 0.3075247 0.3541218
## age 0.2249999 0.3075247 1.0000000 0.1695929
## lpsa 0.7344603 0.3541218 0.1695929 1.0000000
Show the same matrix again, but round the correlations (use two decimal places).
round(cor(newprostatedf),2)
## lcavol lweight age lpsa
## lcavol 1.00 0.19 0.22 0.73
## lweight 0.19 1.00 0.31 0.35
## age 0.22 0.31 1.00 0.17
## lpsa 0.73 0.35 0.17 1.00
Create a regression model: The predictor variable (X) should be lpsa. The outcome variable (Y) should be lcavol. Show the summary of the model.
lmod1 <- lm(lcavol ~ lpsa, data=newprostatedf)
summary(lmod1)
##
## Call:
## lm(formula = lcavol ~ lpsa, data = newprostatedf)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.15948 -0.59383 0.05034 0.50826 1.67751
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.50858 0.19419 -2.619 0.0103 *
## lpsa 0.74992 0.07109 10.548 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.8041 on 95 degrees of freedom
## Multiple R-squared: 0.5394, Adjusted R-squared: 0.5346
## F-statistic: 111.3 on 1 and 95 DF, p-value: < 2.2e-16
Visualize the two variables and the model you just created by doing the following:
Create a scatter plot. Put lcavol in the y-axis and lpsa in the x-axis. Include the regression line and label the axis.
plot(newprostatedf$lpsa, newprostatedf$lcavol,
col = "blue",
xlab = "log(prostate specific antigen in ng/ml)",
ylab = "log(cancer volume in cm^3)")
abline(lmod1, col = "red")
Update the regression model by adding a second predictor: age
Show the regression model summary
lmod2 <- lm(lcavol ~ lpsa + age, data=newprostatedf)
summary(lmod2)
##
## Call:
## lm(formula = lcavol ~ lpsa + age, data = newprostatedf)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.23486 -0.62468 0.02114 0.54421 1.71757
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.50978 0.70670 -2.136 0.0352 *
## lpsa 0.73201 0.07170 10.210 <2e-16 ***
## age 0.01637 0.01112 1.473 0.1442
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.7992 on 94 degrees of freedom
## Multiple R-squared: 0.5498, Adjusted R-squared: 0.5402
## F-statistic: 57.4 on 2 and 94 DF, p-value: < 2.2e-16