In [30]:
##==============================================================
## ëÊ5èÕ
##==============================================================
library(spdep)
library(maptools)
In [2]:
##==============================================================
data(oldcol)
names(COL.OLD)
Out[2]:
In [4]:
##==============================================================
COL.ols <- lm(CRIME ~ INC + HOVAL, data = COL.OLD)
summary(COL.ols)
Out[4]:
In [5]:
##==============================================================
W <- nb2listw(COL.nb, style = "W")
lm.LMtests(COL.ols, listw = W, test = c("LMlag", "LMerr"))
Out[5]:
In [6]:
##==============================================================
COL.ar <- lagsarlm(CRIME ~ 1, data = COL.OLD, listw = W,
method = "eigen")
class(COL.ar)
Out[6]:
In [7]:
##==============================================================
summary(COL.ar)
Out[7]:
In [8]:
##==============================================================
op <- par(no.readonly = TRUE)
par(mfrow = c(2, 1))
plot(COL.OLD$CRIME, type = "l")
lines(fitted(COL.ar), lty = 2)
legend("topright", c("Actual", "Predicted"), lty = 1:2,
bty = "n")
title(main = "Spatial AR Model Actual vs. Predicted")
plot(residuals(COL.ar), type = "l", xlab = "", ylab = "",
main = "Residuals")
par(op)
In [9]:
##==============================================================
COL.sar <- lagsarlm(CRIME ~ INC + HOVAL, data = COL.OLD,
listw = W, method = "eigen")
class(COL.sar)
Out[9]:
In [10]:
##==============================================================
summary(COL.sar)
Out[10]:
In [11]:
##==============================================================
op <- par(no.readonly = TRUE)
par(mfrow = c(2, 1))
plot(COL.OLD$CRIME, type = "l", main = "SEM Actual vs. Predicted")
lines(fitted(COL.sar), lty = 2)
legend("topright", c("Actual", "Predicted"), lty = 1:2,
bty = "n")
plot(residuals(COL.sar), type = "l", xlab = "", ylab = "",
main = "Residuals")
par(op)
In [12]:
##==============================================================
COL.sem <- errorsarlm(CRIME ~ INC + HOVAL, data = COL.OLD,
listw = W, method = "eigen")
In [13]:
##==============================================================
summary(COL.sem)
Out[13]:
In [14]:
##==============================================================
op <- par(no.readonly = TRUE)
par(mfrow = c(2, 1))
plot(COL.OLD$CRIME, type = "l", main = "SEM Actual vs. Predicted")
lines(fitted(COL.sem), lty = 2)
legend("topright", c("Actual", "Predicted"), lty = 1:2,
bty = "n")
plot(residuals(COL.sem), type = "l", xlab = "", ylab = "",
main = "Residuals")
par(op)
In [15]:
##==============================================================
COL.sdm <- lagsarlm(CRIME ~ INC + HOVAL, data = COL.OLD,
listw = W, type = "mixed")
class(COL.sdm)
Out[15]:
In [16]:
##==============================================================
summary(COL.sdm)
Out[16]:
In [17]:
##==============================================================
op <- par(no.readonly = TRUE)
par(mfrow = c(2, 1))
plot(COL.OLD$CRIME, type = "l", main = "SDM Actual vs. Predicted")
lines(fitted(COL.sdm), lty = 2)
legend("topright", c("Actual", "Predicted"), lty = 1:2,
bty = "n")
plot(residuals(COL.sdm), type = "l", xlab = "", ylab = "",
main = "Residuals")
par(op)
In [18]:
##==============================================================
anova(COL.ar, COL.sar, COL.sem, COL.sdm, COL.ols)
Out[18]:
In [19]:
##==============================================================
COL.car <- spautolm(CRIME ~ INC + HOVAL, data = COL.OLD,
family = "CAR", listw = W)
class(COL.car)
Out[19]:
In [20]:
##==============================================================
summary(COL.car)
Out[20]:
In [21]:
##==============================================================
op <- par(no.readonly = TRUE)
par(mfrow = c(2, 1))
plot(COL.OLD$CRIME, type = "o", main = "CAR Actual vs. Predicted")
lines(fitted(COL.car), lty = 2)
legend("topright", c("Actual", "Predicted"), lty = 1:2,
bty = "n")
plot(residuals(COL.car), type = "l", xlab = "", ylab = "",
main = "Residuals")
par(op)
In [31]:
##==============================================================
library(spdep)
columbus <- readShapeSpatial(system.file("etc/shapes/columbus.shp",
package = "spdep")[1])
col.gal.nb <- read.gal(system.file("etc/weights/columbus.gal",
package = "spdep")[1])
names(columbus)
xy <- cbind(columbus$X, columbus$Y)
Out[31]:
In [32]:
##==============================================================
ac1 <- autocov_dist(columbus$CRIME, xy, nbs = 10, style = "W",
type = "one")
acinv <- autocov_dist(columbus$CRIME, xy, nbs = 10, style = "W",
type = "inverse")
acinv2 <- autocov_dist(columbus$CRIME, xy, nbs = 10,
style = "W", type = "inverse.squared")
In [33]:
##==============================================================
summary(columbus$CRIME)
summary(ac1)
Out[33]:
Out[33]:
In [34]:
##==============================================================
res1 <- glm(CRIME ~ HOVAL + ac1, data = columbus, family = "gaussian")
res1
Out[34]:
In [35]:
##==============================================================
AIC(glm(CRIME ~ HOVAL + acinv, data = columbus, family = "gaussian"))
AIC(glm(CRIME ~ HOVAL + acinv2, data = columbus, family = "gaussian"))
Out[35]:
Out[35]:
In [38]:
##==============================================================
library(spgwr)
data(georgia)
In [39]:
##==============================================================
summary(gSRDF["PctBach"])
Out[39]:
In [40]:
##==============================================================
g.gauss <- gwr.sel(PctBach ~ TotPop90 + PctRural + PctEld +
PctFB + PctPov + PctBlack, gweight = gwr.Gauss, data = gSRDF)
g.gauss
Out[40]:
In [41]:
##==============================================================
gwr.sel(PctBach ~ TotPop90 + PctRural + PctEld + PctFB +
PctPov + PctBlack, gweight = gwr.gauss, data = gSRDF)
gwr.sel(PctBach ~ TotPop90 + PctRural + PctEld + PctFB +
PctPov + PctBlack, gweight = gwr.tricube, data = gSRDF)
Out[41]:
In [42]:
##==============================================================
res <- gwr(PctBach ~ TotPop90 + PctRural + PctEld + PctFB +
PctPov + PctBlack, data = gSRDF, bandwidth = g.gauss)
res
Out[42]:
In [43]:
##==============================================================
str(res, max.level = 1)
str(res$SDF@data)
In [44]:
##==============================================================
brks <- c(-0.25, 0, 0.01, 0.025, 0.075)
cols <- grey(5:2/6)
plot(res$SDF, col = cols[findInterval(res$SDF$PctBlack,
brks, all.inside = TRUE)])
legend("bottomleft", leglabs(brks), fill = cols, bty = "n")
In [45]:
##==============================================================
res$lm$coefficients["PctBlack"]
Out[45]:
In [46]:
##==============================================================
g.adapt.gauss <- gwr.sel(PctBach ~ TotPop90 + PctRural +
PctEld + PctFB + PctPov + PctBlack, data = gSRDF,
adapt = TRUE)
res.adpt <- gwr(PctBach ~ TotPop90 + PctRural + PctEld +
PctFB + PctPov + PctBlack, data = gSRDF, adapt = g.adapt.gauss)
res.adpt
Out[46]:
In [47]:
##==============================================================
res <- gwr(PctBach ~ TotPop90 + PctRural + PctEld + PctFB +
PctPov + PctBlack, data = gSRDF, bandwidth = g.gauss,
hatmatrix = TRUE)
LMZ.F3GWR.test(res)
In [ ]: