Hi all,

Currently, I am working with U.S. voter data. Below, I included a brief example 
of the structure of the data with some reproducible code. My data set consists 
of roughly 233,000 (233k) entries, each specifying a voter and their particular 
latitude/longitude pair. I have been using the spdep package with the hope of 
creating a CAR model. To begin the analysis, we need to find all first order 
neighbors of every point in the data. 

While spdep has fantastic commands for finding k nearest neighbors 
(knearneigh), and a useful command for finding lag of order 3 or more (nblag), 
I have yet to find a method which is suitable for our purposes (lag = 1, or lag 
=2). Additionally, I looked into altering the nblag command to accommodate 
maxlag = 1 or maxlag = 2, but the command relies on an nb format, which is 
problematic as we are looking for the underlying neighborhood structure.

There has been numerous work done with polygons, or data which already is in 
“nb” format, but after reading the literature, it seems that polygons are not 
appropriate, nor are distance based neighbor techniques, due to density 
fluctuations over the area of interest. 

Below is some reproducible code I wrote. I would like to note that I am 
currently working in R 1.1.453 on a MacBook.

# Create a data frame of 10 voters, picked at random
voter.1 = c(1, -75.52187, 40.62320)
voter.2 = c(2,-75.56373, 40.55216)
voter.3 = c(3,-75.39587, 40.55416)
voter.4 = c(4,-75.42248, 40.64326)
voter.5 = c(5,-75.56654, 40.54948)
voter.6 = c(6,-75.56257, 40.67375)
voter.7 = c(7, -75.51888, 40.59715)
voter.8 = c(8, -75.59879, 40.60014)
voter.9 = c(9, -75.59879, 40.60014)
voter.10 = c(10, -75.50877, 40.53129)

# Bind the vectors together
voter.subset = rbind(voter.1, voter.2, voter.3, voter.4, voter.5, voter.6, 
voter.7, voter.8, voter.9, voter.10)

# Rename the columns
colnames(voter.subset) = c("Voter.ID", "Longitude", "Latitude")

# Change the class from a matrix to a data frame
voter.subset = as.data.frame(voter.subset)

# Load in the required packages
library(spdep)
library(sp)

# Set the coordinates
coordinates(voter.subset) = c("Longitude", "Latitude")
coords = coordinates(voter.subset)

# Jitter to ensure no duplicate points
coords = jitter(coords, factor = 1)

# Find the first nearest neighbor of each point
one.nn = knearneigh(coords, k=1)

# Convert the first nearest neighbor to format "nb"
one.nn_nb = knn2nb(one.nn, sym = F)

Thank you in advance for any help you may offer, and for taking the time to 
read this. I have consulted Applied Spatial Data Analysis with R (Bivand, 
Pebesma, Gomez-Rubio), as well as other Sig-Geo threads, the spdep 
documentation, and the nb vignette (Bivand, April 3, 2018) from earlier this 
year.

Warmest,
Ben
--
Benjamin Lieberman
Muhlenberg College 2019
Mobile: 301.299.8928



        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[email protected]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo

Reply via email to