library(tidyverse)
library(readxl)
library(ggplot2)
library(MASS)
library(lmtest)
library(car)

district <- read_excel("district.xls")

district_clean <- district %>%
  filter(!is.na(DA0CC23R), DA0CC23R >= 0)

district_data <- lm(
  DA0CC23R ~ DPSTTOSA + DPSTKIDR +
    DPSTURNR + DPFPAREGP,
  data = district_clean
)

summary(district_data)
## 
## Call:
## lm(formula = DA0CC23R ~ DPSTTOSA + DPSTKIDR + DPSTURNR + DPFPAREGP, 
##     data = district_clean)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -33.394 -10.466  -1.653   8.201  57.137 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -3.876e+00  5.119e+00  -0.757    0.449    
## DPSTTOSA     1.168e-04  8.387e-05   1.393    0.164    
## DPSTKIDR    -1.047e-01  8.196e-02  -1.278    0.202    
## DPSTURNR    -3.308e-01  5.088e-02  -6.502 1.24e-10 ***
## DPFPAREGP    8.024e-01  6.089e-02  13.176  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 14.44 on 1014 degrees of freedom
##   (4 observations deleted due to missingness)
## Multiple R-squared:  0.1959, Adjusted R-squared:  0.1927 
## F-statistic: 61.74 on 4 and 1014 DF,  p-value: < 2.2e-16
district_clean <- lm(DA0CC23R ~ DPSTTOSA + DPSTKIDR + DPSTURNR + DPFPAREGP, data = district)
plot(district_clean, which = 1)

This model is linear based on visual inspection.

durbinWatsonTest(district_clean)
##  lag Autocorrelation D-W Statistic p-value
##    1      0.08886798      1.821967   0.008
##  Alternative hypothesis: rho != 0

The model’s errors are not independent per the Durbin-Watson test.

# Testing for homoscedasticity
plot(district_clean, which = 3)

bptest(district_clean)
## 
##  studentized Breusch-Pagan test
## 
## data:  district_clean
## BP = 61.44, df = 4, p-value = 1.445e-12

The model as a p-value above 0.05, so this model is homoscedastic.

# Normality of errors
plot(district_clean, which = 2)

The model is normal by visual inspection, because items are on the line.

# Check Variance Inflation Factors
vif(district_clean)
##  DPSTTOSA  DPSTKIDR  DPSTURNR DPFPAREGP 
##  1.090832  1.091655  1.022546  1.075049

The model is without multicollinearity, because variance inflation factors are all under 10.


The assumption that the model did not meet would be the Durbin-Watson test. The model is not independent per the Durbin-Watson test.

model_log <- lm(sqrt(DA0CC23R) ~ sqrt(DPSTTOSA) + sqrt(DPSTKIDR) + sqrt(DPSTURNR) + sqrt(DPFPAREGP), data = district)
## Warning in sqrt(DA0CC23R): NaNs produced
plot(model_log)

Model was log-transformed to help with independence, but final results did not improve the model.

model_rlm <- rlm(DA0CC23R ~ DPSTTOSA + DPSTKIDR + DPSTURNR + DPFPAREGP, data = district)

summary(model_rlm)
## 
## Call: rlm(formula = DA0CC23R ~ DPSTTOSA + DPSTKIDR + DPSTURNR + DPFPAREGP, 
##     data = district)
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -59.2076 -10.2336  -0.7486   9.3301  59.2273 
## 
## Coefficients:
##             Value   Std. Error t value
## (Intercept) -3.1830  4.9856    -0.6385
## DPSTTOSA     0.0001  0.0001     1.2094
## DPSTKIDR     0.1416  0.0818     1.7312
## DPSTURNR    -0.3948  0.0484    -8.1528
## DPFPAREGP    0.7151  0.0594    12.0486
## 
## Residual standard error: 14.43 on 1065 degrees of freedom
##   (137 observations deleted due to missingness)

Robust linear model was used to transform the data, to no effect, the final results show minimal impact of influential outliers.

# Linearity
plot(district_data, which = 1)

# Independence
durbinWatsonTest(district_data)
##  lag Autocorrelation D-W Statistic p-value
##    1      0.07060998      1.858355   0.024
##  Alternative hypothesis: rho != 0
# Homoscedasticity
plot(district_data, which = 3)

bptest(district_data)
## 
##  studentized Breusch-Pagan test
## 
## data:  district_data
## BP = 37.361, df = 4, p-value = 1.518e-07
# Normality
plot(district_data, which = 2)

# Multicollinearity
vif(district_data)
##  DPSTTOSA  DPSTKIDR  DPSTURNR DPFPAREGP 
##  1.088434  1.093237  1.017736  1.082960