Question: Use RStudio to answer the questions below. Load the New Haven dataset which contains US Census - derived demographic characteristics of each census block. Here,

Use RStudio to answer the questions below.
Load the New Haven dataset which contains US Census-derived demographic characteristics of each census block. Here, we are interested in measuring spatial autocorrelation in the proportion of residents of each census block who self-identify as white (i.e., the variable blocks$P_WHITE). For this variable, you should:
1. Create a lagged means plot for queens case adjacency rules, and fit a linear regression between P_WHITE (independent variable) and the lagged means (dependent variable). What does this regression indicate about spatial autocorrelation in the variable P_WHITE?
2. Calculate Morans I using a k nearest neighbors rule for determining adjacency, for each of k =1,2,3,...,10. Create a plot of k (x axis) vs. Morans I (y axis). What does your plot suggest about how the spatial autocorrelation of P_WHITE changes with how neighboring census blocks are defined?
I started the script for Rstudio and at the end for answering Question 2 is where I am having trouble.
Please see my script for both questions (I cannot upload the Rstudio dataset called newhaven).
#Load libraries
library(maptools)
library(spdep)
library(GISTools)
library(pgirmess)
# Load data
data(newhaven)
coords <- coordinates(blocks) # get the centroids of blocks
# Question 1: Lagged Means Plot and Linear Regression
coords <- coordinates(blocks) # get the centroids of blocks
#Choropleth
par(mfrow=c(1,1))
choropleth(blocks, blocks$PCTOWNHOME)
#Introducing the 'nb' objects and queen's case adjacency rule
?poly2nb
nb.queen <- poly2nb(blocks)
plot(blocks)
plot(nb.queen, coords, add=T, pch=16, col="red")
#rook's case adjacency rule
nb.rook <- poly2nb(blocks, queen=F)
plot(blocks)
plot(nb.rook, coords, add=T, pch=16, col="blue")
#Plotting queen and rook side by side
par(mfrow=c(1,2))
plot(blocks)
plot(nb.queen, coords, add=T, pch=16, col="red")
plot(blocks)
plot(nb.rook, coords, add=T, pch=16, col="blue")
#Creating a nb object from finding the k nearest polygons (centroids) as neighbors
IDs <- row.names(as(blocks, "data.frame"))
nb.k1<- knn2nb(knearneigh(coords, k=1), row.names=IDs)
plot(blocks)
plot(nb.k1, coords, add=T, pch=16, col="red")
#k=2,3,4 nearest neighbors
nb.k2<- knn2nb(knearneigh(coords, k=2), row.names=IDs)
nb.k3<- knn2nb(knearneigh(coords, k=3), row.names=IDs)
nb.k4<- knn2nb(knearneigh(coords, k=4), row.names=IDs)
par(mfrow=c(2,2))
plot(blocks)
plot(nb.k1, coords, add=T, pch=16, col="red")
plot(blocks)
plot(nb.k2, coords, add=T, pch=16, col="purple")
plot(blocks)
plot(nb.k3, coords, add=T, pch=16, col="green")
plot(blocks)
plot(nb.k4, coords, add=T, pch=16, col="gold")
#To determine neighbors based on distance itself, use the function dnearneigh to find neighbors between distance d1 and d2
nb.d500<- dnearneigh(coords, d1=0, d2=500, row.names=IDs)
nb.d1000<- dnearneigh(coords, d1=0, d2=1000, row.names=IDs)
nb.d1500<- dnearneigh(coords, d1=0, d2=1500, row.names=IDs)
nb.d2000<- dnearneigh(coords, d1=0, d2=2000, row.names=IDs)
plot(blocks)
plot(nb.d500, coords, add=T, pch=16, col="red")
plot(blocks)
plot(nb.d1000, coords, add=T, pch=16, col="blue")
plot(blocks)
plot(nb.d1500, coords, add=T, pch=16, col="green")
plot(blocks)
plot(nb.d2000, coords, add=T, pch=16, col="gold")
#Spatial autocorrelation
par(mfrow=c(1,1))
choropleth(blocks, blocks$PCTOWNHOME)
#Lagged plot
# Calculate lagged means
lw.queen <- nb2listw(nb.queen, style ="W")
lagged.means <- lag.listw(lw.queen, blocks$P_WHITE)
plot(lagged.means, blocks$P_WHITE, xlab = "Lagged Means", ylab ="P_WHITE")
abline(lm(blocks$P_WHITE ~ lagged.means), col = "red")
#Question 2
# Calculate Moran's I using k nearest neighbors rule
k <- c(1:10)
estimates <- rep(NA,10)
for (i in 1:10){
lw_k <- knn2nb(knearneigh(coords, k = i), row.names = row.names(blocks)) # create spatial weights with k nearest neighbors rule
estimates[i]<- as.numeric(moran.test(blocks$P_WHITE, lw_k)$estimate[1]) # extract Moran's I
}
# Develop a plot of k vs. Moran's I
plot(k, estimates, type ="b", main = "Moran's I vs. k", xlab ="k", ylab = "Moran's I")

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!