STAT 425 Homework 1

Problem 1

library(faraway)
data(prostate)
library(ggplot2)

1A

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

1B

prostate$gleason <- factor(prostate$gleason)
prostate$svi <- factor(prostate$svi)

1C

hist(prostate$lcavol, main = "Histogram of lcavol", xlab = "lcavol")
hist(prostate$lcavol, main = "Histogram of lcavol", xlab = "lcavol")

hist(prostate$lweight, main = "Histogram of lweight", xlab = "lweight")

hist(prostate$age, main = "Histogram of age", xlab = "age")

hist(prostate$lbph, main = "Histogram of lbph", xlab = "lbph")

1D

boxplot(lpsa ~ svi, data = prostate, main = "Boxplot of lpsa by svi", xlab = "svi", ylab = "lpsa")

# The higher a mans IPSA the more likely he has SVI

1E

plot(lpsa ~ lcavol, data = prostate, main = "Scatterplot of lpsa against lcavol", xlab = "lcavol", ylab = "lpsa")

##Problem 2 # 2A

model_lpsa_lcavol <- lm(lpsa ~ lcavol, data = prostate)
summary(model_lpsa_lcavol)
## 
## Call:
## lm(formula = lpsa ~ lcavol, data = prostate)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.67625 -0.41648  0.09859  0.50709  1.89673 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  1.50730    0.12194   12.36   <2e-16 ***
## lcavol       0.71932    0.06819   10.55   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7875 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

Interpretation of coefficients:

The estimated regression equation is lpsa = intercept + slope * lcavol

The coefficient for lcavol represents the change in lpsa for a one-unit increase in lcavol.

#2B

residual_standard_error <- summary(model_lpsa_lcavol)$sigma
r_squared <- summary(model_lpsa_lcavol)$r.squared

cat("Residual Standard Error:", residual_standard_error, "\n")
## Residual Standard Error: 0.7874994
cat("R-squared Value:", r_squared, "\n")
## R-squared Value: 0.5394319

#2C

residuals <- residuals(model_lpsa_lcavol)
mean_residuals <- mean(residuals)
median_residuals <- median(residuals)

cat("Mean of Residuals:", mean_residuals, "\n")
## Mean of Residuals: -4.913336e-17
cat("Median of Residuals:", median_residuals, "\n")
## Median of Residuals: 0.09859487

2D

cor_residuals_fitted <- cor(residuals, fitted(model_lpsa_lcavol))
cat("Correlation of Residuals with Fitted Values:", cor_residuals_fitted, "\n")
## Correlation of Residuals with Fitted Values: 1.549827e-16

2E

model_lcavol_lpsa <- lm(lpsa ~ lcavol, data = prostate)

lpsa_range <- range(prostate$lpsa)
lpsa_values <- seq(lpsa_range[1], lpsa_range[2], length.out = 100)

plot(prostate$lpsa, prostate$lcavol, xlab = "lpsa", ylab = "lcavol", main = "Regression Lines")
legend("topright", legend = c("lpsa on lcavol", "lcavol on lpsa"), col = c("blue", "red"), lwd = 2)

#Problem 4

library(alr4)
## Loading required package: car
## Loading required package: carData
## 
## Attaching package: 'car'
## The following objects are masked from 'package:faraway':
## 
##     logit, vif
## Loading required package: effects
## lattice theme set by effectsTheme()
## See ?effectsTheme for details.
## 
## Attaching package: 'alr4'
## The following objects are masked from 'package:faraway':
## 
##     cathedral, pipeline, twins
data(Forbes)
str(Forbes)
## 'data.frame':    17 obs. of  3 variables:
##  $ bp   : num  194 194 198 198 199 ...
##  $ pres : num  20.8 20.8 22.4 22.7 23.1 ...
##  $ lpres: num  132 132 135 136 136 ...

#4A

plot1 <- ggplot(Forbes, aes(x = bp, y = lpres)) +
  geom_point() +
  labs(title = "Scatter Plot: lpres vs. bp", x = "bp temperature", y = "lpres")

Forbes$Kelvinbp <- 255.37 + (5/9) * Forbes$bp
Forbes$inverse_Kelvinbp <- 1 / Forbes$Kelvinbp

plot2 <- ggplot(Forbes, aes(x = inverse_Kelvinbp, y = lpres)) +
  geom_point() +
  labs(title = "Scatter Plot: lpres vs. 1/Kelvinbp", x = "1/Kelvinbp", y = "lpres")

plot1

plot2

#4B

model1 <- lm(lpres ~ bp, data = Forbes)
summary(model1)
## 
## Call:
## lm(formula = lpres ~ bp, data = Forbes)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.32220 -0.14473 -0.06664  0.02184  1.35978 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -42.13778    3.34020  -12.62 2.18e-09 ***
## bp            0.89549    0.01645   54.43  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.379 on 15 degrees of freedom
## Multiple R-squared:  0.995,  Adjusted R-squared:  0.9946 
## F-statistic:  2963 on 1 and 15 DF,  p-value: < 2.2e-16
model2 <- lm(lpres ~ inverse_Kelvinbp, data = Forbes)
summary(model2)
## 
## Call:
## lm(formula = lpres ~ inverse_Kelvinbp, data = Forbes)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.33396 -0.17132 -0.03424  0.01791  1.31762 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)          734.47      10.59   69.37   <2e-16 ***
## inverse_Kelvinbp -218968.41    3897.33  -56.18   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3673 on 15 degrees of freedom
## Multiple R-squared:  0.9953, Adjusted R-squared:  0.995 
## F-statistic:  3157 on 1 and 15 DF,  p-value: < 2.2e-16

#4C

fitted_values_model1 <- predict(model1)
fitted_values_model2 <- predict(model2)

plot(fitted_values_model1, fitted_values_model2, xlab = "Fitted Values Model 1", ylab = "Fitted Values Model 2", main = "Fitted Values Comparison")
abline(0, 1, col = "red")