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(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
library(MASS)
##
## Attaching package: 'MASS'
##
## The following object is masked from 'package:dplyr':
##
## select
district<-read_excel("district (2).xls")
district_model1<-lm(DA0CSA21R~DA0AT21R+DPETECOP+DPFRASTAP,data=district)
#testing linearity
plot(district_model1,which=1)
This model is not linear by visual inspection
durbinWatsonTest(district_model1)
## lag Autocorrelation D-W Statistic p-value
## 1 0.1499926 1.69975 0
## Alternative hypothesis: rho != 0
The model’s errors are not independent per the Durbinwatson test
plot(district_model1,which=3)
bptest(district_model1)
##
## studentized Breusch-Pagan test
##
## data: district_model1
## BP = 2.5201, df = 3, p-value = 0.4717
This model is considered homoskedastic per bptest
plot(district_model1,which=2)
Model is not normal by visual inspection
vif(district_model1)
## DA0AT21R DPETECOP DPFRASTAP
## 1.170578 1.200046 1.028933
The assumptions not met are linearity, independence of errors, and Normality of residuals.
district_model_log<-lm(log(DA0CSA21R)~log(DA0AT21R)+log(DPETECOP)+log(DPFRASTAP),data=district)
## Warning in log(DA0CSA21R): NaNs produced
## Warning in log(DA0AT21R): NaNs produced
colSums(district[, c("DA0CSA21R", "DA0AT21R",
"DPETECOP", "DPFRASTAP")] <= 0,
na.rm = TRUE)
## DA0CSA21R DA0AT21R DPETECOP DPFRASTAP
## 165 2 4 0
summary(district$DA0CSA21R)
## Min. 1st Qu. Median Mean 3rd Qu. Max. NAs
## -1.0 887.0 973.0 823.9 1039.0 1344.0 262
summary(district$DA0AT21R)
## Min. 1st Qu. Median Mean 3rd Qu. Max. NAs
## -1.00 94.05 95.40 94.76 96.40 100.00 4
summary(district$DPETECOP)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.00 47.95 61.90 60.75 77.15 100.00
district_model_asinh <- lm(
asinh(DA0CSA21R) ~ asinh(DA0AT21R) +
asinh(DPETECOP) + asinh(DPFRASTAP),
data = district
)
plot(district_model_asinh, which = 1)
This is still not linear after i tried log but got errors because of the
negative data or zeros so i looked up how to fix it and it gave me asinh
and used that and it still did not fix the data. So still not
linear.
district_model_rlm<-rlm(DA0CSA21R~DA0AT21R+DPETECOP+DPFRASTAP,data=district)
durbinWatsonTest(district_model_rlm)
## lag Autocorrelation D-W Statistic p-value
## 1 0.2573303 1.485327 0
## Alternative hypothesis: rho != 0
The robust linear model was used to transform the data and the final result still are non independent.
district_model_log <- lm(
log(DA0CSA21R) ~ log(DA0AT21R) +
log(DPETECOP) + log(DPFRASTAP),
data = district
)
## Warning in log(DA0CSA21R): NaNs produced
## Warning in log(DA0AT21R): NaNs produced
shapiro.test(district_model_log$residuals)
##
## Shapiro-Wilk normality test
##
## data: district_model_log$residuals
## W = 0.9983, p-value = 0.6511
district_model_asinh <- lm(
asinh(DA0CSA21R) ~ asinh(DA0AT21R) +
asinh(DPETECOP) + asinh(DPFRASTAP),
data = district
)
shapiro.test(district_model_asinh$residuals)
##
## Shapiro-Wilk normality test
##
## data: district_model_asinh$residuals
## W = 0.55466, p-value < 2.2e-16
plot(district_model_asinh, which = 2)
The residuals are still not normal