Presentation on theme: "R for Macroecology Spatial models. Next week Any topics that we haven’t talked about? Group projects."— Presentation transcript:
R for Macroecology Spatial models
Next week Any topics that we haven’t talked about? Group projects
SAR Models Augment standard OLS with an additional term to model the spatial autocorrelation We’ll focus on error SAR models, which focuses on spatial pattern in the error part of the model OLSY = β X + ε SAR lag Y = ρ WY + β X + ε SAR error Y = β X + λ Wu+ ε Defining the spatial weights matrix, W, is crucial
Neighborhoods in R spdep dnearneigh() knearneigh() dnearneigh(x, d1, d2, row.names = NULL, longlat = NULL) Coordinates (matrix or SpatialPoints) Minimum and maximum distances (in km if longlat = T) Returns a list of vectors giving the neighbors for each point
Neighborhoods in R spdep dnearneigh() knearneigh() > x = c(1,3,2,5) > y = c(3,2,4,4) > n = dnearneigh(cbind(x,y),d1 = 0,d2 = 3) > n Neighbour list object: Number of regions: 4 Number of nonzero links: 10 Percentage nonzero weights: 62.5 Average number of links: 2.5 > str(n) List of 4 $ : int [1:2] 2 3 $ : int [1:3] $ : int [1:3] $ : int [1:2] attr(*, "class")= chr "nb" - attr(*, "nbtype")= chr "distance”...
Converting a neighborhood to weights nb2listw(neighbours, style="W", zero.policy=NULL) neighbors listwhat to do with neighborless points W = row standardized (rows sum to 1) B = binary (0/1) C = global standardized (all links sum to n) U = C/n S = variance stabilization (Tiefelsdorf et al. 1999)
Neighborhoods on grids x = rep(1:20,20) y = rep(1:20,each = 20) plot(x,y) n = dnearneigh(cbind(x,y),d1=0,d2 = 1) w = nb2listw(n) plot(w,cbind(x,y)) n = dnearneigh(cbind(x,y),d1=0,d2 = sqrt(2)) w = nb2listw(n) plot(w,cbind(x,y)) Rook’s caseQueen’s case
Data size SAR models can take a very long time to fit 2000 points is the maximum I have used sample() is useful again
Fitting the SAR model errorsarlm() errorsarlm(formula, listw, zero.policy=NULL) just like lm()what to do with neighborless points The neighborhood weights
Try it out Build several SAR models with different W Which one works best?
Spatial eigenvector maps Generate new predictors that represent the spatial structure of the data Three steps Calculate a pairwise distance matrix Do a principal components analysis on this matrix Select some of these PCA axes to add to an OLS model
Spatial eigenvector maps Diniz-Filho and Bini 2005