if (!requireNamespace("tidyverse")) install.packages('tidyverse')
## Loading required namespace: tidyverse
if (!requireNamespace("caret")) install.packages('caret')
## Loading required namespace: caret
if (!requireNamespace("MASS")) install.packages('MASS')
if (!requireNamespace("leaps")) install.packages('leaps')
## Loading required namespace: leaps
library(tidyverse)# for data partitioning/ confusion matrix
## Warning: package 'tidyverse' was built under R version 4.3.3
## Warning: package 'ggplot2' was built under R version 4.3.3
## Warning: package 'forcats' was built under R version 4.3.3
## ── 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.5.0 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ 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(caret)# for logistic regression
## Warning: package 'caret' was built under R version 4.3.3
## Loading required package: lattice
##
## Attaching package: 'caret'
##
## The following object is masked from 'package:purrr':
##
## lift
library(MASS)
##
## Attaching package: 'MASS'
##
## The following object is masked from 'package:dplyr':
##
## select
library(leaps)
## Warning: package 'leaps' was built under R version 4.3.3
#-------------------------------------------Question 1 -----------------------------------------
# Load data
housing <- read.csv('C:/Users/sukkumar/Downloads/Ames_Housing_Data.csv')
housing <- na.omit(housing)
str(housing)
## 'data.frame': 1460 obs. of 20 variables:
## $ Id : int 1 2 3 4 5 6 7 8 9 10 ...
## $ LotArea : int 8450 9600 11250 9550 14260 14115 10084 10382 6120 7420 ...
## $ OverallQual : int 7 6 7 7 8 5 8 7 7 5 ...
## $ OverallCond : int 5 8 5 5 5 5 5 6 5 6 ...
## $ YearBuilt : int 2003 1976 2001 1915 2000 1993 2004 1973 1931 1939 ...
## $ YearRemodAdd: int 2003 1976 2002 1970 2000 1995 2005 1973 1950 1950 ...
## $ CentralAir : chr "Y" "Y" "Y" "Y" ...
## $ X1stFlrSF : int 856 1262 920 961 1145 796 1694 1107 1022 1077 ...
## $ X2ndFlrSF : int 854 0 866 756 1053 566 0 983 752 0 ...
## $ GrLivArea : int 1710 1262 1786 1717 2198 1362 1694 2090 1774 1077 ...
## $ FullBath : int 2 2 2 1 2 1 2 2 2 1 ...
## $ HalfBath : int 1 0 1 0 1 1 0 1 0 0 ...
## $ BedroomAbvGr: int 3 3 3 3 4 1 3 3 2 2 ...
## $ KitchenAbvGr: int 1 1 1 1 1 1 1 1 2 2 ...
## $ TotRmsAbvGrd: int 8 6 6 7 9 5 7 7 8 5 ...
## $ Fireplaces : int 0 1 1 1 1 0 1 2 2 2 ...
## $ GarageCars : int 2 2 2 3 3 2 2 2 2 1 ...
## $ GarageArea : int 548 460 608 642 836 480 636 484 468 205 ...
## $ YrSold : int 2008 2007 2008 2006 2008 2009 2007 2009 2008 2008 ...
## $ SalePrice : int 208500 181500 223500 140000 250000 143000 307000 200000 129900 118000 ...
#1. Please use the random seed 123 to divide the data into 75% training and 25% testing.
set.seed(123)
training.samples <- createDataPartition(housing$SalePrice, p = 0.75, list = FALSE)
train_data <- housing[training.samples, ]
test_data <- housing[-training.samples, ]
#2. Please find the best model using the stepwise variable selection method (based on the BIC criterion) using the training data. Please (a) display the coefficients of the fitted model; (b) make prediction on the testing data, and report the RMSE and the Coefficient of Determination R^2.
#a
#Linear model fitting with stepwise variable selection
model_stepwise <- lm(SalePrice ~ ., data = train_data)
final_model <- step(model_stepwise, direction = "both", k = log(nrow(train_data)))
## Start: AIC=22935.79
## SalePrice ~ Id + LotArea + OverallQual + OverallCond + YearBuilt +
## YearRemodAdd + CentralAir + X1stFlrSF + X2ndFlrSF + GrLivArea +
## FullBath + HalfBath + BedroomAbvGr + KitchenAbvGr + TotRmsAbvGrd +
## Fireplaces + GarageCars + GarageArea + YrSold
##
## Df Sum of Sq RSS AIC
## - GarageCars 1 1.8284e+08 1.1614e+12 22929
## - YrSold 1 6.1308e+08 1.1618e+12 22929
## - Id 1 1.0211e+09 1.1622e+12 22930
## - GrLivArea 1 1.6624e+09 1.1628e+12 22930
## - HalfBath 1 1.9541e+09 1.1631e+12 22931
## - YearRemodAdd 1 2.6438e+09 1.1638e+12 22931
## - Fireplaces 1 3.0769e+09 1.1642e+12 22932
## - X2ndFlrSF 1 4.0107e+09 1.1652e+12 22933
## <none> 1.1612e+12 22936
## - FullBath 1 9.7790e+09 1.1709e+12 22938
## - CentralAir 1 9.9825e+09 1.1712e+12 22938
## - TotRmsAbvGrd 1 1.0026e+10 1.1712e+12 22938
## - X1stFlrSF 1 1.2605e+10 1.1738e+12 22941
## - GarageArea 1 1.6134e+10 1.1773e+12 22944
## - OverallCond 1 3.5115e+10 1.1963e+12 22962
## - LotArea 1 3.6303e+10 1.1975e+12 22963
## - KitchenAbvGr 1 3.8261e+10 1.1994e+12 22964
## - BedroomAbvGr 1 5.9457e+10 1.2206e+12 22984
## - YearBuilt 1 9.1354e+10 1.2525e+12 23012
## - OverallQual 1 1.9499e+11 1.3562e+12 23099
##
## Step: AIC=22928.96
## SalePrice ~ Id + LotArea + OverallQual + OverallCond + YearBuilt +
## YearRemodAdd + CentralAir + X1stFlrSF + X2ndFlrSF + GrLivArea +
## FullBath + HalfBath + BedroomAbvGr + KitchenAbvGr + TotRmsAbvGrd +
## Fireplaces + GarageArea + YrSold
##
## Df Sum of Sq RSS AIC
## - YrSold 1 5.8517e+08 1.1619e+12 22923
## - Id 1 1.0481e+09 1.1624e+12 22923
## - GrLivArea 1 1.6988e+09 1.1631e+12 22924
## - HalfBath 1 2.0400e+09 1.1634e+12 22924
## - YearRemodAdd 1 2.5750e+09 1.1639e+12 22924
## - Fireplaces 1 2.9728e+09 1.1643e+12 22925
## - X2ndFlrSF 1 3.9683e+09 1.1653e+12 22926
## <none> 1.1614e+12 22929
## - TotRmsAbvGrd 1 9.9368e+09 1.1713e+12 22931
## - CentralAir 1 9.9685e+09 1.1713e+12 22931
## - FullBath 1 1.0080e+10 1.1714e+12 22931
## - X1stFlrSF 1 1.2540e+10 1.1739e+12 22934
## + GarageCars 1 1.8284e+08 1.1612e+12 22936
## - OverallCond 1 3.5389e+10 1.1967e+12 22955
## - LotArea 1 3.6209e+10 1.1976e+12 22956
## - GarageArea 1 3.7151e+10 1.1985e+12 22957
## - KitchenAbvGr 1 3.8804e+10 1.2002e+12 22958
## - BedroomAbvGr 1 5.9289e+10 1.2206e+12 22977
## - YearBuilt 1 9.1242e+10 1.2526e+12 23005
## - OverallQual 1 1.9572e+11 1.3571e+12 23093
##
## Step: AIC=22922.52
## SalePrice ~ Id + LotArea + OverallQual + OverallCond + YearBuilt +
## YearRemodAdd + CentralAir + X1stFlrSF + X2ndFlrSF + GrLivArea +
## FullBath + HalfBath + BedroomAbvGr + KitchenAbvGr + TotRmsAbvGrd +
## Fireplaces + GarageArea
##
## Df Sum of Sq RSS AIC
## - Id 1 1.0639e+09 1.1630e+12 22917
## - GrLivArea 1 1.6802e+09 1.1636e+12 22917
## - HalfBath 1 2.0698e+09 1.1640e+12 22918
## - YearRemodAdd 1 2.4109e+09 1.1643e+12 22918
## - Fireplaces 1 2.9192e+09 1.1649e+12 22918
## - X2ndFlrSF 1 4.0189e+09 1.1660e+12 22919
## <none> 1.1619e+12 22923
## - TotRmsAbvGrd 1 9.9514e+09 1.1719e+12 22925
## - CentralAir 1 9.9614e+09 1.1719e+12 22925
## - FullBath 1 1.0153e+10 1.1721e+12 22925
## - X1stFlrSF 1 1.2605e+10 1.1745e+12 22927
## + YrSold 1 5.8517e+08 1.1614e+12 22929
## + GarageCars 1 1.5493e+08 1.1618e+12 22929
## - OverallCond 1 3.5180e+10 1.1971e+12 22948
## - LotArea 1 3.6509e+10 1.1984e+12 22950
## - GarageArea 1 3.7306e+10 1.1992e+12 22950
## - KitchenAbvGr 1 3.9162e+10 1.2011e+12 22952
## - BedroomAbvGr 1 5.9275e+10 1.2212e+12 22970
## - YearBuilt 1 9.1804e+10 1.2537e+12 22999
## - OverallQual 1 1.9612e+11 1.3581e+12 23087
##
## Step: AIC=22916.52
## SalePrice ~ LotArea + OverallQual + OverallCond + YearBuilt +
## YearRemodAdd + CentralAir + X1stFlrSF + X2ndFlrSF + GrLivArea +
## FullBath + HalfBath + BedroomAbvGr + KitchenAbvGr + TotRmsAbvGrd +
## Fireplaces + GarageArea
##
## Df Sum of Sq RSS AIC
## - GrLivArea 1 1.8774e+09 1.1649e+12 22911
## - HalfBath 1 2.1432e+09 1.1651e+12 22912
## - YearRemodAdd 1 2.4782e+09 1.1655e+12 22912
## - Fireplaces 1 2.9670e+09 1.1660e+12 22912
## - X2ndFlrSF 1 3.7890e+09 1.1668e+12 22913
## <none> 1.1630e+12 22917
## - TotRmsAbvGrd 1 9.7895e+09 1.1728e+12 22919
## - CentralAir 1 9.9174e+09 1.1729e+12 22919
## - FullBath 1 1.0227e+10 1.1732e+12 22919
## - X1stFlrSF 1 1.2189e+10 1.1752e+12 22921
## + Id 1 1.0639e+09 1.1619e+12 22923
## + YrSold 1 6.0101e+08 1.1624e+12 22923
## + GarageCars 1 1.7970e+08 1.1628e+12 22923
## - OverallCond 1 3.4959e+10 1.1980e+12 22942
## - GarageArea 1 3.6900e+10 1.1999e+12 22944
## - LotArea 1 3.7099e+10 1.2001e+12 22944
## - KitchenAbvGr 1 3.8798e+10 1.2018e+12 22946
## - BedroomAbvGr 1 5.9664e+10 1.2227e+12 22964
## - YearBuilt 1 9.2247e+10 1.2552e+12 22993
## - OverallQual 1 1.9645e+11 1.3594e+12 23081
##
## Step: AIC=22911.29
## SalePrice ~ LotArea + OverallQual + OverallCond + YearBuilt +
## YearRemodAdd + CentralAir + X1stFlrSF + X2ndFlrSF + FullBath +
## HalfBath + BedroomAbvGr + KitchenAbvGr + TotRmsAbvGrd + Fireplaces +
## GarageArea
##
## Df Sum of Sq RSS AIC
## - HalfBath 1 2.0115e+09 1.1669e+12 22906
## - YearRemodAdd 1 2.6644e+09 1.1675e+12 22907
## - Fireplaces 1 2.8099e+09 1.1677e+12 22907
## <none> 1.1649e+12 22911
## - CentralAir 1 9.8930e+09 1.1748e+12 22914
## - FullBath 1 9.9591e+09 1.1748e+12 22914
## - TotRmsAbvGrd 1 1.1301e+10 1.1762e+12 22915
## + GrLivArea 1 1.8774e+09 1.1630e+12 22917
## + Id 1 1.2611e+09 1.1636e+12 22917
## + YrSold 1 5.8256e+08 1.1643e+12 22918
## + GarageCars 1 2.2085e+08 1.1647e+12 22918
## - OverallCond 1 3.3998e+10 1.1989e+12 22936
## - LotArea 1 3.7034e+10 1.2019e+12 22939
## - GarageArea 1 3.7453e+10 1.2023e+12 22939
## - KitchenAbvGr 1 4.0916e+10 1.2058e+12 22942
## - BedroomAbvGr 1 6.0331e+10 1.2252e+12 22960
## - YearBuilt 1 9.0557e+10 1.2554e+12 22986
## - X2ndFlrSF 1 1.8769e+11 1.3526e+12 23068
## - OverallQual 1 1.9854e+11 1.3634e+12 23077
## - X1stFlrSF 1 4.2093e+11 1.5858e+12 23243
##
## Step: AIC=22906.18
## SalePrice ~ LotArea + OverallQual + OverallCond + YearBuilt +
## YearRemodAdd + CentralAir + X1stFlrSF + X2ndFlrSF + FullBath +
## BedroomAbvGr + KitchenAbvGr + TotRmsAbvGrd + Fireplaces +
## GarageArea
##
## Df Sum of Sq RSS AIC
## - Fireplaces 1 2.5070e+09 1.1694e+12 22902
## - YearRemodAdd 1 2.6377e+09 1.1695e+12 22902
## <none> 1.1669e+12 22906
## - FullBath 1 8.0173e+09 1.1749e+12 22907
## - CentralAir 1 9.9056e+09 1.1768e+12 22909
## - TotRmsAbvGrd 1 1.1118e+10 1.1780e+12 22910
## + HalfBath 1 2.0115e+09 1.1649e+12 22911
## + GrLivArea 1 1.7456e+09 1.1651e+12 22912
## + Id 1 1.3307e+09 1.1656e+12 22912
## + YrSold 1 6.1313e+08 1.1663e+12 22913
## + GarageCars 1 3.1191e+08 1.1666e+12 22913
## - OverallCond 1 3.3894e+10 1.2008e+12 22931
## - LotArea 1 3.7537e+10 1.2044e+12 22934
## - GarageArea 1 3.7997e+10 1.2049e+12 22934
## - KitchenAbvGr 1 4.1028e+10 1.2079e+12 22937
## - BedroomAbvGr 1 5.9865e+10 1.2268e+12 22954
## - YearBuilt 1 9.1300e+10 1.2582e+12 22982
## - OverallQual 1 1.9928e+11 1.3662e+12 23072
## - X2ndFlrSF 1 2.2400e+11 1.3909e+12 23092
## - X1stFlrSF 1 4.1892e+11 1.5858e+12 23236
##
## Step: AIC=22901.54
## SalePrice ~ LotArea + OverallQual + OverallCond + YearBuilt +
## YearRemodAdd + CentralAir + X1stFlrSF + X2ndFlrSF + FullBath +
## BedroomAbvGr + KitchenAbvGr + TotRmsAbvGrd + GarageArea
##
## Df Sum of Sq RSS AIC
## - YearRemodAdd 1 1.9916e+09 1.1714e+12 22896
## <none> 1.1694e+12 22902
## - FullBath 1 8.2979e+09 1.1777e+12 22902
## - CentralAir 1 8.9376e+09 1.1783e+12 22903
## - TotRmsAbvGrd 1 1.1181e+10 1.1806e+12 22905
## + Fireplaces 1 2.5070e+09 1.1669e+12 22906
## + HalfBath 1 1.7085e+09 1.1677e+12 22907
## + GrLivArea 1 1.6117e+09 1.1678e+12 22907
## + Id 1 1.3655e+09 1.1680e+12 22907
## + YrSold 1 5.6072e+08 1.1688e+12 22908
## + GarageCars 1 1.7343e+08 1.1692e+12 22908
## - OverallCond 1 3.4291e+10 1.2037e+12 22926
## - GarageArea 1 3.7167e+10 1.2066e+12 22929
## - LotArea 1 4.1366e+10 1.2108e+12 22933
## - KitchenAbvGr 1 4.3838e+10 1.2132e+12 22935
## - BedroomAbvGr 1 6.3470e+10 1.2329e+12 22953
## - YearBuilt 1 9.1019e+10 1.2604e+12 22977
## - OverallQual 1 2.0888e+11 1.3783e+12 23075
## - X2ndFlrSF 1 2.4529e+11 1.4147e+12 23103
## - X1stFlrSF 1 4.7434e+11 1.6437e+12 23268
##
## Step: AIC=22896.4
## SalePrice ~ LotArea + OverallQual + OverallCond + YearBuilt +
## CentralAir + X1stFlrSF + X2ndFlrSF + FullBath + BedroomAbvGr +
## KitchenAbvGr + TotRmsAbvGrd + GarageArea
##
## Df Sum of Sq RSS AIC
## - FullBath 1 7.4074e+09 1.1788e+12 22896
## <none> 1.1714e+12 22896
## - CentralAir 1 9.0714e+09 1.1805e+12 22898
## - TotRmsAbvGrd 1 1.1966e+10 1.1834e+12 22901
## + YearRemodAdd 1 1.9916e+09 1.1694e+12 22902
## + Fireplaces 1 1.8608e+09 1.1695e+12 22902
## + GrLivArea 1 1.7794e+09 1.1696e+12 22902
## + HalfBath 1 1.7234e+09 1.1697e+12 22902
## + Id 1 1.4386e+09 1.1699e+12 22902
## + YrSold 1 4.1749e+08 1.1710e+12 22903
## + GarageCars 1 1.3111e+08 1.1713e+12 22903
## - GarageArea 1 3.6781e+10 1.2082e+12 22923
## - LotArea 1 4.1197e+10 1.2126e+12 22927
## - KitchenAbvGr 1 4.5133e+10 1.2165e+12 22931
## - OverallCond 1 4.7140e+10 1.2185e+12 22933
## - BedroomAbvGr 1 6.9403e+10 1.2408e+12 22953
## - YearBuilt 1 1.2725e+11 1.2986e+12 23003
## - OverallQual 1 2.1765e+11 1.3890e+12 23076
## - X2ndFlrSF 1 2.4686e+11 1.4183e+12 23099
## - X1stFlrSF 1 4.7547e+11 1.6469e+12 23263
##
## Step: AIC=22896.32
## SalePrice ~ LotArea + OverallQual + OverallCond + YearBuilt +
## CentralAir + X1stFlrSF + X2ndFlrSF + BedroomAbvGr + KitchenAbvGr +
## TotRmsAbvGrd + GarageArea
##
## Df Sum of Sq RSS AIC
## - CentralAir 1 7.3715e+09 1.1862e+12 22896
## <none> 1.1788e+12 22896
## + FullBath 1 7.4074e+09 1.1714e+12 22896
## - TotRmsAbvGrd 1 1.2228e+10 1.1910e+12 22901
## + Fireplaces 1 2.2355e+09 1.1766e+12 22901
## + GrLivArea 1 1.5806e+09 1.1772e+12 22902
## + Id 1 1.4271e+09 1.1774e+12 22902
## + YearRemodAdd 1 1.1011e+09 1.1777e+12 22902
## + YrSold 1 4.8764e+08 1.1783e+12 22903
## + GarageCars 1 2.9586e+08 1.1785e+12 22903
## + HalfBath 1 3.7392e+07 1.1788e+12 22903
## - GarageArea 1 3.6460e+10 1.2153e+12 22923
## - LotArea 1 4.1286e+10 1.2201e+12 22927
## - OverallCond 1 4.6195e+10 1.2250e+12 22932
## - KitchenAbvGr 1 5.4264e+10 1.2331e+12 22939
## - BedroomAbvGr 1 7.7274e+10 1.2561e+12 22959
## - YearBuilt 1 1.2185e+11 1.3006e+12 22997
## - OverallQual 1 2.1115e+11 1.3900e+12 23070
## - X2ndFlrSF 1 2.4375e+11 1.4225e+12 23096
## - X1stFlrSF 1 4.7505e+11 1.6538e+12 23261
##
## Step: AIC=22896.15
## SalePrice ~ LotArea + OverallQual + OverallCond + YearBuilt +
## X1stFlrSF + X2ndFlrSF + BedroomAbvGr + KitchenAbvGr + TotRmsAbvGrd +
## GarageArea
##
## Df Sum of Sq RSS AIC
## <none> 1.1862e+12 22896
## + CentralAir 1 7.3715e+09 1.1788e+12 22896
## + FullBath 1 5.7074e+09 1.1805e+12 22898
## - TotRmsAbvGrd 1 1.2247e+10 1.1984e+12 22900
## + GrLivArea 1 1.6128e+09 1.1846e+12 22902
## + Id 1 1.3781e+09 1.1848e+12 22902
## + Fireplaces 1 1.3516e+09 1.1848e+12 22902
## + YearRemodAdd 1 1.2806e+09 1.1849e+12 22902
## + YrSold 1 4.7839e+08 1.1857e+12 22903
## + GarageCars 1 2.8436e+08 1.1859e+12 22903
## + HalfBath 1 1.1220e+08 1.1861e+12 22903
## - GarageArea 1 3.5374e+10 1.2215e+12 22921
## - OverallCond 1 3.9455e+10 1.2256e+12 22925
## - LotArea 1 4.0357e+10 1.2265e+12 22926
## - KitchenAbvGr 1 4.9364e+10 1.2355e+12 22934
## - BedroomAbvGr 1 8.0195e+10 1.2664e+12 22961
## - YearBuilt 1 1.1617e+11 1.3023e+12 22992
## - OverallQual 1 2.1235e+11 1.3985e+12 23070
## - X2ndFlrSF 1 2.4684e+11 1.4330e+12 23097
## - X1stFlrSF 1 4.7263e+11 1.6588e+12 23257
summary(final_model) #to see fitted model coefficients
##
## Call:
## lm(formula = SalePrice ~ LotArea + OverallQual + OverallCond +
## YearBuilt + X1stFlrSF + X2ndFlrSF + BedroomAbvGr + KitchenAbvGr +
## TotRmsAbvGrd + GarageArea, data = train_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -124802 -17854 -1674 15227 245317
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.017e+06 9.199e+04 -11.060 < 2e-16 ***
## LotArea 7.340e-01 1.207e-01 6.079 1.68e-09 ***
## OverallQual 1.667e+04 1.196e+03 13.943 < 2e-16 ***
## OverallCond 5.943e+03 9.889e+02 6.010 2.53e-09 ***
## YearBuilt 4.813e+02 4.667e+01 10.313 < 2e-16 ***
## X1stFlrSF 1.013e+02 4.872e+00 20.802 < 2e-16 ***
## X2ndFlrSF 6.478e+01 4.309e+00 15.033 < 2e-16 ***
## BedroomAbvGr -1.488e+04 1.737e+03 -8.569 < 2e-16 ***
## KitchenAbvGr -3.343e+04 4.972e+03 -6.723 2.88e-11 ***
## TotRmsAbvGrd 4.361e+03 1.302e+03 3.349 0.00084 ***
## GarageArea 3.560e+01 6.255e+00 5.691 1.62e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 33050 on 1086 degrees of freedom
## Multiple R-squared: 0.8354, Adjusted R-squared: 0.8339
## F-statistic: 551.3 on 10 and 1086 DF, p-value: < 2.2e-16
#b
# Testing data predictions
predictions <- predict(final_model, newdata = test_data)
# Calculate RMSE and R squared
rmse <- sqrt(mean((test_data$SalePrice - predictions)^2))
rsquared <- cor(predictions, test_data$SalePrice)^2
# Results
cat("RMSE:", rmse, "\n")
## RMSE: 48727.68
cat("R-squared:", rsquared, "\n")
## R-squared: 0.6601152
#3. Please find the best model using the best subset variable selection method (based on the SSE criterion) using the training data. Please (a) display the coefficients of the fitted model; (b) make prediction on the testing data, and report the RMSE and the Coefficient of Determination R^2.
#a
#best subset model using SSE criterion
subset_model_best <- regsubsets(SalePrice ~ ., data = train_data, nvmax = ncol(train_data) - 1)
#best model
model_best <- lm(SalePrice ~ ., data = train_data, subset = summary(subset_model_best)$which)
#fitted model coefficients
coef(model_best)
## (Intercept) Id LotArea OverallQual OverallCond
## -2.234432e+06 -2.612776e+00 6.160787e-01 1.615894e+04 5.175653e+03
## YearBuilt YearRemodAdd CentralAirY X1stFlrSF X2ndFlrSF
## 5.786136e+02 1.126390e+02 -1.569307e+04 8.142702e+00 -2.212100e+01
## GrLivArea FullBath HalfBath BedroomAbvGr KitchenAbvGr
## 9.379140e+01 -1.356923e+04 -4.767534e+03 -1.284438e+04 -3.314398e+04
## TotRmsAbvGrd Fireplaces GarageCars GarageArea YrSold
## 4.234533e+03 4.438993e+03 -6.942751e+01 4.163596e+01 4.160558e+02
#b
#testing data predictions
predictions <- predict(model_best, newdata = test_data)
# Calculate RMSE and R-squared
rmse <- sqrt(mean((test_data$SalePrice - predictions)^2))
rsquared <- cor(predictions, test_data$SalePrice)^2
#Results
cat("RMSE:", rmse, "\n")
## RMSE: 49228.29
cat("R-squared:", rsquared, "\n")
## R-squared: 0.6523816
#4. Which model selection method among the 2 we have used above is the best? (a) Please compare the BIC of these models using the training data, as well as display these two models so we can see the parameter estimators and model goodness of fit measures. (b) Furthermore, please compare the RMSE and R^2 of these models using the test data. (c) Please discuss any modifications you can do to further improve your model(s).
#a
# Calculate BIC for stepwise model
bic_stepwise <- BIC(final_model)
cat("BIC for Stepwise Model:", bic_stepwise, "\n")
## BIC for Stepwise Model: 26016.31
# Calculate BIC for best subset model
bic_best_subset <- BIC(model_best)
cat("BIC for Best Subset Model:", bic_best_subset, "\n")
## BIC for Best Subset Model: 14540.77
# Compare coefficients of the two models
cat("Coefficients of Stepwise Model:\n")
## Coefficients of Stepwise Model:
summary(final_model)$coefficients
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.017464e+06 9.199295e+04 -11.060236 5.091518e-27
## LotArea 7.339645e-01 1.207458e-01 6.078593 1.676128e-09
## OverallQual 1.667461e+04 1.195886e+03 13.943316 9.015197e-41
## OverallCond 5.943478e+03 9.888844e+02 6.010286 2.525186e-09
## YearBuilt 4.812917e+02 4.666702e+01 10.313315 7.417226e-24
## X1stFlrSF 1.013470e+02 4.872016e+00 20.801855 3.708537e-81
## X2ndFlrSF 6.477953e+01 4.309140e+00 15.033053 1.523294e-46
## BedroomAbvGr -1.488137e+04 1.736707e+03 -8.568727 3.545142e-17
## KitchenAbvGr -3.342536e+04 4.971977e+03 -6.722750 2.875423e-11
## TotRmsAbvGrd 4.361293e+03 1.302444e+03 3.348546 8.402521e-04
## GarageArea 3.559750e+01 6.255146e+00 5.690914 1.624567e-08
cat("\nCoefficients of Best Subset Model:\n")
##
## Coefficients of Best Subset Model:
coef(model_best)
## (Intercept) Id LotArea OverallQual OverallCond
## -2.234432e+06 -2.612776e+00 6.160787e-01 1.615894e+04 5.175653e+03
## YearBuilt YearRemodAdd CentralAirY X1stFlrSF X2ndFlrSF
## 5.786136e+02 1.126390e+02 -1.569307e+04 8.142702e+00 -2.212100e+01
## GrLivArea FullBath HalfBath BedroomAbvGr KitchenAbvGr
## 9.379140e+01 -1.356923e+04 -4.767534e+03 -1.284438e+04 -3.314398e+04
## TotRmsAbvGrd Fireplaces GarageCars GarageArea YrSold
## 4.234533e+03 4.438993e+03 -6.942751e+01 4.163596e+01 4.160558e+02
#b
# Testing data predictions for stepwise model
predictions_stepwise <- predict(model_stepwise, newdata = test_data)
# Calculate RMSE and R-squared for stepwise model
rmse_stepwise <- sqrt(mean((test_data$SalePrice - predictions_stepwise)^2))
rsquared_stepwise <- cor(predictions_stepwise, test_data$SalePrice)^2
# Results for stepwise model
cat("RMSE (Stepwise Model):", rmse_stepwise, "\n")
## RMSE (Stepwise Model): 49437.98
cat("R-squared (Stepwise Model):", rsquared_stepwise, "\n")
## R-squared (Stepwise Model): 0.6549383
# Testing data predictions for best subset model
predictions_best_subset <- predict(model_best, newdata = test_data)
# RMSE and R-squared for best subset model
rmse_best_subset <- sqrt(mean((test_data$SalePrice - predictions_best_subset)^2))
rsquared_best_subset <- cor(predictions_best_subset, test_data$SalePrice)^2
# Results
cat("RMSE (Best Subset Model):", rmse_best_subset, "\n")
## RMSE (Best Subset Model): 49228.29
cat("R-squared (Best Subset Model):", rsquared_best_subset, "\n")
## R-squared (Best Subset Model): 0.6523816
#c
#To enhance the predictive performance of the models, several modifications can be considered. Firstly, feature engineering may be explored to create new relevant variables or transform existing ones. This could involve extracting insights from variables like 'YearBuilt' or creating interaction terms between certain features. Additionally, outlier detection and treatment methods may be applied to address extreme values that could adversely impact model fitting. Regularization techniques, such as ridge or lasso regression, could also be employed to prevent overfitting and enhance model generalization. Furthermore, cross-validation can be utilized during the variable selection process to ensure the robustness of the chosen features. Experimenting with different algorithms and ensemble methods might also provide insights into whether alternative modeling approaches yield better results. Finally, continuous refinement and validation of the model against new data are crucial for ensuring its ongoing accuracy and relevance
#------------------------------------------Question 2---------------------------------------------
#1. For the entire dataset, please perform the data cleaning as instructed before; namely, exclude the variables PassengerID, Name, Ticket, and Cabin and delete missing values in the variable Age. Please report how many passengers are left after this step.
# Load the Titanic data
titanic_data <- read.csv("C:/Users/sukkumar/Downloads/Titanic2.csv")
# Data cleaning
# Exclude variables: PassengerId, Name, Ticket, Cabin
titanic_cleaned <- titanic_data[, !(names(titanic_data) %in% c("PassengerId", "Name", "Ticket", "Cabin"))]
# Remove rows with missing values in the Age variable
titanic_cleaned <- titanic_cleaned[!is.na(titanic_cleaned$Age), ]
# Report the number of passengers left
num_passengers_left <- nrow(titanic_cleaned)
print(paste("Number of passengers after data cleaning:", num_passengers_left))
## [1] "Number of passengers after data cleaning: 714"
#2. Please use the “str()” function to examine the variable type in our data set. Please note that R will recognize a character valued function as categorical variable (namely a Factor variable) automatically and therefore you do not need to transform this type of variables. However, you do need to recode the numerical valued categorical variables using the “as.factor()” function so that R can recognize these as categoricalvariables. Now, please recode the “Survived” and the “Pclass” variables as factors.
# Recode "Survived" and "Pclass" as factors
titanic_cleaned$Survived <- as.factor(titanic_cleaned$Survived)
titanic_cleaned$Pclass <- as.factor(titanic_cleaned$Pclass)
# Examine the variable types using str() function
str(titanic_cleaned)
## 'data.frame': 714 obs. of 8 variables:
## $ Survived: Factor w/ 2 levels "0","1": 1 2 2 2 1 1 1 2 2 2 ...
## $ Pclass : Factor w/ 3 levels "1","2","3": 3 1 3 1 3 1 3 3 2 3 ...
## $ Sex : chr "male" "female" "female" "female" ...
## $ Age : num 22 38 26 35 35 54 2 27 14 4 ...
## $ SibSp : int 1 1 0 1 0 0 3 0 1 1 ...
## $ Parch : int 0 0 0 0 0 0 1 2 0 1 ...
## $ Fare : num 7.25 71.28 7.92 53.1 8.05 ...
## $ Embarked: chr "S" "C" "S" "S" ...
#3. Now please use the random seed 123 to divide the cleaned data into 80% training and 20% testing.
# Set random seed for reproducibility
set.seed(123)
# Divide the cleaned data into 80% training and 20% testing
index <- sample(1:nrow(titanic_cleaned), 0.8 * nrow(titanic_cleaned))
train_data <- titanic_cleaned[index, ]
test_data <- titanic_cleaned[-index, ]
#4. Then you shall fit a logistic regression model with all the other 7 predictors using the training data.
# Fit logistic regression model with all other 7 predictors using training data
logistic_model <- glm(Survived ~ Pclass + Sex + Age + SibSp + Parch + Fare + Embarked,
data = train_data,
family = binomial)
# Display a summary of the logistic regression model
summary(logistic_model)
##
## Call:
## glm(formula = Survived ~ Pclass + Sex + Age + SibSp + Parch +
## Fare + Embarked, family = binomial, data = train_data)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 4.040623 0.573169 7.050 1.79e-12 ***
## Pclass2 -1.090644 0.363805 -2.998 0.00272 **
## Pclass3 -2.212574 0.367180 -6.026 1.68e-09 ***
## Sexmale -2.563812 0.242178 -10.586 < 2e-16 ***
## Age -0.038177 0.008893 -4.293 1.76e-05 ***
## SibSp -0.331146 0.139801 -2.369 0.01785 *
## Parch -0.073210 0.130597 -0.561 0.57509
## Fare 0.001500 0.002645 0.567 0.57060
## EmbarkedQ -0.633477 0.629029 -1.007 0.31390
## EmbarkedS -0.391929 0.299565 -1.308 0.19076
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 766.59 on 570 degrees of freedom
## Residual deviance: 519.49 on 561 degrees of freedom
## AIC: 539.49
##
## Number of Fisher Scoring iterations: 5
#5. Please use this fitted model based on the training data to predict the response variable ‘’Survived” (whether the subject survived or not) for the testing data. Please generate the confusion matrix, and report:
# Predict the response variable for the testing data
predictions <- predict(logistic_model, newdata = test_data, type = "response")
# Convert probabilities to binary predictions (0 or 1)
binary_predictions <- ifelse(predictions > 0.5, 1, 0)
# Create confusion matrix
conf_matrix <- table(Actual = test_data$Survived, Predicted = binary_predictions)
# Calculate overall accuracy
accuracy <- sum(diag(conf_matrix)) / sum(conf_matrix)
# Calculate sensitivity (true positive rate)
sensitivity <- conf_matrix[2, 2] / sum(conf_matrix[2, ])
# Calculate specificity (true negative rate)
specificity <- conf_matrix[1, 1] / sum(conf_matrix[1, ])
# Display the confusion matrix and performance metrics
conf_matrix
## Predicted
## Actual 0 1
## 0 73 6
## 1 18 46
cat("\nOverall Accuracy:", accuracy, "\n")
##
## Overall Accuracy: 0.8321678
cat("Sensitivity:", sensitivity, "\n")
## Sensitivity: 0.71875
cat("Specificity:", specificity, "\n")
## Specificity: 0.9240506
#6. Now we have recovered the record of an additional passenger as follows. Please predict whether this passenger has survived or not.
# Define the additional passenger's data
new_passenger <- data.frame(
PassengerId = 892,
Pclass = 3,
Name = "Mr. Unknown",
Sex = "male",
Age = 23,
SibSp = 0,
Parch = 0,
Ticket = "123456",
Fare = 8.25,
Cabin = NA,
Embarked = "Q"
)
# Convert Pclass and Sex to factor variables
new_passenger$Pclass <- as.factor(new_passenger$Pclass)
new_passenger$Sex <- as.factor(new_passenger$Sex)
# Predict whether the additional passenger has survived or not
prediction <- predict(logistic_model, newdata = new_passenger, type = "response")
# Convert probability to binary prediction (0 or 1)
predicted_class <- ifelse(prediction > 0.5, 1, 0)
# Display whether the additional passenger survived or not
if (predicted_class == 1) {
cat("The additional passenger (Passenger ID 892) is predicted to have survived.\n")
} else {
cat("The additional passenger (Passenger ID 892) is predicted to have not survived.\n")
}
## The additional passenger (Passenger ID 892) is predicted to have not survived.