### ### ### Boston Data (MASS) - Multiple Regression (Simple version) ### ver 0.0.2 ### ################################################## ###------------------------------------------------- ###--- 0. Preliminary # Using Boston3 from https://nmimoto.github.io/R/boston0.txt library(MASS) # install.packages("MASS") library(tidyverse) # install.packages("tidyverse") Boston <- as_tibble(Boston) Boston # Rename "medv" column as "resp" to streamline analysis. Boston2 <- Boston %>% rename(resp=medv) Boston2 # move "resp" columnm to 1st Boston2 <- Boston2 %>% relocate(resp) Boston2 # turn "chas" column into 0/1 factor Boston3 <- Boston2 %>% mutate( chas=as.factor(chas) ) Boston3 # Importance List by Chisq test # top list: # 3 4 6 10 # "zn" "indus" "nox" "rad" # bottom list # c(7, 8, 9, 13) # "rm" "age" "dis" "black" #1 resp (dbl) <- used to be "medv". Response variable (Y). #2 crim (dbl) #3 zn (dbl) #4 indus (dbl) #5 chas (factor) #6 nox (dbl) #7 rm (dbl) #8 age (dbl) #9 dis (dbl) #10 rad (dbl) #11 tax (dbl) #12 ptratio (dbl) #13 black (dbl) #14 lstat (dbl) ###------------------------------------------------- ###--- 1. Multiple Linear Regression #- Regression with all variables Reg01 <- lm(resp ~ . , data=Boston3) summary(Reg01) plot(Reg01) #- Backward stepwise regression Reg11 <- lm(resp ~ . -age, data=Boston3) summary(Reg11) Reg12 <- lm(resp ~ . -age -indus, data=Boston3) summary(Reg12) plot(Reg12) # diagnostic plot #- Forward stepwise regression Reg04 <- lm(resp ~ crim + zn + indus, data=Boston3) summary(Reg04) #- Start with TOP 4 Reg04 <- lm(resp ~ zn + indus + nox + rad, data=Boston3) summary(Reg04) Reg04 <- lm(resp ~ zn + indus + rad, data=Boston3) summary(Reg04) #--- Looking at the result str(Reg04) # click on environment tab in Rstudio Reg04$residuals plot(Reg04$residuals) Reg04$fitted.values # Yhat plot(Reg04$fitted.values) plot(Reg04$fitted.values, Reg04$residuals) # same as 1st diagnostic plot plot(Reg04) # diagnostic plot