library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.1 ✔ readr 2.2.0
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.3 ✔ tibble 3.3.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.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(readxl)
library(MASS)
##
## Attaching package: 'MASS'
##
## The following object is masked from 'package:dplyr':
##
## select
library(lmtest)
## Loading required package: zoo
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(car)
## Loading required package: carData
##
## Attaching package: 'car'
##
## The following object is masked from 'package:dplyr':
##
## recode
##
## The following object is masked from 'package:purrr':
##
## some
cities <- read_xlsx("City_data.xlsx")
cities_model1 <- lm(tax_property_city
~educ_elem_sec_city+public_welfare_city+police_city+housing_commdevt_city,data=cities)
plot(cities_model1,which=1)
model appears to be linear upon visual inspection
durbinWatsonTest(cities_model1)
## lag Autocorrelation D-W Statistic p-value
## 1 0.9448159 0.1102798 0
## Alternative hypothesis: rho != 0
Errors in this model are not independent per DW test as P value is below 0.05 and D-W is far from 2
plot(cities_model1,which=3)
bptest(cities_model1)
##
## studentized Breusch-Pagan test
##
## data: cities_model1
## BP = 2129.8, df = 4, p-value < 2.2e-16
model is heteroscedastistic
plot(cities_model1,which=2)
Errors do not appear normal upon visual inspection. Shapiro-Wilk test
failed due to number of variables
vif(cities_model1)
## educ_elem_sec_city public_welfare_city police_city
## 1.128350 1.376716 1.212610
## housing_commdevt_city
## 1.352939
no multicollearity in this model as all values are below 5
The assumptions not met are independence of errors, homoscedancity, and normality of errors
sqrt_cities_model1 <- lm(sqrt(tax_property_city)~sqrt(educ_elem_sec_city)+sqrt(public_welfare_city)+sqrt(police_city)+sqrt(housing_commdevt_city),data=cities)
cities_model1_changed<-rlm(tax_property_city~educ_elem_sec_city+public_welfare_city+police_city+housing_commdevt_city,data=cities)
durbinWatsonTest(cities_model1_changed)
## lag Autocorrelation D-W Statistic p-value
## 1 0.9511 0.09774841 0
## Alternative hypothesis: rho != 0
errors in the model remain not independent
plot(sqrt_cities_model1,which=3)
bptest(sqrt_cities_model1)
##
## studentized Breusch-Pagan test
##
## data: sqrt_cities_model1
## BP = 951.71, df = 4, p-value < 2.2e-16
model remains heteroscedastistic
plot(sqrt_cities_model1,which=2)
errors are more normal when square rooted