Setup
metro_area <- c("Atlanta", "Boston", "Chicago", "Dallas", "Denver", "Detroit", "LasVegas", "LosAngeles", "MiamiFt.Lauderdale", "MinneapolisStPaul", "NewYork", "Phoenix", "Portland", "SanDiego", "SanFrancisco", "Seattle", "Tampa", "WashingtonDC")
price_change <- c(1.2, -3.4, -0.9, 0.8, -0.7, -9.7, -6.1, -4.8, -6.4, -3.4, -3.8, -7.3, 3.8, -7.8, -4.1, 6.9, -8.8, -7.2)
loan_payments_overdue <- c(4.55, 3.31, 2.99, 4.26, 3.56, 4.71, 4.9, 3.05, 5.63, 3.01, 3.29, 3.26, 1.93, 3.45, 2.29, 1.65, 4.6, 3.14)
data <- data.frame(metro_area, price_change, loan_payments_overdue)
Question 1A
model <- lm(price_change ~ loan_payments_overdue, data)
r_squared <- summary(model)$r.squared
adj_r_squared <- summary(model)$adj.r.squared
r_squared
## [1] 0.2791527
adj_r_squared
## [1] 0.2340997
#An R-squared value of 0.2340997 means that about 23.41% of the variability in the percentage change in average home prices (Y) from July 2006 to July 2007 can be explained by the percentage of mortgage loans 30 days or more overdue (x) in the model. To me, this low R-Squared value indicates a weak relationship.
Question 1B
cov_matrix <- vcov(model)
se_beta0 <- sqrt(cov_matrix[1, 1])
se_beta1 <- sqrt(cov_matrix[2, 2])
se_beta0
## [1] 3.324019
se_beta1
## [1] 0.9033113
cov_matrix
## (Intercept) loan_payments_overdue
## (Intercept) 11.049104 -2.8821922
## loan_payments_overdue -2.882192 0.8159714
# The covarience indicates that when loan payments overdue increases, the Intercept tends to decrease.
Question 1C
X <- cbind(1, data$x)
validity_check <- t(X) %*% (model$residuals)
Question 1D
conf_interval <- confint(model)[2, ]
conf_interval
## 2.5 % 97.5 %
## -4.1634543 -0.3335853
Question 1E
predicted_value <- predict(model, newdata = data.frame(x = 4))
## Warning: 'newdata' had 1 row but variables found have 18 rows
conf_interval_predict <- predict(model, newdata = data.frame(x = 4), interval = "confidence")
## Warning: 'newdata' had 1 row but variables found have 18 rows
predicted_value
## 1 2 3 4 5 6 7
## -5.7162712 -2.9281067 -2.2085804 -5.0642005 -3.4902367 -6.0760344 -6.5032532
## 8 9 10 11 12 13 14
## -2.3434916 -8.1446726 -2.2535508 -2.8831363 -2.8156807 0.1748506 -3.2428995
## 15 16 17 18
## -0.6346165 0.8044361 -5.8286972 -2.5458584
conf_interval_predict
## fit lwr upr
## 1 -5.7162712 -8.491488 -2.94105466
## 2 -2.9281067 -4.949096 -0.90711737
## 3 -2.2085804 -4.440490 0.02332940
## 4 -5.0642005 -7.481961 -2.64644037
## 5 -3.4902367 -5.466633 -1.51384038
## 6 -6.0760344 -9.074366 -3.07770248
## 7 -6.5032532 -9.784038 -3.22246870
## 8 -2.3434916 -4.524323 -0.16266040
## 9 -8.1446726 -12.621330 -3.66801522
## 10 -2.2535508 -4.467903 -0.03919862
## 11 -2.8831363 -4.912535 -0.85373738
## 12 -2.8156807 -4.858976 -0.77238591
## 13 0.1748506 -3.474377 3.82407801
## 14 -3.2428995 -5.224844 -1.26095515
## 15 -0.6346165 -3.726845 2.45761233
## 16 0.8044361 -3.305859 4.91473096
## 17 -5.8286972 -8.671972 -2.98542254
## 18 -2.5458584 -4.659489 -0.43222788
Setup
library(faraway)
data(sat)
View(sat)
Question 2A
model <- lm(total ~ expend + ratio + salary, data = sat)
summary(model)
##
## Call:
## lm(formula = total ~ expend + ratio + salary, data = sat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -140.911 -46.740 -7.535 47.966 123.329
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1069.234 110.925 9.639 1.29e-12 ***
## expend 16.469 22.050 0.747 0.4589
## ratio 6.330 6.542 0.968 0.3383
## salary -8.823 4.697 -1.878 0.0667 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 68.65 on 46 degrees of freedom
## Multiple R-squared: 0.2096, Adjusted R-squared: 0.1581
## F-statistic: 4.066 on 3 and 46 DF, p-value: 0.01209
Question 2B
t_test_salary <- summary(model)$coefficients["salary", "t value"]
p_value_salary <- summary(model)$coefficients["salary", "Pr(>|t|)"]
p_value_salary
## [1] 0.06666771
t_test_salary
## [1] -1.878437
cat("T-test for βsalary: t =", t_test_salary, "p-value =", p_value_salary, "\n")
## T-test for βsalary: t = -1.878437 p-value = 0.06666771
Question 2C
f_test <- summary(model)$fstatistic
p_value_f_test <- pf(f_test[1], f_test[2], f_test[3], lower.tail = FALSE)
f_test
## value numdf dendf
## 4.066203 3.000000 46.000000
p_value_f_test
## value
## 0.01208607
#Yes it appears that the salary, ratio and expend impacts SAT scores based on the p-value of .012
Question 2D
model_takers <- lm(total ~ expend + ratio + salary + takers, data = sat)
anova_result <- anova(model, model_takers)
cat("F-test comparing the two models:\n")
## F-test comparing the two models:
print(anova_result)
## Analysis of Variance Table
##
## Model 1: total ~ expend + ratio + salary
## Model 2: total ~ expend + ratio + salary + takers
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 46 216812
## 2 45 48124 1 168688 157.74 2.607e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
t_test_takers <- summary(model_takers)$coefficients["takers", "t value"]
p_value_takers <- summary(model_takers)$coefficients["takers", "Pr(>|t|)"]
cat("T-test for βtakers: t =", t_test_takers, "p-value =", p_value_takers, "\n")
## T-test for βtakers: t = -12.55937 p-value = 2.606559e-16
setup
data(prostate)
Question 3A
original_model <- lm(lpsa ~ ., data = prostate)
original_model <- lm(lpsa ~ ., data = prostate)
summary(original_model)
##
## Call:
## lm(formula = lpsa ~ ., data = prostate)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.7331 -0.3713 -0.0170 0.4141 1.6381
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.669337 1.296387 0.516 0.60693
## lcavol 0.587022 0.087920 6.677 2.11e-09 ***
## lweight 0.454467 0.170012 2.673 0.00896 **
## age -0.019637 0.011173 -1.758 0.08229 .
## lbph 0.107054 0.058449 1.832 0.07040 .
## svi 0.766157 0.244309 3.136 0.00233 **
## lcp -0.105474 0.091013 -1.159 0.24964
## gleason 0.045142 0.157465 0.287 0.77503
## pgg45 0.004525 0.004421 1.024 0.30886
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.7084 on 88 degrees of freedom
## Multiple R-squared: 0.6548, Adjusted R-squared: 0.6234
## F-statistic: 20.86 on 8 and 88 DF, p-value: < 2.2e-16
significant_predictors <- subset(summary(original_model)$coefficients, 'Pr(>|t|)' <= 0.05)
selected_predictors <- rownames(significant_predictors)
Question 3B
set.seed(123)
perm_test_stat <- function(data) {
perm_model <- lm(lpsa ~ age, data = data)
return(summary(perm_model)$coef[2, 3])
}
Setup
library(faraway)
data(fat)
Question 4A
full_model <- lm(brozek ~ ., data = fat)
summary(full_model)
##
## Call:
## lm(formula = brozek ~ ., data = fat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.11191 -0.04847 0.00277 0.04625 1.47542
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 12.1524013 4.1718589 2.913 0.00393 **
## siri 0.8884085 0.0111341 79.792 < 2e-16 ***
## density -9.8456305 3.7471770 -2.627 0.00917 **
## age -0.0005268 0.0012935 -0.407 0.68421
## weight 0.0084855 0.0036200 2.344 0.01991 *
## height -0.0005459 0.0044439 -0.123 0.90234
## adipos -0.0153248 0.0124778 -1.228 0.22062
## free -0.0097388 0.0044270 -2.200 0.02880 *
## neck 0.0005002 0.0094279 0.053 0.95773
## chest 0.0021454 0.0043013 0.499 0.61840
## abdom 0.0014464 0.0044217 0.327 0.74388
## hip -0.0044514 0.0058941 -0.755 0.45087
## thigh 0.0156926 0.0059507 2.637 0.00892 **
## knee -0.0252126 0.0098531 -2.559 0.01113 *
## ankle 0.0027790 0.0089580 0.310 0.75667
## biceps -0.0147134 0.0069201 -2.126 0.03454 *
## forearm 0.0149983 0.0080832 1.855 0.06478 .
## wrist 0.0326518 0.0218000 1.498 0.13554
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.1706 on 234 degrees of freedom
## Multiple R-squared: 0.9995, Adjusted R-squared: 0.9995
## F-statistic: 3.046e+04 on 17 and 234 DF, p-value: < 2.2e-16
Question 4B
smaller_model <- lm(brozek ~ age + weight + height + abdom, data = fat)
summary(smaller_model)
##
## Call:
## lm(formula = brozek ~ age + weight + height + abdom, data = fat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.5105 -2.9346 0.0087 2.8942 9.4179
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -32.769636 6.541902 -5.009 1.04e-06 ***
## age -0.007051 0.024342 -0.290 0.772
## weight -0.123722 0.025046 -4.940 1.44e-06 ***
## height -0.116694 0.082727 -1.411 0.160
## abdom 0.889704 0.067267 13.226 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.126 on 247 degrees of freedom
## Multiple R-squared: 0.7211, Adjusted R-squared: 0.7166
## F-statistic: 159.7 on 4 and 247 DF, p-value: < 2.2e-16
# Compare the full model to the smaller model using an F-test
anova_result <- anova(smaller_model, full_model)
cat("F-test comparing the two models:\n")
## F-test comparing the two models:
anova_result
## Analysis of Variance Table
##
## Model 1: brozek ~ age + weight + height + abdom
## Model 2: brozek ~ siri + density + age + weight + height + adipos + free +
## neck + chest + abdom + hip + thigh + knee + ankle + biceps +
## forearm + wrist
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 247 4205.0
## 2 234 6.8 13 4198.2 11096 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1