This repository has been archived by the owner on Nov 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspatial_pattern.r
298 lines (160 loc) · 7.02 KB
/
spatial_pattern.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
################################################################################
# Data generating process
# Code adapted from Andre's paper without the treatment or spatial aggregation
# Steps
# 1. create a raster grid as values of X and T
# 2. Define a distribution for X
# 3. create patterns on the grid for T
# 4. create a spatial weight matrix in Y
# 5. generate y based on the values of X, T, and spatial weight matrix of Y
##############################################################################
library(raster)
library(splm)
library(tidyverse)
# Define extent
grid_size = 250
lon_min = 0
lat_min = 0
lon_max = grid_size
lat_max = grid_size
grid_extent = extent(lon_min, lon_max, lat_min, lat_max)
# Create the raster grid and get xy coordinates
grid_raster = raster(ext=grid_extent, resolution=1)
# fill with random X (such as slope, soil with patterns in value related to lontitude and latitude)
values(grid_raster) = 1:ncell(grid_raster)
slope_error = runif(grid_size^2, min = 40, max = 100)
# X with error
plot(grid_raster+slope_error)
##############################################
# 1. radial pattern: define T as circular points
###################################################################
radial_grid = grid_raster
# Consider “Treatment” (human activities) as distance-based,
# with intensity varying with distance from the center.
# lat_lon_center = c(grid_size/2,grid_size/2)
# define multiple centers
dist_compute = function(num_centers, lat_lon_centers ){
for (i in 1:length(num_centers)){
if (i==1){
min_dist = as.matrix(distanceFromPoints(radial_grid,lat_lon_centers[,i]))
}
else{
temp_matrix = as.matrix (distanceFromPoints(radial_grid,lat_lon_centers[,i]))
for (row_index in 1:grid_size){
for (col_index in 1:grid_size){
temp_dist = temp_matrix[row_index,col_index]
current_dist = min_dist[row_index,col_index]
min_dist[row_index,col_index] = min( temp_dist, current_dist )
}
}
}
}
return(min_dist)
}
num_centers = 25
lat_coord = sample(1:grid_size, size = num_centers)
lon_coord = sample(1:grid_size, size = num_centers)
lat_lon_centers = mapply(c, lat_coord, lon_coord, SIMPLIFY = TRUE)
plot(raster(10000/as.matrix(dist_compute(num_centers = num_centers, lat_lon_centers = lat_lon_centers))))
temp_matrix = as.matrix (distanceFromPoints(radial_grid,lat_lon_centers[,i]))
temp_matrix[25,25]
#temp_matrix = as.matrix (distanceFromPoints(radial_grid,lat_lon_centers[,2]))
#min_dist - temp_matrix
min_dist
temp_matrix
dist_radial <- 100000/distanceFromPoints(radial_grid,xy_center )
radial_error = runif(grid_size^2, min = 0, max = 0.3)
plot(min_dist+radial_error)
min_dist
dist_radial.matrix= as.matrix(dist_radial+radial_error)
# create points with distance to center at given intervals
##############################################
# 2. fishbone pattern: define T as roads
###################################################################
fishbone_grid = grid_raster
# set the back ground to 0
values(fishbone_grid)=0
# choose the index of the main road
grid_range = (grid_size*12+5) :(grid_size*12+23)
# set the main road value to be 1
values(fishbone_grid)[grid_range] = 1
plot(fishbone_grid)
# randomly choose index where the side roads will emerge
random_index = sample(grid_range)[1:10]
# for each index, generate north or south road
for (j in 1:length(random_index)){
# randomly choose north or south, i.e. i randomly equals 1 or -1
i= floor(runif(1, 0, 2)) * 2 - 1
values(fishbone_grid)[random_index[j]+i*grid_size]=1
values(fishbone_grid)[random_index[j]+i*grid_size*2]=1
values(fishbone_grid)[random_index[j]+i*grid_size*3]=1
}
plot(fishbone_grid)
# Y = a + bX + r WY + e, where a = b = 1
# Y is land use (binary: 0 if deforestation, 1 if covered )
# X variables: slope, elevation, or soil type
# Soil type pattern, Moisture level
# Justify why we use type 1 or type 2 X, specified in the
# 1. X ~ U (0, 5) (spatial grid pattern)
# 2. X = (1- piW)K or X = W^2 K , with K ~ U (0,5)
# Y is also a function of contiguous parcels W, where our spatial matrix come in to depict the deforestation patterns.
# OLS
# # coefficients:
# 1. Constant
# 2. X =
# 3. Spatial lag
# 4. X type
# 5. Disaggregation SD
# xType Options:
# "lagged" : lagged X using 0.9
# "wwx" : W*W*X
# "random" : purely random
############################################
# Creating Spatial Weight Matrices:
############################################
# Functions used
# Spatial weights for neighbours lists
?nb2listw
# generate neighbors based on the type
?cell2nb(nrow, ncol, type="rook", torus=FALSE)
# For the circle pattern, it would just be a spatial lag thing (i.e. spatial matrix using Queen)
# Create spatial weight matrix W
size = 120
p=1
eval(parse(text = paste("Wn", p, "= as(as_dgRMatrix_listw(nb2listw(cell2nb(size, size, type = \"queen\"))), \"CsparseMatrix\")", sep = "")))
# eval(parse(text = paste("Wl", p, "= as(as_dgRMatrix_listw(nb2listw(cell2nb(size, size, type = \"queen\"))))", sep = "")))
# For the fishbone, you might consider dropping fake roads on the map
# and then having a spatial lag that was not based on a rook or queen's weights matrix
# but one that was more linear
# Y = bX + a
# generate the X, based on X type
nRep = 1
size =120
xType = "random"
X = runif(size^2, min = 0, max = 5)
if (xType != "random"){
if (xType == "lagged"){
X = as(powerWeights(Wn, 0.9, X = as.matrix(X)), "matrix")[,1]
}else{
X = as(lag.listw(Wl, lag.listw(Wl, X)), "matrix")[,1]
}
}
# generate the error term
e = rnorm(size^2, mean = 0, sd = 1)
# generate the Y for different spatial weights in X and Y
# Y random
trueCoeff = c(1, 1, 0.5, 0)
cat(paste(" Coefficients: const =", trueCoeff[1] ,"| X =", trueCoeff[2], "| T =", trueCoeff[3], ifelse(trueCoeff[4] > 0, paste("| Spatial Lag Y =", trueCoeff[4]), "| No Spatial Lag Y"),"\n"))
Y = powerWeights(Wn1, trueCoeff[4], X = as.matrix(trueCoeff[1] + trueCoeff[2]*X + e))
# Y + Lag 0.9
trueCoeff = c(1, 1, 0.5, 0.9)
cat(paste(" Coefficients: const =", trueCoeff[1] ,"| X =", trueCoeff[2], "| T =", trueCoeff[3], ifelse(trueCoeff[4] > 0, paste("| Spatial Lag Y =", trueCoeff[4]), "| No Spatial Lag Y"),"\n"))
Y = powerWeights(Wn1, trueCoeff[4], X = as.matrix(trueCoeff[1] + trueCoeff[2]*X + e))
image(Y)
image(Wn1)
image(Wl1)
# Simulation
# 1000 simulations
#The results of aggregating the observed data to different resolutions are illustrated by mapping the estimated coefficients and their standard errors against their values at the true level.
# the vertical axis represents the magnitudes of the coefficient estimates and the horizontal axis represents the resolution chosen
# Thus, it ranges from the pixel level, up to the highest aggregation level of 12x12 (the lowest resolution). The vertical dashed line denotes the true level, which in the case of Fig 5 case is a 6x6 resolution.