Load Necessary Libraries
library(tidymodels)
library(ggplot2)
library(readr)
library(dplyr)
# Ensure 'kknn' is installed for KNN models
if (!requireNamespace("kknn", quietly = TRUE)) {
install.packages("kknn")
}
## Load the Dataset
boston <- readr::read_csv("C:\\Users\\raush\\boston.csv", show_col_types = FALSE)
## Split the Data into Training and Test Sets
set.seed(123)
split <- initial_split(boston, prop = 0.7, strata = cmedv)
train <- training(split)
test <- testing(split)
# Print the number of observations
n_train <- nrow(train)
n_test <- nrow(test)
n_train
## [1] 352
n_test
## [1] 154
## Compare Distributions of cmedv
ggplot(train, aes(x = cmedv)) +
geom_density(color = "blue", fill = "blue", alpha = 0.3) +
geom_density(data = test, aes(x = cmedv), color = "red", fill = "red", alpha = 0.3) +
labs(title = "Distribution of cmedv in Training vs Test Set", x = "cmedv", y = "Density")

## Fit Linear Regression Models
# Model using 'rm' to predict 'cmedv'
lm1 <- linear_reg() %>%
set_engine("lm") %>%
fit(cmedv ~ rm, data = train)
# Compute RMSE for Model 1
lm1 %>%
predict(test) %>%
bind_cols(test %>% select(cmedv)) %>%
rmse(truth = cmedv, estimate = .pred)
## # A tibble: 1 × 3
## .metric .estimator .estimate
## <chr> <chr> <dbl>
## 1 rmse standard 6.83
# Model using all features to predict 'cmedv'
lm2 <- linear_reg() %>%
set_engine("lm") %>%
fit(cmedv ~ ., data = train)
# Compute RMSE for Model 2
lm2 %>%
predict(test) %>%
bind_cols(test %>% select(cmedv)) %>%
rmse(truth = cmedv, estimate = .pred)
## # A tibble: 1 × 3
## .metric .estimator .estimate
## <chr> <chr> <dbl>
## 1 rmse standard 4.83
## Fit a KNN Model
knn <- nearest_neighbor() %>%
set_engine("kknn") %>%
set_mode("regression") %>%
fit(cmedv ~ ., data = train)
# Compute RMSE for the KNN model
knn %>%
predict(test) %>%
bind_cols(test %>% select(cmedv)) %>%
rmse(truth = cmedv, estimate = .pred)
## # A tibble: 1 × 3
## .metric .estimator .estimate
## <chr> <chr> <dbl>
## 1 rmse standard 3.37
## Summary Statistics and Missing Values
# Check for missing values
missing_values <- sum(is.na(boston$cmedv))
missing_values
## [1] 0
# Calculate minimum, maximum, and average cmedv
summary_stats <- boston %>%
summarise(
minimum_cmedv = min(cmedv, na.rm = TRUE),
maximum_cmedv = max(cmedv, na.rm = TRUE),
average_cmedv = mean(cmedv, na.rm = TRUE)
)
summary_stats
## # A tibble: 1 × 3
## minimum_cmedv maximum_cmedv average_cmedv
## <dbl> <dbl> <dbl>
## 1 5 50 22.5
# Calculate the median of cmedv
median_cmedv <- median(boston$cmedv, na.rm = TRUE)
print(median_cmedv)
## [1] 21.2