-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpadroes_de_pontos_dados_e_figuras.R
305 lines (273 loc) · 11.9 KB
/
padroes_de_pontos_dados_e_figuras.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
294
295
296
297
298
299
300
301
302
303
304
305
library(spatstat)
################################################################################
## Figuras de exemplos dos 4 processos, para os exercicios
################################################################################
## Poisson homogeneo
pp.1 <- rpoispp(35, win = unit.square())
## Poisson inhomogeneous
##Z <- as.im(function(x,y){ifelse(x>0.5, 25, 100)}, unit.square())
## Using a raster image
Z1 <- as.im(function(x,y){100 * exp(-2.5*x + 3*y)}, unit.square())
pp.2 <- rpoispp(Z1)
## Thomas homogeneous
pp.3 <- rThomas(kappa = mean(Z1/10), scale = 0.05, mu = 10)
##inhomogeneous
##Z2 <- as.im(function(x,y)10 * exp(2.5*x - 3*y), unit.square())
janela <- owin(c(10,51), c(10,77.5))
Z2 <- as.im(volcano, W=janela)
##pp.4 <- rThomas(kappa = function(x,y){10 * exp(2.5*x - 3*y)}, 0.05, 10, win = unit.square())
pp.4 <- rThomas(kappa = exp(as.im(volcano)/10-15)*1.e-3, scale = 1.5, mu = 20, win = janela)
## Save plots
png("images/point_pattern_examples%02d.png")
plot(pp.1, main = "")
plot(pp.2, main = "")
plot(pp.3, main = "")
plot(pp.4, main = "")
image(Z1, main = "")
points(pp.2, pch=19)
plot(Z2, main ="")
points(pp.4)
dev.off()
################################################################################
## Exemplo das Vitoria-regia
################################################################################
vit <- read.csv("data/vit_regia_coords.csv")
## convert to meters (approx)
vit$xm <- vit$x*0.3752972
vit$ym <- vit$y*0.3752972
## point pattern
vitoria.pp <- ppp(vit$xm, vit$ym, window = owin(c(0,36.513*0.3752972), c(0,20.505*0.3752972)))
## Salva o arquivo em format ppp
##save(vitoria.pp, file = "data/vitoria_regia.rds")
write.csv(vit, file = "data/vit_regia_coord_m.csv")
################################################################################
## L de Ripley para os exemplos do primeiro roteiro
################################################################################
## Poisson homogeneo
PH <- rpoispp(lambda=100, win = unit.square())
PH.L <- envelope(PH, fun = Lest, nsim = 1000)
## Aqui definimos uma funcao que descreve como a intensidade muda na area
## No caso, a itensidade muda de 180 para 20 a partir do valor da coordenada X = 0.5
padrao1 <- function(x,y) ifelse(test = x > 0.5, yes = 20, no = 180)
## Gera o padrão de pontos
IP <- rpoispp(lambda = padrao1, win = unit.square())
## L de Ripley
IP.L <- envelope(IP, fun = Lest, nsim = 1000)
## Thomas homogeneo
TH <- rThomas(kappa = 15, scale = 0.02, mu = 10, win = unit.square())
TH.L <- envelope(TH, fun = Lest, nsim = 1000)
## Vitorias regias
## carrega um objeto com as coordenadas dos pontos de inserção dos pecíolos de cada folha
vit <- read.csv("data/vit_regia_coord_m.csv")
## Cria objeto com o padrão de pontos para analisar
## Define a área de observação
vit.w <- owin(xrange = c(0, 13.70323), yrange = c(0, 7.695469))
## Cria o padrão de pontos nesta área
vit.p <- ppp(x = vit$xm, y = vit$ym, window = vit.w)
## Envelope
vit.env <- envelope(vit.p, fun = Lest, nsim = 1000)
png("images/figsL_Ripley%02d.png")
## Plota os grafico
plot(IP, main = "")
## Uma linha para marcar as duas regiões
abline(v = 0.5, lty =2 , col = "blue")
plot(TH, main = "")
## Grafico do L
plot(IP.L, . -r ~ r, ylab = "L de Ripley", legend=FALSE, main = "A")
plot(TH.L, . -r ~ r, ylab = "L de Ripley", legend=FALSE, main = "B")
plot(PH.L, . -r ~ r, ylab = "L de Ripley", legend=FALSE, main = "C")
plot(vit.env, . -r ~ r, ylab = "L de Ripley", legend=FALSE, main = "D")
dev.off()
################################################################################
## exemplos para roteiro de ajuste de modelos
################################################################################
## Preparacao dos arquivos
## Area 100x100, variave ambiental em escala compativel com MO no solo
Z3 <- as.im(function(x,y) 20 + 0.005*x*y, W = owin(c(0,100), c(0,100)), eps = 1)
write.table(as.matrix(Z3), file = "data/EP_roteiro3_solo.txt", row.names=FALSE, col.names=FALSE)
## Poisson nao homogeneo
p1 <- rpoispp(Z3/250)
write.csv(as.data.frame(p1), file = "data/EP_roteiro3_padrao1.csv", row.names=FALSE)
## Um padrao dificil de distinguir só com os envelopes
ilogit <- function(x,a,b) exp(a+b*x)/(1+exp(a+b*x))
p2 <- rpoispp(ilogit(Z3, -3.5, .2)/10)
write.csv(as.data.frame(p2), file = "data/EP_roteiro3_padrao2.csv", row.names=FALSE)
## Padrao Thomas não homogêneo para desafio
p3 <- rThomas(kappa = 0.05, scale = 2.5, mu = exp(MO.solo/25), win = owin(c(0,100), c(0,100)))
write.csv(as.data.frame(p3), file = "data/EP_roteiro3_padrao3.csv", row.names=FALSE)
## Ilustracao do AIC
carac <- readPNG("images/caracol.png")[,,1]
carac <- rotate(as.im(abs(carac-1)))
carac1 <- blur(carac, sigma = 2.5)
carac2 <- blur(carac, sigma = 5)
c.p1 <- rpoispp(carac*0.75)
c.p1.m1 <- ppm(c.p1 ~ carac1)
c.p1.m2 <- ppm(c.p1 ~ carac2)
png("images/roteiro_EP_AIC_%01d.png")
plot(carac, col=grey.colors(100)[100:1], main = "Padrão caracol - Intensidade real")
plot(c.p1, pch=19, cex=0.25, main = "Pontos gerados com a intensidade padrão caracol")
plot(carac2, col=grey.colors(100)[100:1])
plot(predict(c.p1.m1), col=grey.colors(100)[100:1],
main = paste0("Modelo 1 - Intensidade prevista \n AIC = ", round(AIC(c.p1.m1))))
plot(predict(c.p1.m2), col=grey.colors(100)[100:1],
main = paste0("Modelo 2 - Intensidade prevista \n AIC = ", round(AIC(c.p1.m2))))
dev.off()
png("images/roteiro_EP_AIC_5.png", width = 960)
par(mfrow=c(1,2))
plot(predict(c.p1.m1), col=grey.colors(100)[100:1],
main = paste0("Intensidade prevista pelo Modelo 1 \n AIC = ", round(AIC(c.p1.m1))))
plot(predict(c.p1.m2), col=grey.colors(100)[100:1],
main = paste0("Intensidade prevista pelo Modelo 2 \n AIC = ", round(AIC(c.p1.m2))))
dev.off()
################################################################################
## Inicio dos codigos do exercício
################################################################################
URL <- "https://github.com/piLaboratory/BIE-0320/raw/main/data/"
## Carrega padrao de pontos
p1 <-
paste0(URL, "EP_roteiro3_padrao1.csv") |>
read.csv() |>
as.ppp( W = owin(c(0,100), c(0,100), unitname = "m"))
plot(p1, main = "Padrão 1")
## carrega dados solo
MO.solo <-
paste0(URL,"EP_roteiro3_solo.txt") |>
read.table() |>
as.matrix() |>
as.im( W = owin(c(0,100), c(0,100)), eps=1 )
plot(MO.solo, main = "% de M.O. no solo superficial")
## Modelos
## Poisson homogeneo
p1.m1 <- ppm(p1)
p1.m1.L <- envelope(p1.m1, fun = Lest)
plot(p1.m1.L, . -r ~ r, ylab = "L de Ripley", legend=FALSE, main = "Padrão 1 - Modelo Poisson Homogêneo")
## Poisson nao homogeneo
p1.m2 <- ppm(p1 ~ MO.solo)
p1.m2.L <- envelope(p1.m2, fun = Lest)
plot(p1.m2.L, . -r ~ r, ylab = "L de Ripley", legend=FALSE, main = "Padrão 1 - Modelo Poisson não-homogêneo")
(cfs <- coef(p1.m2))
curve(exp(cfs[1] + cfs[2]*x), 20,70, main = "Padrão 1 - Intensidade prevista Poisson não-homogêneo",
ylab = "Intensidade / m2", xlab = "% M.O. solo superficial")
## AIC
AIC(p1.m1)
AIC(p1.m2)
## Previsto pelo modelo selecionado
plot(predict(p1.m2), main = "Padrão 1 - previsto Poisson não-homogêneo")
points(p1, cex=.5)
## Para ilustrar o AIC: um padrao dificil de identificar
p2 <- as.ppp(read.csv("data/EP_roteiro3_padrao2.csv"), owin(c(0,100), c(0,100)))
plot(p2, main = "Padrão 2")
## Modelos
## Poisson homogeneo
p2.m1 <- ppm(p2)
p2.m1.L <- envelope(p2.m1, fun = Lest)
plot(p2.m1.L, . -r ~ r, ylab = "L de Ripley", legend=FALSE, main = "")
## Poisson nao homogeneo
p2.m2 <- ppm(p2 ~ MO.solo)
p2.m2.L <- envelope(p2.m2, fun = Lest)
plot(p2.m2.L, . -r ~ r, ylab = "L de Ripley", legend=FALSE, main = "")
## AIC
AIC(p2.m1)
AIC(p2.m2)
################################################################################
## Para o desafio: um processo de Thomas nao homogeneo
################################################################################
p3 <- as.ppp(read.csv("data/EP_roteiro3_padrao3.csv"), owin(c(0,100), c(0,100)))
## Modelos
## Poisson homogeneo
p3.m1 <- ppm(p3)
p3.m1.L <- envelope(p3.m1, fun = Lest)
## Poisson nao homogeneo
p3.m2 <- ppm(p3 ~ MO.solo)
p3.m2.L <- envelope(p3.m2, fun = Lest)
## Thomas homogeneo
p3.m3 <- kppm(p3, method = "palm")
p3.m3.L <- envelope(p3.m3, fun = Lest)
## Thomas não-homogêneo
p3.m4 <- kppm(p3 ~ MO.solo, method = "palm")
p3.m4.L <- envelope(p3.m4, fun = Lest)
## Comparando os Thomas com AIC
## Obs: não dá para comparar modelos ajustados com as funcoes ppm com modelos ajustados com kppm
AIC(p3.m3)
AIC(p3.m4)
## Grava as figuras
png("images/EP_roteiro3_%01d.png")
plot(p3, "Padrão mistêrio-ôso")
plot(predict(p3.m4), main = "Padrão misterioso \n Intensidade Prevista pelo melhor modelo")
plot(p3.m1.L, . -r ~ r, ylab = "L de Ripley", legend=FALSE, main = "Padrão misterioso - Poisson homogêneo")
plot(p3.m2.L, . -r ~ r, ylab = "L de Ripley", legend=FALSE, main = "Padrão misterioso - Poisson não-homogêneo")
plot(p3.m3.L, . -r ~ r, ylab = "L de Ripley", legend=FALSE, main = "Padrão misterioso - Thomas homogêneo")
plot(p3.m4.L, . -r ~ r, ylab = "L de Ripley", legend=FALSE, main = "Padrão misterioso - Thomas não-homogêneo")
dev.off()
## Nao ajuda muito: plot de n de pontos por MO em quadrados de 1x1
## Grafico de N de pontos x MO em cada pixel de 1x1
p1.im <- pixellate(p1, W = owin(c(0,10), c(0,10)), eps = 1)
X <- as.matrix(MO.solo)
Y <- p1.im$v
class(Y) <- "matrix"
plot(X, Y, xlab = "% de MO no solo", ylab = "Densidade de pontos por m2")
p1.m1.cf <- coef(p1.m1)
p1.m3.cf <- coef(p1.m3)
curve(exp(p1.m1.cf[1] + p1.m1.cf[2]*x), add=TRUE, col = "blue")
curve(exp(p1.m3.cf[1] + p1.m3.cf[2]*x), add=TRUE, col = "red")
## Alternativa em quadrado de 10x10
##MO.solo <- as.im(function(x,y) 20 + 0.5*x*y, W = owin(c(0,10), c(0,10)), eps = 1)
## p1 <- rThomas(kappa = 1, scale = 0.3, mu = exp(MO.solo/20), win = owin(c(0,10), c(0,10)))
##
################################################################################
## Um outro conjunto de dados
################################################################################
Z4 <- as.im(function(x,y) sqrt((x^2 + y^2)), owin(c(0,10), c(0,10)), eps = 1)
set.seed(42)
p2 <- rThomas(kappa = function(x,y) sqrt((x^2 + y^2))/5,
scale = 0.5, mu = 5, win = owin(c(0,10), c(0,10)), eps = 1)
plot(p2)
## modelos
p2.m0 <- ppm(p2 ~ 1)
p2.m1 <- ppm(p2 ~ MO.solo)
p2.m2 <- kppm(p2 ~ 1 , cluster = "Thomas", method = "palm")
p2.m3 <- kppm(p2 ~ MO.solo , cluster = "Thomas", method = "palm")
AIC(p2.m0)
AIC(p2.m1)
AIC(p2.m2)
AIC(p2.m3)
p2.m0.L <- envelope(p2.m0, fun = Lest)
p2.m1.L <- envelope(p2.m1, fun = Lest)
p2.m2.L <- envelope(p2.m2, fun = Lest)
p2.m3.L <- envelope(p2.m3, fun = Lest)
##
plot(p2.m0.L, . -r ~ r, ylab = "L de Ripley", legend=FALSE, main = "")
plot(p2.m1.L, . -r ~ r, ylab = "L de Ripley", legend=FALSE, main = "")
plot(p2.m2.L, . -r ~ r, ylab = "L de Ripley", legend=FALSE, main = "")
plot(p2.m3.L, . -r ~ r, ylab = "L de Ripley", legend=FALSE, main = "")
plot(predict(p2.m3))
points(p2)
## People in park
plot(gordon)
gordon.im <- as.im(function(x,y) y/33, W = gordon$window)
plot(gordon.im)
gordon.m1 <- kppm(gordon~1, "Thomas")
gordon.m1.env <- envelope(gordon.m1, fun=Lest)
plot(gordon.m1.env, . -r ~ r, ylab = "L de Ripley", legend=FALSE, main = "")
gordon.m2 <- kppm(gordon~gordon.im, "Thomas")
gordon.m2.env <- envelope(gordon.m2, fun=Lest)
plot(gordon.m2.env, . -r ~ r, ylab = "L de Ripley", legend=FALSE, main = "")
## Bei
plot(bei)
plot(bei.extra$elev)
bei.m1 <- ppm(bei ~ 1)
bei.m2 <- ppm(bei ~ grad + I(grad^2), data = bei.extra)
bei.m3 <- kppm(bei ~ 1, cluster = "Thomas", method = "palm")
bei.m4 <- kppm(bei ~ grad + I(grad^2), data = bei.extra, cluster = "Thomas", method = "palm")
bei.m1.L <- envelope(bei.m1, fun = Lest)
bei.m2.L <- envelope(bei.m2, fun = Lest)
bei.m3.L <- envelope(bei.m3, fun = Lest)
bei.m4.L <- envelope(bei.m4, fun = Lest)
plot(bei.m1.L, . -r ~ r, ylab = "L de Ripley", legend=FALSE, main = "")
plot(bei.m2.L, . -r ~ r, ylab = "L de Ripley", legend=FALSE, main = "")
plot(bei.m3.L, . -r ~ r, ylab = "L de Ripley", legend=FALSE, main = "")
plot(bei.m4.L, . -r ~ r, ylab = "L de Ripley", legend=FALSE, main = "")
AIC(bei.m1)
AIC(bei.m2)
AIC(bei.m3)
AIC(bei.m4)