library(MASS)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following object is masked from 'package:MASS':
##
## select
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.1 ✔ readr 2.2.0
## ✔ ggplot2 4.0.3 ✔ stringr 1.6.0
## ✔ lubridate 1.9.5 ✔ tibble 3.3.1
## ✔ purrr 1.2.2 ✔ tidyr 1.3.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ✖ dplyr::select() masks MASS::select()
## ℹ 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:purrr':
##
## some
##
## The following object is masked from 'package:dplyr':
##
## recode
library(pastecs)
##
## Attaching package: 'pastecs'
##
## The following object is masked from 'package:tidyr':
##
## extract
##
## The following objects are masked from 'package:dplyr':
##
## first, last
cities <- read_xlsx("City_data.xlsx")
cities_clean <- cities |> select(year,tax_property_city,education_city,public_welfare_city,police_city,housing_commdevt_city)
cities_1977 <- cities_clean %>% filter(year == 1977)
cities_1987 <- cities_clean %>% filter(year == 1987)
cities_1997 <- cities_clean %>% filter(year == 1997)
cities_2007 <- cities_clean %>% filter(year == 2007)
cities_2017 <- cities_clean %>% filter(year == 2017)
summary(cities_1977)
## year tax_property_city education_city public_welfare_city
## Min. :1977 Min. : 27.5 Min. : 0.000 Min. : 0.00
## 1st Qu.:1977 1st Qu.: 270.7 1st Qu.: 1.295 1st Qu.: 0.00
## Median :1977 Median : 418.5 Median : 29.540 Median : 0.00
## Mean :1977 Mean : 610.0 Mean : 320.940 Mean : 54.25
## 3rd Qu.:1977 3rd Qu.: 646.3 3rd Qu.: 56.110 3rd Qu.: 10.29
## Max. :1977 Max. :3557.1 Max. :3021.520 Max. :2389.51
## police_city housing_commdevt_city
## Min. : 2.05 Min. : 0.000
## 1st Qu.: 167.78 1st Qu.: 5.115
## Median : 216.83 Median : 50.370
## Mean : 237.35 Mean : 105.598
## 3rd Qu.: 272.90 3rd Qu.: 135.765
## Max. :1043.49 Max. :1780.100
summary(cities_1987)
## year tax_property_city education_city public_welfare_city
## Min. :1987 Min. : 18.67 Min. : 0.00 Min. : 0.00
## 1st Qu.:1987 1st Qu.: 243.10 1st Qu.: 0.00 1st Qu.: 0.00
## Median :1987 Median : 406.03 Median : 30.37 Median : 0.00
## Mean :1987 Mean : 561.59 Mean : 318.42 Mean : 45.65
## 3rd Qu.:1987 3rd Qu.: 642.10 3rd Qu.: 68.84 3rd Qu.: 10.21
## Max. :1987 Max. :4415.05 Max. :3237.24 Max. :2345.46
## police_city housing_commdevt_city
## Min. : 0.0 Min. : 0.00
## 1st Qu.: 198.2 1st Qu.: 36.72
## Median : 242.3 Median : 68.46
## Mean : 273.6 Mean : 120.78
## 3rd Qu.: 310.9 3rd Qu.: 143.69
## Max. :1542.2 Max. :1188.63
summary(cities_1997)
## year tax_property_city education_city public_welfare_city
## Min. :1997 Min. : 42.48 Min. : 0.00 Min. : 0.00
## 1st Qu.:1997 1st Qu.: 246.92 1st Qu.: 0.00 1st Qu.: 0.00
## Median :1997 Median : 417.50 Median : 30.30 Median : 0.00
## Mean :1997 Mean : 614.21 Mean : 396.16 Mean : 56.47
## 3rd Qu.:1997 3rd Qu.: 676.73 3rd Qu.: 72.22 3rd Qu.: 10.28
## Max. :1997 Max. :4874.84 Max. :3730.54 Max. :3935.83
## police_city housing_commdevt_city
## Min. : 0.07 Min. : 0.00
## 1st Qu.: 239.96 1st Qu.: 43.08
## Median : 305.17 Median : 85.82
## Mean : 328.19 Mean : 129.58
## 3rd Qu.: 369.53 3rd Qu.: 167.57
## Max. :1760.59 Max. :1286.84
summary(cities_2007)
## year tax_property_city education_city public_welfare_city
## Min. :2007 Min. : 0.0 Min. : 0.00 Min. : 0.00
## 1st Qu.:2007 1st Qu.: 343.8 1st Qu.: 0.00 1st Qu.: 0.00
## Median :2007 Median : 555.0 Median : 34.54 Median : 0.00
## Mean :2007 Mean : 747.1 Mean : 530.67 Mean : 60.40
## 3rd Qu.:2007 3rd Qu.: 828.5 3rd Qu.: 79.30 3rd Qu.: 10.29
## Max. :2007 Max. :5511.4 Max. :5379.66 Max. :5565.58
## police_city housing_commdevt_city
## Min. : 27.55 Min. : 0.00
## 1st Qu.: 294.75 1st Qu.: 45.12
## Median : 368.79 Median : 91.11
## Mean : 401.59 Mean : 144.59
## 3rd Qu.: 441.84 3rd Qu.: 189.77
## Max. :1466.31 Max. :1155.68
summary(cities_2017)
## year tax_property_city education_city public_welfare_city
## Min. :2017 Min. : 36.52 Min. : 0.00 Min. : 0.00
## 1st Qu.:2017 1st Qu.: 331.10 1st Qu.: 0.00 1st Qu.: 0.00
## Median :2017 Median : 523.42 Median : 29.99 Median : 0.00
## Mean :2017 Mean : 755.77 Mean : 527.02 Mean : 66.24
## 3rd Qu.:2017 3rd Qu.: 816.14 3rd Qu.: 75.60 3rd Qu.: 10.47
## Max. :2017 Max. :4593.64 Max. :5423.53 Max. :6804.10
## police_city housing_commdevt_city
## Min. : 21.49 Min. : 0.00
## 1st Qu.: 299.81 1st Qu.: 26.77
## Median : 380.69 Median : 61.97
## Mean : 408.74 Mean : 119.36
## 3rd Qu.: 478.71 3rd Qu.: 112.14
## Max. :1205.10 Max. :1786.42
stat.desc(cities_1977$education_city)
## nbr.val nbr.null nbr.na min max range
## 218.00000 46.00000 0.00000 0.00000 3021.52000 3021.52000
## sum median mean SE.mean CI.mean.0.95 var
## 69964.86000 29.54000 320.93972 44.33673 87.38576 428532.61937
## std.dev coef.var
## 654.62403 2.03971
stat.desc(cities_1987$education_city)
## nbr.val nbr.null nbr.na min max range
## 2.180000e+02 6.100000e+01 0.000000e+00 0.000000e+00 3.237240e+03 3.237240e+03
## sum median mean SE.mean CI.mean.0.95 var
## 6.941656e+04 3.037000e+01 3.184246e+02 4.492732e+01 8.854979e+01 4.400252e+05
## std.dev coef.var
## 6.633439e+02 2.083206e+00
stat.desc(cities_1997$education_city)
## nbr.val nbr.null nbr.na min max range
## 2.180000e+02 7.000000e+01 0.000000e+00 0.000000e+00 3.730540e+03 3.730540e+03
## sum median mean SE.mean CI.mean.0.95 var
## 8.636208e+04 3.030500e+01 3.961563e+02 5.740698e+01 1.131467e+02 7.184324e+05
## std.dev coef.var
## 8.476039e+02 2.139569e+00
stat.desc(cities_2007$education_city)
## nbr.val nbr.null nbr.na min max range
## 2.180000e+02 7.300000e+01 0.000000e+00 0.000000e+00 5.379660e+03 5.379660e+03
## sum median mean SE.mean CI.mean.0.95 var
## 1.156857e+05 3.454000e+01 5.306685e+02 7.972859e+01 1.571416e+02 1.385749e+06
## std.dev coef.var
## 1.177179e+03 2.218294e+00
stat.desc(cities_2017$education_city)
## nbr.val nbr.null nbr.na min max range
## 2.180000e+02 7.700000e+01 0.000000e+00 0.000000e+00 5.423530e+03 5.423530e+03
## sum median mean SE.mean CI.mean.0.95 var
## 1.148911e+05 2.999000e+01 5.270233e+02 8.137217e+01 1.603810e+02 1.443472e+06
## std.dev coef.var
## 1.201446e+03 2.279682e+00
stat.desc(cities_1977$tax_property_city)
## nbr.val nbr.null nbr.na min max range
## 2.180000e+02 0.000000e+00 0.000000e+00 2.750000e+01 3.557140e+03 3.529640e+03
## sum median mean SE.mean CI.mean.0.95 var
## 1.329720e+05 4.184600e+02 6.099634e+02 3.929111e+01 7.744107e+01 3.365466e+05
## std.dev coef.var
## 5.801264e+02 9.510838e-01
stat.desc(cities_1987$tax_property_city)
## nbr.val nbr.null nbr.na min max range
## 2.180000e+02 0.000000e+00 0.000000e+00 1.867000e+01 4.415050e+03 4.396380e+03
## sum median mean SE.mean CI.mean.0.95 var
## 1.224263e+05 4.060300e+02 5.615884e+02 3.718417e+01 7.328837e+01 3.014204e+05
## std.dev coef.var
## 5.490177e+02 9.776157e-01
stat.desc(cities_1997$tax_property_city)
## nbr.val nbr.null nbr.na min max range
## 2.180000e+02 0.000000e+00 0.000000e+00 4.248000e+01 4.874840e+03 4.832360e+03
## sum median mean SE.mean CI.mean.0.95 var
## 1.338976e+05 4.174950e+02 6.142092e+02 4.327397e+01 8.529110e+01 4.082347e+05
## std.dev coef.var
## 6.389325e+02 1.040252e+00
stat.desc(cities_2007$tax_property_city)
## nbr.val nbr.null nbr.na min max range
## 2.180000e+02 1.000000e+00 0.000000e+00 0.000000e+00 5.511430e+03 5.511430e+03
## sum median mean SE.mean CI.mean.0.95 var
## 1.628695e+05 5.549750e+02 7.471079e+02 4.893462e+01 9.644800e+01 5.220222e+05
## std.dev coef.var
## 7.225110e+02 9.670772e-01
stat.desc(cities_2017$tax_property_city)
## nbr.val nbr.null nbr.na min max range
## 2.180000e+02 0.000000e+00 0.000000e+00 3.652000e+01 4.593640e+03 4.557120e+03
## sum median mean SE.mean CI.mean.0.95 var
## 1.647572e+05 5.234200e+02 7.557671e+02 5.151576e+01 1.015353e+02 5.785443e+05
## std.dev coef.var
## 7.606210e+02 1.006423e+00
cities_model77 <- lm(tax_property_city~education_city+public_welfare_city+police_city+housing_commdevt_city,data=cities_1977)
summary(cities_model77)
##
## Call:
## lm(formula = tax_property_city ~ education_city + public_welfare_city +
## police_city + housing_commdevt_city, data = cities_1977)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1385.20 -176.26 -40.67 133.62 1421.52
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 250.53238 61.95305 4.044 7.35e-05 ***
## education_city 0.68837 0.03912 17.597 < 2e-16 ***
## public_welfare_city -0.04617 0.11740 -0.393 0.6945
## police_city 0.53037 0.24740 2.144 0.0332 *
## housing_commdevt_city 0.14323 0.13428 1.067 0.2873
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 344.8 on 213 degrees of freedom
## Multiple R-squared: 0.6532, Adjusted R-squared: 0.6467
## F-statistic: 100.3 on 4 and 213 DF, p-value: < 2.2e-16
plot(cities_model77,which=1)

plot(cities_model77,which=2)

plot(cities_model77,which=3)

durbinWatsonTest(cities_model77)
## lag Autocorrelation D-W Statistic p-value
## 1 0.2739975 1.433816 0
## Alternative hypothesis: rho != 0
bptest(cities_model77)
##
## studentized Breusch-Pagan test
##
## data: cities_model77
## BP = 75.637, df = 4, p-value = 1.461e-15
vif(cities_model77)
## education_city public_welfare_city police_city
## 1.196961 1.421523 1.290340
## housing_commdevt_city
## 1.034912
cities_model77_changed<-lm(sqrt(tax_property_city)~sqrt(education_city)+sqrt(public_welfare_city)+sqrt(police_city)+sqrt(housing_commdevt_city),data=cities_1977)
durbinWatsonTest(cities_model77_changed)
## lag Autocorrelation D-W Statistic p-value
## 1 0.4050859 1.184583 0
## Alternative hypothesis: rho != 0
plot(cities_model77_changed,which=1)

plot(cities_model77_changed,which=2)

plot(cities_model77_changed,which=3)

bptest(cities_model77_changed)
##
## studentized Breusch-Pagan test
##
## data: cities_model77_changed
## BP = 27.72, df = 4, p-value = 1.421e-05
cities_model87 <- lm(tax_property_city~education_city+public_welfare_city+police_city+housing_commdevt_city,data=cities_1987)
summary(cities_model87)
##
## Call:
## lm(formula = tax_property_city ~ education_city + public_welfare_city +
## police_city + housing_commdevt_city, data = cities_1987)
##
## Residuals:
## Min 1Q Median 3Q Max
## -791.82 -205.43 -10.65 133.25 1895.51
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -43.91189 56.64797 -0.775 0.439
## education_city 0.47880 0.03910 12.247 < 2e-16 ***
## public_welfare_city 0.05248 0.14530 0.361 0.718
## police_city 1.62805 0.18387 8.855 3.32e-16 ***
## housing_commdevt_city 0.04298 0.18174 0.236 0.813
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 342.5 on 213 degrees of freedom
## Multiple R-squared: 0.618, Adjusted R-squared: 0.6108
## F-statistic: 86.15 on 4 and 213 DF, p-value: < 2.2e-16
plot(cities_model87,which=1)

plot(cities_model87,which=2)

plot(cities_model87,which=3)

durbinWatsonTest(cities_model87)
## lag Autocorrelation D-W Statistic p-value
## 1 0.4288402 1.11578 0
## Alternative hypothesis: rho != 0
bptest(cities_model87)
##
## studentized Breusch-Pagan test
##
## data: cities_model87
## BP = 85.358, df = 4, p-value < 2.2e-16
vif(cities_model87)
## education_city public_welfare_city police_city
## 1.244188 1.591575 1.200765
## housing_commdevt_city
## 1.275819
cities_model87_changed<-lm(sqrt(tax_property_city)~sqrt(education_city)+sqrt(public_welfare_city)+sqrt(police_city)+sqrt(housing_commdevt_city),data=cities_1987)
plot(cities_model87_changed,which=1)

plot(cities_model87_changed,which=2)

plot(cities_model87_changed,which=3)

durbinWatsonTest(cities_model87_changed)
## lag Autocorrelation D-W Statistic p-value
## 1 0.5191628 0.9531299 0
## Alternative hypothesis: rho != 0
bptest(cities_model87_changed)
##
## studentized Breusch-Pagan test
##
## data: cities_model87_changed
## BP = 28.212, df = 4, p-value = 1.13e-05
cities_model97 <- lm(tax_property_city~education_city+public_welfare_city+police_city+housing_commdevt_city,data=cities_1997)
summary(cities_model97)
##
## Call:
## lm(formula = tax_property_city ~ education_city + public_welfare_city +
## police_city + housing_commdevt_city, data = cities_1997)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1317.76 -228.84 -39.57 162.72 2249.50
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -36.35314 69.58423 -0.522 0.602
## education_city 0.47042 0.03579 13.144 < 2e-16 ***
## public_welfare_city 0.05400 0.10775 0.501 0.617
## police_city 1.48659 0.19798 7.509 1.61e-12 ***
## housing_commdevt_city -0.20631 0.20345 -1.014 0.312
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 416.7 on 213 degrees of freedom
## Multiple R-squared: 0.5826, Adjusted R-squared: 0.5748
## F-statistic: 74.33 on 4 and 213 DF, p-value: < 2.2e-16
plot(cities_model97,which=1)

plot(cities_model97,which=2)

plot(cities_model97,which=3)

durbinWatsonTest(cities_model97)
## lag Autocorrelation D-W Statistic p-value
## 1 0.4154871 1.167328 0
## Alternative hypothesis: rho != 0
bptest(cities_model97)
##
## studentized Breusch-Pagan test
##
## data: cities_model97
## BP = 72.555, df = 4, p-value = 6.55e-15
vif(cities_model97)
## education_city public_welfare_city police_city
## 1.150409 1.280063 1.187908
## housing_commdevt_city
## 1.174920
cities_model97_changed<-lm(sqrt(tax_property_city)~sqrt(education_city)+sqrt(public_welfare_city)+sqrt(police_city)+sqrt(housing_commdevt_city),data=cities_1997)
plot(cities_model97_changed,which=1)

plot(cities_model97_changed,which=2)

plot(cities_model97_changed,which=3)

durbinWatsonTest(cities_model97_changed)
## lag Autocorrelation D-W Statistic p-value
## 1 0.4774381 1.040648 0
## Alternative hypothesis: rho != 0
bptest(cities_model97_changed)
##
## studentized Breusch-Pagan test
##
## data: cities_model97_changed
## BP = 27.218, df = 4, p-value = 1.796e-05
cities_model07 <- lm(tax_property_city~education_city+public_welfare_city+police_city+housing_commdevt_city,data=cities_2007)
summary(cities_model07)
##
## Call:
## lm(formula = tax_property_city ~ education_city + public_welfare_city +
## police_city + housing_commdevt_city, data = cities_2007)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1482.3 -258.9 -50.7 144.1 3304.8
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 31.30764 84.10430 0.372 0.7101
## education_city 0.34251 0.02884 11.878 < 2e-16 ***
## public_welfare_city 0.14316 0.09703 1.475 0.1416
## police_city 1.12857 0.22129 5.100 7.5e-07 ***
## housing_commdevt_city 0.49915 0.26415 1.890 0.0602 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 477.8 on 213 degrees of freedom
## Multiple R-squared: 0.5707, Adjusted R-squared: 0.5626
## F-statistic: 70.79 on 4 and 213 DF, p-value: < 2.2e-16
plot(cities_model07,which=1)

plot(cities_model07,which=2)

plot(cities_model07,which=3)

durbinWatsonTest(cities_model07)
## lag Autocorrelation D-W Statistic p-value
## 1 0.2702364 1.451296 0
## Alternative hypothesis: rho != 0
bptest(cities_model07)
##
## studentized Breusch-Pagan test
##
## data: cities_model07
## BP = 51.23, df = 4, p-value = 1.998e-10
vif(cities_model07)
## education_city public_welfare_city police_city
## 1.095129 1.415018 1.490881
## housing_commdevt_city
## 1.717106
cities_model07_changed<-lm(sqrt(tax_property_city)~sqrt(education_city)+sqrt(public_welfare_city)+sqrt(police_city)+sqrt(housing_commdevt_city),data=cities_2007)
plot(cities_model07_changed,which=1)

plot(cities_model07_changed,which=2)

plot(cities_model07_changed,which=3)

durbinWatsonTest(cities_model07_changed)
## lag Autocorrelation D-W Statistic p-value
## 1 0.3894611 1.210991 0
## Alternative hypothesis: rho != 0
bptest(cities_model07_changed)
##
## studentized Breusch-Pagan test
##
## data: cities_model07_changed
## BP = 15.014, df = 4, p-value = 0.004673
cities_model17 <- lm(tax_property_city~education_city+public_welfare_city+police_city+housing_commdevt_city,data=cities_2017)
summary(cities_model17)
##
## Call:
## lm(formula = tax_property_city ~ education_city + public_welfare_city +
## police_city + housing_commdevt_city, data = cities_2017)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1887.36 -255.02 -39.54 187.78 2263.12
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 164.97339 91.06989 1.812 0.07147 .
## education_city 0.36046 0.03028 11.903 < 2e-16 ***
## public_welfare_city 0.25028 0.09033 2.771 0.00609 **
## police_city 0.86192 0.21718 3.969 9.88e-05 ***
## housing_commdevt_city 0.26759 0.20606 1.299 0.19549
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 501.1 on 213 degrees of freedom
## Multiple R-squared: 0.5739, Adjusted R-squared: 0.5659
## F-statistic: 71.73 on 4 and 213 DF, p-value: < 2.2e-16
plot(cities_model17,which=1)

plot(cities_model17,which=2)

plot(cities_model17,which=3)

durbinWatsonTest(cities_model17)
## lag Autocorrelation D-W Statistic p-value
## 1 0.2422 1.508341 0
## Alternative hypothesis: rho != 0
bptest(cities_model17)
##
## studentized Breusch-Pagan test
##
## data: cities_model17
## BP = 48.005, df = 4, p-value = 9.415e-10
vif(cities_model17)
## education_city public_welfare_city police_city
## 1.143862 1.676622 1.209420
## housing_commdevt_city
## 1.672429
cities_model17_changed<-lm(sqrt(tax_property_city)~sqrt(education_city)+sqrt(public_welfare_city)+sqrt(police_city)+sqrt(housing_commdevt_city),data=cities_2017)
plot(cities_model17_changed,which=1)

plot(cities_model17_changed,which=2)

plot(cities_model17_changed,which=3)

durbinWatsonTest(cities_model17_changed)
## lag Autocorrelation D-W Statistic p-value
## 1 0.3628853 1.26271 0
## Alternative hypothesis: rho != 0
bptest(cities_model17_changed)
##
## studentized Breusch-Pagan test
##
## data: cities_model17_changed
## BP = 24.848, df = 4, p-value = 5.396e-05