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(tidyverse)
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
library(readxl)
district<-read_excel("district.xls")
district_data <-district
district_data<-lm(DA0GR21N~DPSTURNR+DPSTTOSA+DPSTADFP,data=district)
plot(district_data,which=1)

raintest(district_data)
## 
##  Rainbow test
## 
## data:  district_data
## Rain = 0.79774, df1 = 539, df2 = 535, p-value = 0.9955
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_data)
##  lag Autocorrelation D-W Statistic p-value
##    1       0.1061114       1.78761   0.012
##  Alternative hypothesis: rho != 0
plot(district_data,which=3)

bptest(district_data)
## 
##  studentized Breusch-Pagan test
## 
## data:  district_data
## BP = 37.448, df = 3, p-value = 3.7e-08
shapiro.test(district_data$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  district_data$residuals
## W = 0.55805, p-value < 2.2e-16
district_data_1_robust<-rlm(DA0GR21N~DPSTADFP,data=district)


summary(district_data_1_robust)
## 
## Call: rlm(formula = DA0GR21N ~ DPSTADFP, data = district)
## Residuals:
##      Min       1Q   Median       3Q      Max 
##  -271.82   -71.75   -28.74    98.40 11464.69 
## 
## Coefficients:
##             Value   Std. Error t value
## (Intercept) 30.5710 10.3930     2.9415
## DPSTADFP     3.9803  0.4554     8.7397
## 
## Residual standard error: 111.6 on 1077 degrees of freedom
##   (128 observations deleted due to missingness)
summary(district_data_1_robust)
## 
## Call: rlm(formula = DA0GR21N ~ DPSTADFP, data = district)
## Residuals:
##      Min       1Q   Median       3Q      Max 
##  -271.82   -71.75   -28.74    98.40 11464.69 
## 
## Coefficients:
##             Value   Std. Error t value
## (Intercept) 30.5710 10.3930     2.9415
## DPSTADFP     3.9803  0.4554     8.7397
## 
## Residual standard error: 111.6 on 1077 degrees of freedom
##   (128 observations deleted due to missingness)
kitchen_sink<-lm(DA0GR21N~DPSTURNR+DPSTTOSA+DPSTADFP,data=district)
vif(kitchen_sink)
## DPSTURNR DPSTTOSA DPSTADFP 
## 1.051593 1.188830 1.135042
plot(district_data,which=2)

district_clean<-district %>% drop_na()

district_data_log<-lm(log(DA0GR21N)~log(DPSTADFP),data=district_clean)

plot(district_data_log,which=2)

summary(district_data_log)
## 
## Call:
## lm(formula = log(DA0GR21N) ~ log(DPSTADFP), data = district_clean)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.7864 -0.7354 -0.0380  0.7469  3.3715 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    0.07486    0.75668   0.099    0.921    
## log(DPSTADFP)  1.87755    0.23819   7.883 5.04e-14 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.166 on 321 degrees of freedom
## Multiple R-squared:  0.1622, Adjusted R-squared:  0.1596 
## F-statistic: 62.14 on 1 and 321 DF,  p-value: 5.043e-14