Using the Mississippi police expenditure data:
library(spData)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tmap)
library(tmaptools)
library(sf)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(sp)
library(spgwr)
## Warning: package 'spgwr' was built under R version 4.0.3
## NOTE: This package does not constitute approval of GWR
## as a method of spatial analysis; see example(gwr)
library(dplyr)
library(spdep)
## Warning: package 'spdep' was built under R version 4.0.3
police.sf = st_read ("police")
## Reading layer `police' from data source `C:\R\police' using driver `ESRI Shapefile'
## Simple feature collection with 82 features and 21 fields
## geometry type: POLYGON
## dimension: XY
## bbox: xmin: -91.64356 ymin: 30.19474 xmax: -88.09043 ymax: 35.00496
## CRS: NA
pPOLICE as the police expenditure per person by dividing POLICE by POP and a new variable NONWHITE as 100 minus WHITE.pPolice = police.sf$POLICE/police.sf$POP
NONWHITE = 100 - police.sf$WHITE
pPOLICE onto CRIME.crime.lm <- lm(pPolice ~ police.sf$CRIME)
summary((crime.lm))
##
## Call:
## lm(formula = pPolice ~ police.sf$CRIME)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.0140364 -0.0047745 -0.0008891 0.0046232 0.0168849
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.109e-02 1.011e-03 20.867 < 2e-16 ***
## police.sf$CRIME 1.861e-05 3.490e-06 5.331 8.8e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.00678 on 80 degrees of freedom
## Multiple R-squared: 0.2622, Adjusted R-squared: 0.2529
## F-statistic: 28.42 on 1 and 80 DF, p-value: 8.803e-07
style = "W" to define the weights.police.sp <- as(police.sf, "Spatial")
nbs <- poly2nb(police.sp)
wts <- nb2listw(nbs)
m <- length(nbs)
s <- Szero(wts)
moran(police.sp$CRIME,
listw = wts,
n = m,
S0 = s)
## $I
## [1] 0.1035887
##
## $K
## [1] 33.26095
geary(police.sp$CRIME,
listw = wts,
n = m,
S0 = s,
n1 = m - 1)
## $C
## [1] 0.7813509
##
## $K
## [1] 33.26095
lm.LMtests(crime.lm,
listw = wts,
test = c("LMerr", "LMlag"))
##
## Lagrange multiplier diagnostics for spatial dependence
##
## data:
## model: lm(formula = pPolice ~ police.sf$CRIME)
## weights: wts
##
## LMerr = 6.0467, df = 1, p-value = 0.01393
##
##
## Lagrange multiplier diagnostics for spatial dependence
##
## data:
## model: lm(formula = pPolice ~ police.sf$CRIME)
## weights: wts
##
## LMlag = 10.483, df = 1, p-value = 0.001205
library("spatialreg")
## Warning: package 'spatialreg' was built under R version 4.0.3
## Loading required package: Matrix
## Registered S3 methods overwritten by 'spatialreg':
## method from
## residuals.stsls spdep
## deviance.stsls spdep
## coef.stsls spdep
## print.stsls spdep
## summary.stsls spdep
## print.summary.stsls spdep
## residuals.gmsar spdep
## deviance.gmsar spdep
## coef.gmsar spdep
## fitted.gmsar spdep
## print.gmsar spdep
## summary.gmsar spdep
## print.summary.gmsar spdep
## print.lagmess spdep
## summary.lagmess spdep
## print.summary.lagmess spdep
## residuals.lagmess spdep
## deviance.lagmess spdep
## coef.lagmess spdep
## fitted.lagmess spdep
## logLik.lagmess spdep
## fitted.SFResult spdep
## print.SFResult spdep
## fitted.ME_res spdep
## print.ME_res spdep
## print.lagImpact spdep
## plot.lagImpact spdep
## summary.lagImpact spdep
## HPDinterval.lagImpact spdep
## print.summary.lagImpact spdep
## print.sarlm spdep
## summary.sarlm spdep
## residuals.sarlm spdep
## deviance.sarlm spdep
## coef.sarlm spdep
## vcov.sarlm spdep
## fitted.sarlm spdep
## logLik.sarlm spdep
## anova.sarlm spdep
## predict.sarlm spdep
## print.summary.sarlm spdep
## print.sarlm.pred spdep
## as.data.frame.sarlm.pred spdep
## residuals.spautolm spdep
## deviance.spautolm spdep
## coef.spautolm spdep
## fitted.spautolm spdep
## print.spautolm spdep
## summary.spautolm spdep
## logLik.spautolm spdep
## print.summary.spautolm spdep
## print.WXImpact spdep
## summary.WXImpact spdep
## print.summary.WXImpact spdep
## predict.SLX spdep
##
## Attaching package: 'spatialreg'
## The following objects are masked from 'package:spdep':
##
## anova.sarlm, as.spam.listw, as_dgRMatrix_listw, as_dsCMatrix_I,
## as_dsCMatrix_IrW, as_dsTMatrix_listw, bptest.sarlm, can.be.simmed,
## cheb_setup, coef.gmsar, coef.sarlm, coef.spautolm, coef.stsls,
## create_WX, deviance.gmsar, deviance.sarlm, deviance.spautolm,
## deviance.stsls, do_ldet, eigen_pre_setup, eigen_setup, eigenw,
## errorsarlm, fitted.gmsar, fitted.ME_res, fitted.sarlm,
## fitted.SFResult, fitted.spautolm, get.ClusterOption,
## get.coresOption, get.mcOption, get.VerboseOption,
## get.ZeroPolicyOption, GMargminImage, GMerrorsar, griffith_sone,
## gstsls, Hausman.test, HPDinterval.lagImpact, impacts, intImpacts,
## Jacobian_W, jacobianSetup, l_max, lagmess, lagsarlm, lextrB,
## lextrS, lextrW, lmSLX, logLik.sarlm, logLik.spautolm, LR.sarlm,
## LR1.sarlm, LR1.spautolm, LU_prepermutate_setup, LU_setup,
## Matrix_J_setup, Matrix_setup, mcdet_setup, MCMCsamp, ME, mom_calc,
## mom_calc_int2, moments_setup, powerWeights, predict.sarlm,
## predict.SLX, print.gmsar, print.ME_res, print.sarlm,
## print.sarlm.pred, print.SFResult, print.spautolm, print.stsls,
## print.summary.gmsar, print.summary.sarlm, print.summary.spautolm,
## print.summary.stsls, residuals.gmsar, residuals.sarlm,
## residuals.spautolm, residuals.stsls, sacsarlm, SE_classic_setup,
## SE_interp_setup, SE_whichMin_setup, set.ClusterOption,
## set.coresOption, set.mcOption, set.VerboseOption,
## set.ZeroPolicyOption, similar.listw, spam_setup, spam_update_setup,
## SpatialFiltering, spautolm, spBreg_err, spBreg_lag, spBreg_sac,
## stsls, subgraph_eigenw, summary.gmsar, summary.sarlm,
## summary.spautolm, summary.stsls, trW, vcov.sarlm, Wald1.sarlm
crime.lag <- lagsarlm(pPolice ~ police.sf$CRIME,
data = police.sp,
listw = wts)
summary(crime.lag)
##
## Call:lagsarlm(formula = pPolice ~ police.sf$CRIME, data = police.sp,
## listw = wts)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.0121713 -0.0041669 -0.0011300 0.0044230 0.0174000
##
## Type: lag
## Coefficients: (asymptotic standard errors)
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.1420e-02 3.1761e-03 3.5955 0.0003237
## police.sf$CRIME 1.6700e-05 3.2451e-06 5.1462 2.658e-07
##
## Rho: 0.40788, LR test value: 8.8641, p-value: 0.0029084
## Asymptotic standard error: 0.12721
## z-value: 3.2064, p-value: 0.0013439
## Wald statistic: 10.281, p-value: 0.0013439
##
## Log likelihood: 298.5801 for lag model
## ML residual variance (sigma squared): 3.8804e-05, (sigma: 0.0062293)
## Number of observations: 82
## Number of parameters estimated: 4
## AIC: -589.16, (AIC for lm: -582.3)
## LM test for residual autocorrelation
## test value: 0.58817, p-value: 0.44313
NONWHITE to the non-spatial regression. Is it a significant term in the model? Test the residuals from the new model for spatial autocorrelation. What do you conclude?crime2.lm <- lm(pPolice ~ police.sf$CRIME+NONWHITE)
summary((crime2.lm))
##
## Call:
## lm(formula = pPolice ~ police.sf$CRIME + NONWHITE)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.0131364 -0.0040887 -0.0006579 0.0041658 0.0157054
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.701e-02 1.780e-03 9.556 8.02e-15 ***
## police.sf$CRIME 1.831e-05 3.358e-06 5.453 5.49e-07 ***
## NONWHITE 1.087e-04 3.971e-05 2.738 0.00764 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.006521 on 79 degrees of freedom
## Multiple R-squared: 0.3261, Adjusted R-squared: 0.309
## F-statistic: 19.11 on 2 and 79 DF, p-value: 1.697e-07
lm.LMtests(crime2.lm,
listw = wts,
test = c("LMerr", "LMlag"))
##
## Lagrange multiplier diagnostics for spatial dependence
##
## data:
## model: lm(formula = pPolice ~ police.sf$CRIME + NONWHITE)
## weights: wts
##
## LMerr = 0.97127, df = 1, p-value = 0.3244
##
##
## Lagrange multiplier diagnostics for spatial dependence
##
## data:
## model: lm(formula = pPolice ~ police.sf$CRIME + NONWHITE)
## weights: wts
##
## LMlag = 5.6268, df = 1, p-value = 0.01769