Question 1

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidymodels)
## ── Attaching packages ────────────────────────────────────── tidymodels 1.1.1 ──
## ✔ broom        1.0.5      ✔ rsample      1.2.0 
## ✔ dials        1.2.1      ✔ tune         1.1.2 
## ✔ infer        1.0.6      ✔ workflows    1.1.4 
## ✔ modeldata    1.3.0      ✔ workflowsets 1.0.1 
## ✔ parsnip      1.2.0      ✔ yardstick    1.3.0 
## ✔ recipes      1.0.10     
## ── Conflicts ───────────────────────────────────────── tidymodels_conflicts() ──
## ✖ scales::discard() masks purrr::discard()
## ✖ dplyr::filter()   masks stats::filter()
## ✖ recipes::fixed()  masks stringr::fixed()
## ✖ dplyr::lag()      masks stats::lag()
## ✖ yardstick::spec() masks readr::spec()
## ✖ recipes::step()   masks stats::step()
## • Use tidymodels_prefer() to resolve common conflicts.
library(vip)
## Warning: package 'vip' was built under R version 4.3.3
## 
## Attaching package: 'vip'
## 
## The following object is masked from 'package:utils':
## 
##     vi
boston <- read.csv("C:/Users/edmic/OneDrive/Documents/BANA 4080/Data/boston.csv")

Question 2

set.seed(123)
split <- initial_split(boston, prop = .7, strata = cmedv)
train <- training(split)
test <- testing(split)

Question 3

correlation_train <- cor(train)
cmedv_cor <- correlation_train[ , c("cmedv")]
correlation_train
##                  lon          lat        cmedv        crim          zn
## lon      1.000000000  0.118627847 -0.315456520  0.05231851 -0.17715022
## lat      0.118627847  1.000000000 -0.002024587 -0.06812909 -0.15128868
## cmedv   -0.315456520 -0.002024587  1.000000000 -0.38429834  0.34427202
## crim     0.052318507 -0.068129091 -0.384298342  1.00000000 -0.20965099
## zn      -0.177150216 -0.151288677  0.344272023 -0.20965099  1.00000000
## indus    0.049891859 -0.015842154 -0.489537963  0.41661456 -0.56273963
## chas    -0.167050035 -0.030762145  0.164575979 -0.04145114 -0.05755616
## nox      0.146558157 -0.032479012 -0.439021014  0.42510666 -0.53518054
## rm      -0.254380108 -0.091052134  0.708152619 -0.20243253  0.30374544
## age      0.189646885  0.105780247 -0.398915864  0.36579711 -0.56800475
## dis     -0.005552915 -0.119482055  0.271455846 -0.37892135  0.69823284
## rad      0.018072446 -0.180676676 -0.396999035  0.62180678 -0.32846152
## tax      0.038632974 -0.152837678 -0.479383777  0.58163434 -0.31466231
## ptratio  0.295131367  0.029730874 -0.500927283  0.29094218 -0.40426953
## b       -0.014049482  0.098512585  0.358302318 -0.39659958  0.18711154
## lstat    0.174459740  0.068907273 -0.742823016  0.46132769 -0.41963609
##               indus         chas         nox          rm        age
## lon      0.04989186 -0.167050035  0.14655816 -0.25438011  0.1896469
## lat     -0.01584215 -0.030762145 -0.03247901 -0.09105213  0.1057802
## cmedv   -0.48953796  0.164575979 -0.43902101  0.70815262 -0.3989159
## crim     0.41661456 -0.041451138  0.42510666 -0.20243253  0.3657971
## zn      -0.56273963 -0.057556164 -0.53518054  0.30374544 -0.5680048
## indus    1.00000000  0.092914630  0.77579143 -0.38742584  0.6613868
## chas     0.09291463  1.000000000  0.13998710  0.09636009  0.1139658
## nox      0.77579143  0.139987101  1.00000000 -0.33092646  0.7335741
## rm      -0.38742584  0.096360086 -0.33092646  1.00000000 -0.2509187
## age      0.66138681  0.113965790  0.73357407 -0.25091874  1.0000000
## dis     -0.72454202 -0.112561946 -0.77392372  0.23125230 -0.7357245
## rad      0.61509299  0.027838179  0.61259338 -0.19636554  0.4859286
## tax      0.71174833  0.001513233  0.66231556 -0.27804269  0.5198837
## ptratio  0.38632004 -0.119454252  0.20040485 -0.31750464  0.2939749
## b       -0.36534437  0.037985702 -0.37881764  0.09450024 -0.2867335
## lstat    0.61620507 -0.053505300  0.60568661 -0.63650085  0.6147860
##                  dis         rad          tax     ptratio           b
## lon     -0.005552915  0.01807245  0.038632974  0.29513137 -0.01404948
## lat     -0.119482055 -0.18067668 -0.152837678  0.02973087  0.09851258
## cmedv    0.271455846 -0.39699904 -0.479383777 -0.50092728  0.35830232
## crim    -0.378921353  0.62180678  0.581634340  0.29094218 -0.39659958
## zn       0.698232837 -0.32846152 -0.314662313 -0.40426953  0.18711154
## indus   -0.724542023  0.61509299  0.711748325  0.38632004 -0.36534437
## chas    -0.112561946  0.02783818  0.001513233 -0.11945425  0.03798570
## nox     -0.773923722  0.61259338  0.662315556  0.20040485 -0.37881764
## rm       0.231252304 -0.19636554 -0.278042694 -0.31750464  0.09450024
## age     -0.735724494  0.48592861  0.519883726  0.29397494 -0.28673352
## dis      1.000000000 -0.50420608 -0.531245189 -0.26280241  0.29068125
## rad     -0.504206078  1.00000000  0.913482857  0.47079664 -0.46025108
## tax     -0.531245189  0.91348286  1.000000000  0.45556397 -0.45742288
## ptratio -0.262802412  0.47079664  0.455563973  1.00000000 -0.21327451
## b        0.290681250 -0.46025108 -0.457422875 -0.21327451  1.00000000
## lstat   -0.518427525  0.51082454  0.559621480  0.38198242 -0.37205941
##               lstat
## lon      0.17445974
## lat      0.06890727
## cmedv   -0.74282302
## crim     0.46132769
## zn      -0.41963609
## indus    0.61620507
## chas    -0.05350530
## nox      0.60568661
## rm      -0.63650085
## age      0.61478605
## dis     -0.51842752
## rad      0.51082454
## tax      0.55962148
## ptratio  0.38198242
## b       -0.37205941
## lstat    1.00000000
cmedv_cor
##          lon          lat        cmedv         crim           zn        indus 
## -0.315456520 -0.002024587  1.000000000 -0.384298342  0.344272023 -0.489537963 
##         chas          nox           rm          age          dis          rad 
##  0.164575979 -0.439021014  0.708152619 -0.398915864  0.271455846 -0.396999035 
##          tax      ptratio            b        lstat 
## -0.479383777 -0.500927283  0.358302318 -0.742823016

Question 4

ggplot(train, aes(rm, cmedv)) +
  geom_point(alpha = 0.2) +
  geom_smooth(method = "lm")
## `geom_smooth()` using formula = 'y ~ x'

## Question 5

simple_lr <- linear_reg() %>%
  fit(cmedv ~ rm, data = train)

Question 6

residual <- simple_lr %>%
  predict(train) %>%
  bind_cols(train) %>%
  select(rm, cmedv, .pred) %>%
  mutate(residual = cmedv - .pred)
residual %>%
  mutate(squared_residuals = residual^2) %>%
  summarize(sum_of_squared_residuals = sum(squared_residuals))
## # A tibble: 1 × 1
##   sum_of_squared_residuals
##                      <dbl>
## 1                   14751.
simple_lr %>%
  predict(test) %>%
  bind_cols(test) %>%
  rmse(truth = cmedv, estimate = .pred)
## # A tibble: 1 × 3
##   .metric .estimator .estimate
##   <chr>   <chr>          <dbl>
## 1 rmse    standard        6.83

Question 7

mult_lr <- linear_reg() %>%
  fit(cmedv ~ ., data = train)
tidy(mult_lr) %>%
  arrange(desc(p.value))
## # A tibble: 16 × 5
##    term          estimate std.error statistic  p.value
##    <chr>            <dbl>     <dbl>     <dbl>    <dbl>
##  1 indus         -0.0143    0.0738     -0.193 8.47e- 1
##  2 age           -0.00921   0.0156     -0.591 5.55e- 1
##  3 lat            5.54      4.24        1.31  1.93e- 1
##  4 lon           -5.65      3.86       -1.46  1.45e- 1
##  5 (Intercept) -608.      342.         -1.78  7.64e- 2
##  6 zn             0.0332    0.0165      2.01  4.56e- 2
##  7 crim          -0.0830    0.0396     -2.10  3.65e- 2
##  8 chas           2.28      1.05        2.17  3.06e- 2
##  9 nox          -11.7       4.74       -2.46  1.44e- 2
## 10 tax           -0.0121    0.00436    -2.78  5.78e- 3
## 11 rad            0.272     0.0790      3.44  6.47e- 4
## 12 b              0.0123    0.00310     3.97  8.72e- 5
## 13 dis           -1.26      0.244      -5.17  4.06e- 7
## 14 ptratio       -0.874     0.163      -5.37  1.48e- 7
## 15 lstat         -0.479     0.0637     -7.51  5.24e-13
## 16 rm             4.37      0.516       8.46  8.13e-16

Question 8

mult_lr %>%
  predict(test) %>%
  bind_cols(test) %>%
  rmse(truth = cmedv, estimate = .pred)
## # A tibble: 1 × 3
##   .metric .estimator .estimate
##   <chr>   <chr>          <dbl>
## 1 rmse    standard        4.83

Question 9

mult_lr %>%
  vip::vip(num_features = 5)