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
district_data<-read_excel("district.xls")
district_model1<-lm(DDA00A001222R~DPETALLC+DA0912DR21R+DPSTKIDR,data=district_data)
plot(district_model1,which=1)
This model is not linear by visual inspection.
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
durbinWatsonTest(district_model1)
## lag Autocorrelation D-W Statistic p-value
## 1 0.01494427 1.968301 0.596
## Alternative hypothesis: rho != 0
The models errors are mostly independent per the Durbin Watson Test. P-value is more than .05.
plot(district_model1,which=3)
bptest(district_model1)
##
## studentized Breusch-Pagan test
##
## data: district_model1
## BP = 5.275, df = 3, p-value = 0.1527
The model is homoscedastic per the bptest, because the p-value is more than .05.
plot(district_model1,which=2)
Model is normal based on visual inspectation because variables are
plotted on the line.
vif(district_model1)
## DPETALLC DA0912DR21R DPSTKIDR
## 1.094141 1.008119 1.097962
The assumption not met is linearity.
district_model_log<-lm(sqrt(DDA00A001222R)~sqrt(DPETALLC)+sqrt(DA0912DR21R)+sqrt(DPSTKIDR),data=district_data)
## Warning in sqrt(DA0912DR21R): NaNs produced
## Warning in sqrt(DPSTKIDR): NaNs produced
plot(district_model_log,which=1)
Model was log-transformed to help with linearity but the final result
was still no linear.