This package makes it easy to calculate monadic spatial weights. Its focus is on weights in time-series cross-sectional data. It can find spatial weights and test statistics for spatial autocorrelation (Moran's I) on a per-time interval basis. It can be important to test for spatial autocorrelation on a per-time interval basis in order to assess if a spatial autocorrelation process is temporally bounded.
Here is a simple example with fake data with spatial clustering:
library(spatialWeights)
# Create fake time series data
faked1 <- expand.grid(ID = letters, year = 2010:2015)
faked1$located_continuous <- nrow(faked1):1
faked1$y <- nrow(faked1):1 - 200
# Find weights for continuous data
df_weights_cont1 <- monadic_spatial_weights(df = faked1, id_var = 'ID',
time_var = 'year',
location_var = 'located_continuous',
y_var = 'y', mc_cores = 1)
## Continuous location variable detected. Proximity found using method = euclidean.
## 2010: Moran's I p-value: <2e-16
## 2011: Moran's I p-value: <2e-16
## 2012: Moran's I p-value: <2e-16
## 2013: Moran's I p-value: <2e-16
## 2014: Moran's I p-value: <2e-16
## 2015: Moran's I p-value: <2e-16
head(df_weights_cont1)
## ID year sp_wght_located_continuous_y
## 1 a 2010 -19825
## 2 b 2010 -18444
## 3 c 2010 -17153
## 4 d 2010 -15954
## 5 e 2010 -14849
## 6 f 2010 -13840
Unlike in the first example, the units in the following data are independent of one another, i.e. not spatially clustered.
# Create fake time series data
faked2 <- expand.grid(ID = letters, year = 2010:2015)
faked2$located_continuous <- rnorm(nrow(faked2))
faked2$y <- nrow(faked2):1 - 200
# Find weights for continuous data
df_weights_cont2 <- monadic_spatial_weights(df = faked2, id_var = 'ID',
time_var = 'year',
location_var = 'located_continuous',
y_var = 'y', mc_cores = 1)
## Continuous location variable detected. Proximity found using method = euclidean.
## 2010: Moran's I p-value: 0.659
## 2011: Moran's I p-value: 0.951
## 2012: Moran's I p-value: 0.369
## 2013: Moran's I p-value: 0.335
## 2014: Moran's I p-value: 0.435
## 2015: Moran's I p-value: 0.354
By default monadic_spatial_weights
creates a spatially lagged variable. Using the arguement tlsl = TRUE
will lag this variable in time by one period creating an additional time lag spatial lag. For example:
# Create fake time series data
faked1 <- expand.grid(ID = letters, year = 2010:2015)
faked1$located_continuous <- nrow(faked1):1
faked1$y <- nrow(faked1):1 - 200
# Find weights for continuous data
df_weights_cont_tlsl <- monadic_spatial_weights(df = faked1, id_var = 'ID',
time_var = 'year',
location_var = 'located_continuous',
y_var = 'y', mc_cores = 1, tlsl = TRUE)
## Continuous location variable detected. Proximity found using method = euclidean.
## 2010: Moran's I p-value: <2e-16
## 2011: Moran's I p-value: <2e-16
## 2012: Moran's I p-value: <2e-16
## 2013: Moran's I p-value: <2e-16
## 2014: Moran's I p-value: <2e-16
## 2015: Moran's I p-value: <2e-16
head(df_weights_cont_tlsl)
## ID year sp_wght_located_continuous_y lag_sp_wght_located_continuous_y
## 1 a 2010 -19825 NA
## 2 a 2011 -28275 -19825
## 3 a 2012 -36725 -28275
## 4 a 2013 -45175 -36725
## 5 a 2014 -53625 -45175
## 6 a 2015 -62075 -53625
The argument mc_cores
allows you to specify the number of cores you would like to use to find the spatial weights in parellel. Parallel computation can lead to substantial speed improvements depending on how many cores you have available and how your data set is organised. To determine how many cores you have available use: parallel::detectCores()
.
The package can be installed with:
devtools::install_github("christophergandrud/spatialWeights")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.