1. Load your preferred dataset into R studio
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()
  1. Create a linear model “lm()” from the variables, with a continuous dependent variable as the outcome
district_model<-lm(DA0GR21N~DPSTURNR+DPFRAALLK, data=clean_district)
  1. Check the following assumptions:
  1. Linearity (plot and raintest)
plot(district_model,which=1)

raintest(district_model)
## 
##  Rainbow test
## 
## data:  district_model
## Rain = 0.67354, df1 = 162, df2 = 158, p-value = 0.9936
  1. Independence of errors (durbin-watson)
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
  1. Homoscedasticity (plot, bptest)
plot(district_model,which=3)

bptest(district_model)
## 
##  studentized Breusch-Pagan test
## 
## data:  district_model
## BP = 2.3293, df = 2, p-value = 0.312
  1. Normality of residuals (QQ plot, shapiro test)
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
  1. No multicolinarity (VIF, cor)
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
  1. 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?

  2. 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.

  3. 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