library(tidyverse)
## ── 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.2 ✔ tibble 3.3.0
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.1.0
## ── 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(MASS)
##
## Attaching package: 'MASS'
##
## The following object is masked from 'package:dplyr':
##
## select
setwd("~/Desktop/UTSA/Quantitative Methods/RStudio")
district <- read_excel("district.xls")
clean_district<-district |> drop_na()
district_model<-lm(DA0GR21N~DPSTURNR+DPFRAALLK, data=clean_district)
plot(district_model,which=1)
raintest(district_model)
##
## Rainbow test
##
## data: district_model
## Rain = 0.67354, df1 = 162, df2 = 158, p-value = 0.9936
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_model)
## lag Autocorrelation D-W Statistic p-value
## 1 0.1361684 1.725153 0.03
## Alternative hypothesis: rho != 0
plot(district_model,which=3)
bptest(district_model)
##
## studentized Breusch-Pagan test
##
## data: district_model
## BP = 2.3293, df = 2, p-value = 0.312
plot(district_model,which=2)
shapiro.test(district_model$residuals)
##
## Shapiro-Wilk normality test
##
## data: district_model$residuals
## W = 0.67851, p-value < 2.2e-16
kitchen_sink<-lm(DA0GR21N~DPSTURNR+DPFRAALLK+DPSTTOSA+DPSAKIDR+DPSTKIDR+DPFVTOTK+DPFRAOPRT+DPFEAOPFT+DPFEAOPFK+DPFEAINSP,data=clean_district)
vif(kitchen_sink)
## DPSTURNR DPFRAALLK DPSTTOSA DPSAKIDR DPSTKIDR DPFVTOTK DPFRAOPRT
## 1.270614 3.483985 1.492601 1.664667 1.656170 2.223679 255.080687
## DPFEAOPFT DPFEAOPFK DPFEAINSP
## 257.769209 2.662505 1.737183
does your model meet those assumptions? You don’t have to be perfectly right, just make a good case. ANSWER: Overall, I think my model meets most of the assumptions, like linearity and equal variance, but not all of them perfectly. The errors aren’t completely independent or normally distributed, and a few variables are highly correlated. Possibly suggesting that my model could be improved by removing or adjusting some predictors?
If your model violates an assumption, which one? ANSWER: My model violates the assumption of normality. The residuals aren’t normally distributed, which means the errors don’t follow the typical bell-shaped curve. This could slightly affect how accurate the model’s predictions and p-values are.
What would you do to mitigate this assumption? Show your work. ANSWER: I applied a log transformation to the dependent variable to make the residuals more normally distributed.
plot(district_model, which = 2)
district_model_log <- lm(log(DA0GR21N) ~ DPSTURNR + DPFRAALLK, data = clean_district)
plot(district_model_log, which = 2)
shapiro.test(residuals(district_model_log))
##
## Shapiro-Wilk normality test
##
## data: residuals(district_model_log)
## W = 0.99289, p-value = 0.1283