Вы находитесь на странице: 1из 113

Tutorials on the usage of the package geoR

1. Exploratory data analysis 1. Basic aspects of the spatial exploratory data analysis:

2.

3.

4.

5.

2. Reading data and exploratory analysis: a tutorial on first steps of the data analysis. Simulating geostatistical data 1. Simulating from the geostatistical model: simulations using the function grf illustrating different aspects of the geostatistical model. 2. Animated 2D simulations: (be patient! this may take a while to load in your browser) 3. Simulating from the generalised linear geostatistical model. Illustrating geostatistical calculations step by step 1. Example on the simple kriging algorithm. 2. Example of some geostatistical calculations. 3. Constructing a covariate and obtaining the confidence interval for the coeficcient. 4. Kriging with covariates: example on how to construct a covariate on the prediction points to be used in the kriging calculations. 5. Kriging in presence of covariates: a note: comments on the usage of the arguments trend.l and trend.d. Generic tutorials 1. A traditional geostatistical data analysis: example of the basic steps for a simple geostatistical analysis. 2. Some commands for a standard geostatistical analysis: illustrate the package syntax for exploratory analysis (including variograms), parameter estimation and kriging prediction. 3. An introduction to geoR: web page with the main functionality of the package geoR. 4. Commands file with contents shown during a pratical session during a short course on geostatistics. 5. A basic example of a geostatistical analysis of a soil conductivity data-set using R/geoR. 6. Yet another example of the steps of a data analysis. More on Bayesian analysis 1. An example of Bayesian prediction: Illustrates the usage of the geoR's function krige.bayes()

2. Further examples for the function krige.bayes are given in the file examples.krige.bayes.R 6. Bivariate geostatistical data 1. Simulating bivariate proccess: 2. Inference and prediction for the bivariate Gaussian common component model: 7. Other data-sets (in addition to the ones already included in the package) Data this is a directory with data-sets used in some of the tutorials. Typically there are two files corresponding to each data set: files with extension .dat are the data files. o files with extension .txt are files with a brief explanation on the data. 8. geoR and sp: converting data formats 0. Converting kriging results to SpatialGridDataFrame and SpatialPixelsDataFrame formats 9. Other tutorials 0. An introduction to geoRglm: web page with the main aspects of the package geoRglm. 1. Simulating stochastic proccesses: simulation routines to illustrate basic concepts of stochastic proccesses.
o

paulojus AT ufpr.br Last modified: Tue Dec 16 12:03:26 BRST 2008

Tutorials on the usage of the package geoR


10. Exploratory data analysis 1. Basic aspects of the spatial exploratory data analysis:
11. 12. 13. 14. 15. 16. 17. 18. ## ## Anlises exploratrias iniciais e espaciais ## ## ## ## ## A anlise exploratria espacial tenta identificar: 1. presena de dependncia espacial 2. a existncia de dados discrepantes (globais ou locais) 3. assimetrias, necessidades de transformao, etc

19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33.

## 4. tendncias com coordenadas ou covariveis ## carregando o pacote geoR require(geoR) ## carregando o conjunto de data(s100) ## um resumo dos dados summary(s100) ## inspecionando o objeto names(s100) dados "s100" (distribudo com a geoR)

## ilustrantd algumas operaes bsicas que podem ser feitas com os dados 34. var(s100$data) 35. boxplot(s100$data) 36. 37. ## 38. ## Anlise exploratria espacial 39. ## 40. 41. ## 42. ## Usando a funo points.geodata(), e ilustrando algumas de suas opes 43. ## 44. points(s100) 45. ## controlando o tamanho dos pontos 46. points(s100, pt.div="equal") 47. points(s100, cex.min=.6, cex.max=.6) 48. points(s100, cex.min=.3, cex.max=3) 49. ## dividindo os dados em quantis 50. points(s100, cex.min=1, cex.max=1, pt.div="quart") 51. points(s100, pt.div="quart") 52. points(s100, pt.div=4) 53. points(s100, pt.div="dec") 54. points(s100, cex.min=1, cex.max=1, pt.div="quint") 55. ## opes para cores dos pontos 56. points(s100, cex.min=1, cex.max=1, col="gray") 57. points(s100, cex.min=1, cex.max=1, col=rainbow(100)) 58. points(s100, cex.min=1, cex.max=1, col=terrain.colors(100)) 59. ## opo para modificar caracter dos pontos 60. points(s100, pt.div="equal", pch.seq="+") 61. 62. ## Verificando o aspecto do grficos para dados sem correlao espacial 63. ## (permutando a localizao dos dados para gerar dados espacialmente independentes) 64. points(s100, data=sample(s100$data)) 65. ## e agora compare com o padro dos dados originais 66. X11(); points(s100) 67. dev.off() 68. 69. ## verificando o aspecto de dados assimtricos 70. ## (exponenciando dados para gerar dados log-normais) 71. points(s100, data=exp(s100$data))

72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84.

## inspecionando argumentos e outras opes da funo class(s100) args(points.geodata) help(points.geodata) ## Carregando um outro conjunto de dados data(parana) summary(parana)

## Note a tendncia nestes dados: points(parana, border=borders) ## agora "removendo" a tendncia, isto , fazendo o grfico dos resduos 85. ## de uma regresso linear nas coordenadas 86. points(parana, border=borders, trend="1st") 87. points(parana, border=borders, trend="1st", pt.div="quart") 88. ## permutando os dados nas posies para ver aspecto do grfico 89. ## sem correlao espacial 90. X11();points(parana, data=sample(parana$data),border=borders, trend="1st", pt.div="quart") 91. dev.off() 92. 93. ## 94. ## Um outro grfico descritivo: usando a funo plot.geodata() 95. ## 96. plot(s100) 97. plot(parana) 98. ## alguns argumentos adicionais 99. plot(parana, bor=borders, low=T) 100. ## agora compare anterior com os grfico dos resduos ("retirando a tendncia") 101. X11(); plot(parana, trend="1st", bor=borders, low=T) 102. dev.off() 103. 104. ## Carregando um outro conjunto de dados que possui covariveis 105. data(ca20) 106. summary(ca20) 107. names(ca20) 108. points(ca20, cex.min=0.5, cex.max=0.5, bor=borders) 109. polygon(ca20$reg1) 110. polygon(ca20$reg2) 111. polygon(ca20$reg3) 112. 113. plot(ca20, bor=borders) 114. plot(ca20, bor=borders, trend=~area) 115. plot(ca20, bor=borders, trend=~area+altitude) 116. 117. ## Carregando um outro conjunto de dados com comortamento assimtrico 118. ksat <read.geodata("http://www.leg.ufpr.br/geoR/tutorials/datasets/Crucia ni.dat", head=T, row.names=1) 119. summary(ksat) 120. plot(ksat) 121. 122. require(MASS)

123. boxcox(ksat) 124. ## o grfico produzido mostra que a transformao recomendada lambda=0 (log) 125. 126. ## visualizando dados transformados 127. plot(ksat, lambda=0) 128. 129. ## Melhorando o grfico 130. ## carregando as bordas 131. kbor <read.table("http://www.leg.ufpr.br/geoR/tutorials/datasets/Cruciani .border", head=T, row.names=1) 132. kbor 133. plot(ksat, lambda=0, bor=kbor) 134. points(ksat, lambda=0, bor=kbor) 135. 136. ## 137. ## Explorando a dependncia espacial atravs dos variogramas: 138. ## variograma exploratrio (emprico) 139. ## 140. s100.v <- variog(s100, max.d=1) 141. plot(s100.v) 142. 143. ## dados com tendncia: obtendo variograma dos resduos 144. parana.v <- variog(parana, max.dist=400, trend="1st") 145. plot(parana.v) 146. 147. ## dados com tendncia em covariveis: variograma dos resduos 148. ca20.v <- variog(ca20, trend=~area, max.d=800) 149. plot(ca20.v) 150. 151. ## variograma para dados transformados 152. ksat.v <- variog(ksat, max.d=10, lam=0) 153. plot(ksat.v)

1. Reading data and exploratory analysis: a tutorial on first steps of the data analysis.
154. 155. 156. 157. 158. 159. 160. 161. 162. 163. 164. 165. 166. 167. 168. ## ## Aspectos iniciais de anlise de dados utilizando pacote geoR ## ============================================================ ## ## ## 0. Comandos iniciais (ajustando o ambiente do R) ## -----------------------------------------------## ## Mostrar ajuda no "browser" (opcional) # options(htmlhelp = TRUE) ## Salvando parmetros grficos originais (opcional) par.ori <- par(no.readonly = TRUE) ## Carregando o pacote geoR require(geoR) ## library(geoR)

169. ## library(geoR, lib="coloque_diretorio_onde_geoR_foi_instalado_aqui") 170. ## 171. ## 172. ## 1. Entrada de dados 173. ## ------------------174. ## 175. ## 1.1. Carregando dados includos no pacote 176. ## 177. ls() 178. data(s100) 179. ls() 180. #help(s100) 181. 182. data(SIC) 183. ls() 184. #help(SIC) 185. ## 186. ## 1.2. Convertendo data-frames para objetos no formato geodata 187. ## 188. require(MASS) 189. data(topo) 190. topo 191. gtopo <- as.geodata(topo) 192. gtopo 193. 194. args(as.geodata) 195. #help(as.geodata) 196. ## 197. ## 1.3. Lendo dados de um arquivo ASCII (texto) 198. ## 199. ## exemplo: copiar os arquivos Cruciani.dat e Cruciani.border 200. ## para o diretrio dados sob o de trabalho. 201. ## (veja o link "dados" na sesso de tutoriais da pgina do curso) 202. ## 203. ## lendo dados 204. cru <- read.geodata("Cruciani.dat", head=T, coords.col=2:3, data.col=4) 205. cru 206. ## lendo arquivo com bordas da rea 207. cru.borda <- read.table("Cruciani.border", head=T)[,2:3] 208. cru.borda <- rbind(cru.borda, cru.borda[1,]) 209. ## 210. ## 211. ## 2. Anlise exploratria I - visualisando os dados 212. ## (usando funes/mtodos do pacote geoR) 213. ## 214. plot(cru, bord = cru.borda) 215. 216. ## Inspecting the options and documentation for the plot options 217. args(plot.geodata) 218. #help(plot.geodata) 219. 220. plot(cru, bord=cru.borda, lambda=0) 221. 222. hist(cru$data, main="", xlab="K")

223. hist(log(cru$data), main="", xlab="log(K)") 224. 225. points(cru, bord=cru.borda) 226. 227. args(points.geodata) 228. #help(points.geodata) 229. 230. points(cru, bord=cru.borda, cex.min=1, cex.max=1, pt.div="quartile") 231. points(cru, bord=cru.borda, cex.min=1, cex.max=1, col=gray(seq(0.9,0,l=length(cru$data)))) 232. points(cru, bord=cru.borda, cex.min=1, cex.max=1, col=gray(seq(0.9,0,l=length(cru$data))), xla="Coord X", ylab="Coord Y") 233. 234. points(cru, lambda=0, bord=cru.borda) 235. points(cru, lambda=0, bord=cru.borda, cex.min=1, cex.max=1, pt.div="quartile") 236. points(cru, lambda=0, bord=cru.borda, cex.min=1, cex.max=1, col=gray(seq(0.9,0,l=length(cru$data)))) 237. 238. ## 239. ## 240. ## 3. Anlise exploratria 2 - variogramas 241. ## 242. cru.v1 <- variog(cru) 243. plot(cru.v1) 244. cru.v1 245. names(cru.v1) 246. 247. args(variog) 248. #help(variog) 249. 250. cru.cl1 <- variog(cru, option = "cloud") 251. plot(cru.cl1) 252. 253. cru.v2 <- variog(cru, lambda = 0) 254. plot(cru.v2) 255. cru.cl2 <- variog(cru, lambda = 0, option = "cloud") 256. plot(cru.cl2) 257. 258. cru.v3 <- variog(cru, lambda = 0, max.dist=9) 259. plot(cru.v3) 260. 261. cru.v4 <- variog(cru, lambda = 0, uvec=seq(0,9,l=8)) 262. plot(cru.v4) 263. 264. cru.v5 <- variog(cru, lambda = 0, uvec=seq(0,12,l=8)) 265. plot(cru.v5) 266. 267. cru.v6 <- variog(cru, lambda = 0, uvec=seq(0,15,l=8)) 268. plot(cru.v6) 269. 270. 271. cru.v <- variog(cru, lambda = 0, uvec=seq(0,9,l=8), bin.cl=T) 272. plot(cru.v) 273. plot(cru.v, bin.cloud = T)

274. 275. cru.mv <- variog(cru, lambda = 0, uvec=seq(0,9,l=8), est="mod") 276. plot(cru.mv) 277. 278. ## 279. ## Sugestes: 280. ## - verifique outros argumentos para serem utilizados/explorados na funo variog() 281. ## - veja tambem a funo variog4() 282. 283. cru.v <- variog(cru, lambda = 0, uvec=seq(0,9,l=8), bin.cl=T) 284. cru.env <- variog.mc.env(cru, obj.variog = cru.v) 285. plot(cru.v, env = cru.env) 286. 287. ## 288. ## Sugesto: 289. ## - explore outros conjuntos de dados includos no pacote 290. ## 291. #data(package=geoR) 292. 293. data(wolfcamp) 294. #help(wolfcamp) 295. 296. data(parana) 297. #help(parana)

298.

Simulating geostatistical data 1. Simulating from the geostatistical model: simulations using the function grf illustrating different aspects of the geostatistical model.

299. ##################################### 300. ## 301. ## Simulando dados com a funo grf() 302. ## (simulao nao-condicional) 303. ## 304. ##################################### 305. ## 306. ## 1. Preparativos iniciais 307. ## -----------------------308. ## 309. ## 310. ## 1.1 Carregando o pacote geoR 311. ## 312. require(geoR) 313. ## 314. ## 1.2 Iniciando a ajuda no formato html (opcional) 315. ## 316. ## options(htmlhelp=TRUE) 317. ## help.start() 318. ## 319. ## 1.3 Salvando padres grficos iniciais e inicializando o gerador de numeros aleatorios

320. ## 321. par.ori <- par(no.readonly = TRUE) 322. if(!exists(".Random.seed")) set.seed(123) 323. ## 324. ## 1.4 Inspecionando os argumentos da funo grf() 325. ## 326. args(grf) 327. #help(grf) 328. ## 329. ## 2. Uma primeira simulao com argumentos minimos usando diversas opes "default" 330. ## -------------------------------------------------------------------------------331. ## 332. ## 2.1 simula dados: 333. ## 334. sim01 <- grf(50, cov.pars=c(1, .25)) 335. ## 336. ## 2.2 inspeciona objeto com dados simulados: 337. ## 338. sim01 339. ## 340. ## 2.3 inspeciona variogramas terico e emprico 341. ## 342. plot(sim01) 343. ## 344. ## 2.4 visualizando as localizaoes simuladas 345. ## 346. ## pode-se ainda visualizar as localizaes dos dados simulados 347. 348. #plot(sim01, plot.loc=T) 349. ## ou usar funes padrao de visualizao de dados 350. plot.geodata(sim01) 351. points.geodata(sim01) 352. ## 353. ## Nota: veja ainda opes destas funes com: 354. ## args(plot.geodata) 355. ## args(points.geodata) 356. ## 357. ## 358. ## 3. Uma segunda simulao em grid regular 359. ## ---------------------------------------360. ## 361. ## 3.1 Simulando dados 362. ## 363. sim02 <- grf(256, cov.pars=c(1, .3), grid="reg") 364. ## 365. ## 3.2 Visualizando as simulaes em grid regular 366. ## 367. points.geodata(sim02) 368. image(sim02) 369. persp(sim02) 370. ## e, se desejar mude o angulo de visualizao 371. persp(sim02, theta=30, phi=20) 372. ## 373. ## Exercicio 01: Explorando as opes graficas 374. ##

375. ## - inspecione outras opes em help(image.grf) e help(persp.grf) 376. ## e use estas opes para midoficar os graficos 377. ## 378. ## - use as outras funes de visualizao mencionadas anteriormente 379. ## (plot, plot.geodata, points.geodata) 380. ## 381. ## 382. ## Exercicio 02: Explorando outros argumentos da funo grf() 383. ## 384. ## - gere novas simulaes usando os argumentos: nx, ny, xlims e ylims 385. ## 386. ## 387. ## 4. Usando simulaes em 1D para compreender o modelo e seus parametros 388. ## --------------------------------------------------------------389. ## 390. ## 4.1 Simulando com diferentes alcances ("range") definidos pelo parametro $\phi$ 391. ## 392. sim11 <- grf(100, ylims=c(0,0), cov.pars=c(1, 0.25)) 393. image(sim11) 394. image(sim11, type="l") 395. image(sim11, type="b", cex=0.5) 396. ## 397. sim12 <- grf(100, ylims=c(0,0), cov.pars=c(1, 0.0)) 398. image(sim12, type="l") 399. ## 400. sim13 <- grf(100, ylims=c(0,0), cov.pars=c(1, 0.5)) 401. image(sim13, type="l") 402. ## 403. par(mfrow=c(3,1), mar=c(2,2,1,1)) 404. yl <- range(c(sim11$data, sim12$data, sim13$data)) 405. image(sim11, type="l", ylim=yl) 406. image(sim12, type="l", ylim=yl) 407. image(sim13, type="l", ylim=yl) 408. par(par.ori) 409. ## 410. ## Exercicio 03: 411. ## 412. ## - gere diferentes simulaes variando o parametro $\sigma^2$ no argumento cov.pars 413. ## e discuta os resultados 414. ## 415. ## 416. ## 4.2 Simulaes com diferentes modelos de variograma 417. ## 418. sim21 <- grf(100, ylims=c(0,0), cov.pars=c(1, 0.75), cov.model="sph") 419. image(sim21, type="l") 420. ## 421. sim22 <- grf(50, ylims=c(0,0), cov.pars=c(1, 0.75/sqrt(3)), cov.model="mat", kappa=2) 422. image(sim22, type="l")

423. ## 424. par(mfrow=c(3,1), mar=c(2,2,1,1)) 425. yl <- range(c(sim11$data, sim21$data, sim22$data)) 426. image(sim11, type="l", ylim=yl) 427. image(sim21, type="l", ylim=yl) 428. image(sim22, type="l", ylim=yl) 429. par(par.ori) 430. ## 431. ## 432. ## Exercicio 04: Explorando os modelos de variograma 433. ## 434. ## - gere e compare simulaes com o modelo de Matern 435. ## (cov.model="mat") com diferentes valores 436. ## no argumento kappa (tente, por exemplo valores 0.5, 2 e 5) 437. ## 438. ## - tente tambem com cov.model="gau" e observe se ha' 439. ## mensagens de alerta 440. ## 441. ## - gere e compare simulaes com outros modelos de variogramas 442. ## 443. ## 444. ## 4.3 Simulaes com ruido 445. ## 446. sim31 <- grf(50, ylims=c(0,0), cov.pars=c(1, 0.75/sqrt(3)), nugget = 0.1, cov.model="gau") 447. ## 448. ## repare novamente se ha' alguma mensagem de alerta (warning) ! 449. ## 450. image(sim31, type="l") 451. ## 452. ## Agora gerando simulaes com a mesma "semente" e com 453. ## diferentes nveis de rudo 454. ## 455. set.seed(12) 456. sim32 <- grf(100, ylims=c(0,0), cov.pars=c(1, 0.25)) 457. ## 458. set.seed(12) 459. sim33 <- grf(100, ylims=c(0,0), cov.pars=c(1, 0.25), nug=0.25) 460. ## 461. set.seed(12) 462. sim34 <- grf(100, ylims=c(0,0), cov.pars=c(1, 0.25), nug=0.5) 463. ## 464. par(mfrow=c(3,1), mar=c(2,2,1,1)) 465. yl <- range(c(sim32$data, sim33$data, sim34$data)) 466. image(sim32, type="l", ylim=yl) 467. image(sim33, type="l", ylim=yl) 468. image(sim34, type="l", ylim=yl) 469. par(par.ori) 470. ## 471. ## 472. ## Exercicio 05: Simulaes com ruido 473. ## 474. ## - gere em compare mais simulaes com differentes valores no argumento nugget 475. ## 476. ## 477. ## 5. Modelos com anisotropia

478. ## -------------------------479. ## 480. ## simulao sem estrutura espacial 481. sim39 <- grf(961, grid="reg", cov.pars=c(1, 0)) 482. image(sim39) 483. ## simulao com estrutura espacial isotropica 484. sim40 <- grf(961, grid="reg", cov.pars=c(1, .15)) 485. image(sim40) 486. ## simulao com estrutura espacial anisotropica 487. sim41 <- grf(961, grid="reg", cov.pars=c(1, .15), aniso.pars=c(pi/4, 3)) 488. image(sim41) 489. ## 490. ## Exercicio 06: Simulaes com anisotropia 491. ## 492. ## - gere em compare simulaes com differentes valores no argumento aniso.pars 493. ## 494. ## 495. ## 6. Simulando modelos com tendencia 496. ## ---------------------------------497. ## A funo grf gera simulaes com media constante e igual a zero. 498. ## Para gerar simulaes com media diferente de zero basta somar 499. ## quantidades desejadas aos dados simulados como no exemplo a seguir 500. ## 501. sim51 <- grf(100, ylims=c(0,0), cov.pars=c(1, 0.25)) 502. sim51$data <- 10 - 5*sim51$coords[,1] + sim51$data 503. image(sim51, ty="l") 504. ## 505. ## 506. ## 7. Simulaes de variaveis nao-Gaussianas 507. ## 508. ## Simulao de variavel log-Normal 509. ## 510. sim61 <- grf(961, grid="reg", cov.pars=c(1, 0.25), lambda = 0) 511. image(sim61) 512. hist(sim61$data) 513. ## 514. ## Simulao de variavel Poisson 515. ## 516. sim71 <- grf(121, grid="reg", cov.pars=c(1, 0.25)) 517. sim71$data <- rpois(121, exp(0.5 + sim71$data)) 518. image(sim71, col=gray(seq(1,0.2,l=11))) 519. text(sim71$coords[,1], sim71$coords[,2], sim71$data) 520. barplot(table(sim71$data)) 521. ## 522. ## Exercicio 07: Simulaes de variaveis nao-Gaussianas 523. ## 524. ## - gere, explore e compare simulaes com differentes valores no argumento lambda 525. ## - gere simulaes com variavel resposta Binomial 526. ## 527. ## 528. ## 8. Gerando simulaes em grids muito finos (malha com muitos pontos)

529. 530. 531. 532. 533. 534. 535.

## ## ## ## ## ## ##

Use o pacote RandomFields ======================================================= Para mais sobre simulaes veja o pacote RandomFields. =======================================================

1. Animated 2D simulations: (be patient! this may take a while to load in your browser) http://leg.ufpr.br/geoR/tutorials/sim2D.html

Simulations from the Gaussian Geostatistical Model


1. Simulations with different values for the correlation parameter phi o Click here for an animation with 10 simulations (It may take a while for loading the image)

Click here for the individual simulations


simulation 01 simulation 02 simulation 03 simulation 04 simulation 05 simulation 06 simulation 07 simulation 08 simulation 09 simulation 10

2. Simulations with different values for the parameter sigmasq


o

Click here for an animation with 10 simulations (It may take a while for loading the image)

Click here for the individual simulations


simulation 01 simulation 02 simulation 03 simulation 04 simulation 05 simulation 06 simulation 07 simulation 08 simulation 09 simulation 10

3. Simulations with different values for the nugget parameter tausq


o

Click here for an animation with 10 simulations (It may take a while for loading the image)

Click here for the individual simulations


simulation 01 simulation 02 simulation 03 simulation 04 simulation 05 simulation 06 simulation 07 simulation 08 simulation 09 simulation 10

4. Simulations with different correlation functions


o

Click here for an animation with 10 simulations (It may take a while for loading the image)

Click here for the individual simulations


simulation 01 simulation 02 simulation 03 simulation 04 simulation 05 simulation 06 simulation 07 simulation 08 simulation 09 simulation 10

5. Simulations with different degrees of anisotropy


o

Clique para ver animation com 10 simulations (It may take a while for loading the image)

Click here for the individual simulations


simulation 01 simulation 02 simulation 03 simulation 04 simulation 05 simulation 06 simulation 07 simulation 08 simulation 09 simulation 10

See the file with the commands used to generate these simulations.

This page has been translated from Portuguese, 9 October 2002 (Ole Christensen).

2. Simulating from the generalised linear geostatistical model.


536. 537. 538. 539. 540. 541. 542. 543. 544. 545. 546. 547. 548. 549. 550. 551. 552. 553. 554. 555. 556. 557. 558. 559. 560. 561. 562. 563. 564. 565. 566. 567. 568. 569. 570. 571. 572. 573. 574. 575. 576. 577. 578. 579. 580. 581. ## ## Simulando do Modelo Linear Generalizado ## ## Exemplo 1: Modelo Binomial (Bernoulli) ## com ligao "logit" ## valores da covarivel: x <- c(23, 28, 25, 27, 32, 45, 51) n <- length(x) ## Definindo o valor do parmetro $\beta$ beta <- .03 ## calculando o vetor de mdias $\mu$ mu <- exp(x * beta)/(1+ exp(x*beta)) mu ## Simulando dados y <- rbinom(n, size=1, prob=mu) y ## Exemplo 2: Modelo Poisson ## com ligao "log" ## (usando a mesma covarivel) beta <- 0.05 ## calculando o vetor de mdias $\mu$ mu <- exp(x * beta) mu ## Simulando dados y <- rpois(n, lam=mu) y ## Exemplo 3: Modelo Poisson com efeito aleatrio ## com ligao "log" beta <- 0.05 x * beta ## simulando o efeito aleatrio u <- rnorm(n, sd=1) u ## termo $X\beta + U$ x * beta + u ## calculando o vetor de mdias $\mu$ mu <- exp(x * beta + u) ## Simulando dados com efeito aleatrio

582. 583. 584. 585. 586. 587. 588. 589. 590. 591. 592. 593. 594. 595. 596. 597. 598. 599. 600. 601. 602. 603. 604. 605. 606. 607. 608. 609. 610. 611. 612. 613. 614. 615. 616. 617. 618. 619. 620. 621. 622. 623. 624. 625. 626. 627. 628. 629. 630. 631. 632. 633. 634. 635. 636. 637. 638.

set.seed(123) y <- rpois(n, lam=mu) y ## Mesma simulao sem o efeito aleatrio set.seed(123) y <- rpois(n, lam=exp(x*beta)) y ## Exemplo 4: Simulando do modelo Poisson ## com efeito espacial ## com funo de ligao "log" ## definindo as coordenadas dos pontos cp <- expand.grid(1:10, 1:10) cp plot(cp, asp=1) ## simulando valores nestes pontos require(geoR) s <- grf(grid=cp, cov.pars=c(2, 3))$data ## calculando a mdia mu <- exp(s) ## simulando da Poisson y <- rpois(100, lam=mu) plot(cp, asp=1, type="n") text(cp[,1], cp[,2], y, cex=2) ## visualizando S e Y par(mfrow=c(1,2), mar=c(3,3,0.5,0.5), mgp=c(2,1,0)) plot(cp, asp=1, type="n") text(cp[,1], cp[,2], round(s, dig=2), cex=1.5) plot(cp, asp=1, type="n") text(cp[,1], cp[,2], y, cex=1.5) ## ## Mais exemplos sobre simulao de modelos Poisson e Binomial ## ## ## I. Simulando do modelo Poisson - log link ## ## $$Y(x) \sim P(\lambda(x))$$ ## 1. ## simulando com intensidade proporcional S(x) ## $$\lambda(x) = \exp(S(x)) $$ set.seed(23) S <- grf(100, cov.pars=c(1, 0.25)) S1 <- S S1$data <- exp(S$data) set.seed(351) y1 <- rpois(100, lam=S1$data) points(S1)

639. text(S1$coords, lab=y1, col=4,pos=4, offset=0.3) 640. 641. ## 2. adicionando uma mdia contante 642. ## $$\lambda(x) = \exp(\mu + S(x)) $$ 643. S2 <- S 644. S2$data <- exp(1 + S$data) 645. set.seed(351) 646. y2 <- rpois(100, lam=S2$data) 647. 648. points(S2) 649. text(S2$coords, lab=y2, col=4,pos=4, offset=0.3) 650. 651. ## 3. adicionando um rudo ao processo S 652. ## $$\lambda(x) = \exp(\mu + S(x) + \epsilon) $$ 653. S3 <- S 654. err <- rnorm(100, sd=0.1) 655. S3$data <- exp(1 + S$data + err) 656. set.seed(351) 657. y3 <- rpois(100, lam=S3$data) 658. 659. points(S3) 660. text(S3$coords, lab=y3, col=4,pos=4, offset=0.3) 661. 662. ## comparando os valores e termos 663. round(cbind(S=S$data, lam1=S1$data, y1, lam2=S2$data, y2, lam3=S3$data, err, y3), dig=3) 664. 665. ## 666. ## II. Simulando do modelo Binomial(n=1) - logit link 667. ## 668. ## $$Y(x) \sim P(\lambda(x))$$ 669. 670. ## 1. 671. ## simulando com intensidade proporcional S(x) 672. ## $$\lambda(x) = \frac{\exp(\mu+S(x))}{1+\exp(S(x))} $$ 673. S4 <- S 674. S4$data <- exp(S$data)/(1+exp(S$data)) 675. set.seed(351) 676. y4 <- rbinom(100, size=1, prob=S4$data) 677. y4 678. sum(y4) 679. points(S4) 680. text(S4$coords, lab=y1, col=4,pos=4, offset=0.3) 681. 682. ## 2. simulando com intensidade proporcional S(x) + uma mdia cte 683. ## $$\lambda(x) = \frac{\exp(\mu+S(x))}{1+\exp(\mu+S(x))} $$ 684. S5 <- S 685. S5$data <- exp(2+S$data)/(1+exp(2+S$data)) 686. set.seed(351) 687. y5 <- rbinom(100, size=1, prob=S5$data) 688. y5 689. sum(y5) 690. points(S5) 691. text(S5$coords, lab=y1, co

692.

Illustrating geostatistical calculations step by step 1. Example on the simple kriging algorithm.

693. ## Definindo parmetros 694. mu <- 5 695. sigma2 <- 3 696. phi <- 2 # funo de correlao exponencial 697. tau2 <- 0.5 698. 699. ## Dados 700. y <- c(10,7,8) 701. y 702. n <- length(y) 703. n 704. ## Coordenadas dos dados 705. x <- matrix(c(1,4,3,4,3,1), nc=2) 706. x 707. ## Coordenada do ponto a ser predito 708. x0 <- c(2,2) 709. x0 710. ## Calculando distncias entre dados 711. d <- dist(x) 712. d 713. ## Calculando a matrix R de correlao entre dados 714. R <- exp(-d/phi) 715. R 716. R <- as.matrix(R) 717. R 718. diag(R) <- 1 719. R 720. ## Calculando distncias entre cada dado e o ponto a ser predito 721. d0 <- dist(rbind(x0,x))[1:n] 722. d0 723. ## Calculando a matrix r de correlao entre dados e ponto a ser predito 724. r <- exp(-d0/phi) 725. r 726. ## Encontrando o valor da estimativa 727. y0 <- mu + 728. (sigma2*t(r))%*%solve(tau2*diag(n)+sigma2*R)%*%(y - mu) 729. y0 730. 731. ## e a varincia da estimativa 732. v.y0 <- sigma2 733. (sigma2*t(r)) %*% solve(tau2*diag(n)+sigma2*R) %*% (sigma2*r) 734. v.y0

1. Example of some geostatistical calculations.


735. ## 736. ## Ilustrando os clculos dos passos bsicos de uma anlise geoestatstica

737. ## 738. 739. ## 1) Lendo os dados 740. ## -------------741. ##xy <read.table("http://www.leg.ufpr.br/geoR/tutorials/datasets/xy.dat") 742. xy <- read.table("xy.dat") 743. xy 744. x <- xy[,1:2] 745. x 746. y <- xy[,3] 747. y 748. n <- length(y) 749. n 750. 751. ## visualizando 752. plot(x, asp=1) 753. text(x, lab=as.character(y), pos=3) 754. 755. ## especificando ponto a ser predito 756. x0 <- c(4,4) 757. x0 758. 759. points(t(x0), pch="?", col=2, cex=2) 760. 761. ## 2) Clculos da estimao do variograma 762. ## ----------------------------------763. ## Calculando matrix de distncias (euclidianas) entre dados 764. u <- dist(x) 765. u 766. 767. ## Calculando a semivarincia para cada par de pontos 768. dist(y) 769. v <- as.vector(0.5 * (dist(y)^2)) 770. v 771. 772. ## grfico da nuvem variogrfica 773. ## (note que o grfico deve incluir a origem) 774. plot(u, v, xlim=c(0, max(u)), ylim=c(0, max(v))) 775. 776. ## calculando variograma em classes de distncia 777. ## limites das classes de distncia: (0, 2, 4, 6, 8) 778. ## pontos mdios : (1, 3, 5, 7) 779. abline(v=c(0,2,4,6,8), lty=2) 780. ind <- cut(u, br=c(0,2,4,6,8)) 781. ind 782. vm <- tapply(v, ind, mean) 783. um <- c(1,3,5,7) 784. 785. points(um, vm, pch="x", col=4, cex=2) 786. 787. plot(um, vm, xlim=c(0, max(um)), ylim=c(0, max(vm, na.rm=T))) 788. 789. ## ajustando o modelo gaussiano ("a olho") 790. plot(function(x) 0.5 + 5 * (1 - exp(-(x/3)^2)), 0, 7, add=T) 791. 792. ## considerando que as estimativas dos parmetros so:

793. tau2 = 0.5 794. sigma2 = 5 795. phi = 3 796. 797. ## 3) Estimando a mdia geral 798. ## ----------------------799. ## Calculando a matrix R de correlao entre dados 800. R <- exp(-(u/phi)^2) 801. R 802. R <- as.matrix(R) 803. R 804. diag(R) <- 1 805. R 806. ## Matrix de varincias e covarincias 807. Sigma <- tau2 * diag(5) + sigma2 * R 808. 809. ## Estimando a mdia 810. um <- rep(1,5) 811. mu <- solve(t(um) %*% solve(Sigma) %*% um) %*% (t(um) %*% solve(Sigma) %*% y) 812. mu 813. 814. ## 4) Predio em um certo ponto (krigagem) 815. ## -----------------------------------816. ## Estimando ponto no posio (4,4) 817. ## Calculando distncias entre cada dado e o ponto a ser predito 818. u0 <- dist(rbind(x0,x))[1:n] 819. u0 820. ## Calculando a matrix r de correlao entre dados e ponto a ser predito 821. r <- exp(-(u0/phi)^2) 822. r 823. ## Encontrando o valor da estimativa 824. y0 <- mu + 825. (sigma2*t(r))%*%solve(tau2*diag(n)+sigma2*R)%*%(y - mu) 826. y0 827. 828. ## e a varincia da estimativa 829. v.y0 <- sigma2 830. (sigma2*t(r)) %*% solve(tau2*diag(n)+sigma2*R) %*% (sigma2*r) 831. v.y0 832. 833. ## estimando probabilidades 834. ## i. P(Yest > 10) 835. pnorm(10, m=y0, sd=sqrt(v.y0), low=F) 836. ## ii P(Yest < 11) 837. pnorm(11, m=y0, sd=sqrt(v.y0)) 838. 839. ## estimar quantis 840. ## i. a=? tal que P(Yest < a) = 0.1 841. qnorm(0.1, m=y0, sd=sqrt(v.y0)) 842. ## i. a=? tal que P(Yest > a) = 0.1 843. qnorm(0.9, m=y0, sd=sqrt(v.y

1. Constructing a covariate and obtaining the confidence interval for the coeficcient.
844. ## Ilustrando: 845. ## - a montagem de uma covarivel definida como a distncia 846. ## da cada ponto a uma certa localidade 847. ## - a obteno de IC para coeficiente de uma covarivel 848. require(geoR) 849. ## carregando um conjunto de dados 850. cru <- read.geodata("Cruciani.dat", head=T, coords.col=2:3, data.col=4) 851. cru.borda <- read.table("Cruciani.border", head=T)[,2:3] 852. cru.borda <- rbind(cru.borda, cru.borda[1,]) 853. points(cru) 854. 855. ## marcando a localidade no mapa 856. #pt <- locator() 857. #pt 858. #pt <- unlist(pt) 859. #pt 860. pt <- c(14,9) 861. 862. ## montando a covarivel: 863. ## calculando a distncia da localidade a cada ponto 864. d1 <- dist(rbind(pt, cru$coords))[1:32] 865. 866. ## variograma aps "remoo" da tendncia 867. variog(cru, trend=~d1) 868. 869. ## ajustando o modelo com a covarivel 870. ml <- likfit(cru, ini=c(2, 3), trend=~d1) 871. ml 872. 873. ## vendo as estimativas dos parmetros de mdias, seus erros padro 874. summary(ml) 875. names(ml) 876. ml$beta 877. ml$beta.var 878. ## montando o IC (95%) para o coeficiente da covarivel 879. ml$beta[2] + qnorm(c(0.025, 0.975)) * sqrt(ml$beta.var[2,2])

1. Kriging with covariates: example on how to construct a covariate on the prediction points to be used in the kriging calculations.

## ## Spatial prediction (kriging) using geoR function krige.conv() ## in the presence of cavariate (external trend)

## loading the package require(geoR) ## loading data set \code{ca20} data(ca20) ## more on this data with ## help(ca20) ## a quick data summary summary(ca20) ## names in the data object names(ca20) ## visualising data and the division of the study area in sub-areas points(ca20, borders=borders) polygon(ca20$reg1) polygon(ca20$reg2) polygon(ca20$reg3) ## Consider the following model for the mean of the proccess ## covariates : a linear trend with the Y coordinate ## + an effect of the sub-areas ## ## model for the correlation function: exponential ## ## an variogram as an exploratory tool v <- variog(ca20, trend=~area+ca20$coords[,2]) plot(v) ## model parameter estimates using maximum likelihood ml <- likfit(ca20, trend=~area+ca20$coords[,2], ini=c(100, 400), nug=40) ml ## Spatial prediction (kriging) ## a rectangular grid over the area gr0 <- pred_grid(ca20$borders, by=50) ## visialising the prediction grid points(ca20) points(gr0, pch="+") ## selecting prediction points within the area gr <- polygrid(gr0, bor=ca20$borders) points(gr, pch="+", col=2) ## creating the covariate "gr.area" for the prediction locations ## indicating the sub-area each prediction point belongs to gr.area <- numeric(nrow(gr)) gr.area[.geoR_inout(gr, poly=ca20$reg1)] <- 1 gr.area[.geoR_inout(gr, poly=ca20$reg2)] <- 2 gr.area[.geoR_inout(gr, poly=ca20$reg3)] <- 3 gr.area <- as.factor(gr.area)

## checking the covariate points(ca20, borders=borders) polygon(ca20$reg1) polygon(ca20$reg2) polygon(ca20$reg3) text(gr[,1], gr[,2], as.character(gr.area)) length(gr.area) length(gr[,2]) dim(gr) ## setting the kriging options KC <- krige.control(trend.d=~area+ca20$coords[,2], trend.l=~gr.area+gr[,2], obj=ml) ## performing the spatial prediction kr <- krige.conv(ca20, loc=gr0, borders=ca20$borders, krige=KC) ## visualising the prediction image(kr, col=gray(seq(1,0,l=51))) polygon(ca20$reg1) polygon(ca20$reg2) polygon(ca20$reg3) ## an alternative way to run kriging withing the borders of the area kr <- krige.conv(ca20, loc=gr, krige=KC) image(kr, col=gray(seq(1,0,l=51)), loc=gr0, bor=ca20$b)

2. Kriging in presence of covariates: a note: comments on the usage of the arguments trend.l and trend.d.
880. ## 881. ## A note on the kriging with external trend 882. ## 883. 884. To perform " kriging with external trend" and/ou "universal kriging" 885. you need the value of the covariates on both: 886. data locations and prediction locations. 887. 888. Therefore in geoR implementation you use: 889. - "trend.d" to specify the trend values at data locations 890. - "trend.l" to specify the trend values at prediction locations 891. 892. For example to perform a kriging 893. with a 2nd degree polynomial + a covariate 894. you will need the values of this covariate at both, 895. data and prediction locations. 896. 897. Consider the following example having the objects: 898. 899. "foo" : is a geodata object with a covariate called "foocov" 900. "gr" : is a matrix with coordinates of the prediction locations 901. "grcov" : is the covariate values at the prediction locations

902. 903. Now assume you are performing kriging with 2nd degree polynomial trend 904. plus the covariate. 905. The code would be something along the lines: 906. 907. > kc <- krige.control(obj.m=fit, 908. trend.d = trend.spatial("2nd", geodata=foo, 909. add = ~foocov), 910. trend.l = trend.spatial("2nd", geodata=list(coords=gr), 911. add = ~grcov)) 912. > kc <- krige.conv(foo, loc=gr, krige=kc)

913.

Generic tutorials 1. A traditional geostatistical data analysis: example of the basic steps for a simple geostatistical analysis.

914. ## Lendo os dados no R 915. ctc <read.table("http://www.leg.ufpr.br/geoR/tutorials/datasets/ctc.dat" , head=T) 916. ## Visualizando os 5 primeiros dados 917. ctc[1:5,] 918. 919. require(geoR) 920. ## colocando os dados no formato geodata da geoR 921. ctc <- as.geodata(ctc) 922. 923. ## Visualizando os dados 924. points(ctc) 925. ## mudando algumas opcoes de visualizacao 926. args(points.geodata) 927. points(ctc, pt.div= "equal") 928. points(ctc, pt.div= "equal", cex.max=1.2) 929. points(ctc, pt.div= "quint", cex.max=1, cex.min=1) 930. 931. ## adicionando as bordas 932. bor <read.table("http://www.leg.ufpr.br/geoR/tutorials/datasets/ctcborda .dat", head=T) 933. bor 934. polygon(bor) 935. ## 936. points(ctc, pt.div= "quint", cex.max=1, cex.min=1, bord=bor) 937. 938. ## um outro grafico para visualizar os dados 939. plot(ctc) 940. #args(plot.geodata) 941. #help(plot.geodata) 942. ## adicionando uma opcao para ajudar visualizar tendencia 943. plot(ctc, low=T) 944. 945. ## resumo dos dados

946. summary(ctc) 947. 948. ## box-plot 949. boxplot(ctc$data) 950. 951. ## verificando a necessidade de transformacao de dados 952. require(MASS) 953. boxcox(ctc) 954. ## dando ukm zoom na regiao de interesse 955. boxcox(ctc, lambda=seq(-1,1,l=20)) 956. ## indicou a transformacao logaritmica 957. 958. ## refazendo graficos 959. points(ctc, pt.div= "quint", cex.max=1, cex.min=1, bord=bor, lam=0) 960. plot(ctc, low=T, lam=0) 961. par(mfrow=c(2,2)) 962. hist(ctc$data) 963. hist(log(ctc$data)) 964. boxplot(ctc$data) 965. boxplot(log(ctc$data)) 966. par(mfrow=c(1,1)) 967. 968. ## Descrevendo a dependncia espacial 969. ctc.v <- variog(ctc, max.dist=20, lam=0) 970. plot(ctc.v) 971. 972. ## outras opcoes 973. ctc.v <- variog(ctc, uvec=seq(0,25,l=11), lam=0) 974. plot(ctc.v) 975. 976. ## ajustando uma curva ao variograma 977. ## no olhometro 978. #ctc.va <- eyefit(ctc.v) 979. 980. ## por minimos quadrados 981. ctc.vf <- variofit(ctc.v, ini=c(0.2, 10), cov.model= "exp") 982. lines(ctc.vf) 983. 984. ctc.vf 985. 986. ## interpolando 987. apply(bor,2,range) 988. 989. gr <- expand.grid(x=seq(-3.6, 38.5, by=.25), y=seq(0,23.6, by=.25)) 990. points(ctc, pt.div= "quint", cex.max=1, cex.min=1, bord=bor, lam=0) 991. points(gr, pch="+") 992. 993. gi <- polygrid(gr, bor=bor) 994. points(gi, pch="+", col=2) 995. 996. KC <- krige.control(obj.mo=ctc.vf, lam=0) 997. ctc.k <- krige.conv(ctc, loc=gi, krige=KC) 998. 999. image(ctc.k, loc=gr, border=bor, col=gray(seq(1,0,l=11)))

1000. image(ctc.k, loc=gr, border=bor, col=gray(seq(1,0,l=21)), x.leg=c(0,30), y.leg=c(-8,-4))

1. Some commands for a standard geostatistical analysis: illustrate the package syntax for exploratory analysis (including variograms), parameter estimation and kriging prediction.
1001. # fixando parmetros grficos 1002. par.ori <- par(no.readonly=T) 1003. 1004. # carregando o pacote geoR 1005. require(geoR) 1006. 1007. # Lendo os dados de um arquivo texto no R 1008. cru.df <- read.table("Cruciani.dat", head=T) 1009. cru.df 1010. cru.df$Ksat 1011. 1012. ## Outra forma de ler os dados para anlise geoestatstica 1013. ## Lendo os dados no formato "geodata" (para uso no pacote geoR) 1014. args(read.geodata) 1015. cru <- read.geodata("Cruciani.dat", head=T, coords=2:3, data.col=4) 1016. cru 1017. 1018. ## Lendo arquivo com bordas da regio 1019. cru.b <- as.matrix(read.table("Cruciani.border", head=T)[,2:3]) 1020. 1021. ## O objeto criado uma lista, com componentes coords e data 1022. is.list(cru) 1023. names(cru) 1024. cru$coords 1025. ## ... e da classe "geodata" 1026. class(cru) 1027. 1028. ## inspecionando e manipulando os elementos da lista. 1029. ## Alguns exemplos: 1030. min(cru$coords[,1]) 1031. max(cru$coords[,1]) 1032. min(cru$coords[,2]) 1033. max(cru$coords[,2]) 1034. 1035. # valores mnimos e mximos nas coordenadas 1036. apply(cru$coords,2,range) 1037. # menor e maior distncia entre pares 1038. range(dist(cru$coords)) 1039. 1040. # alguns resumos dos dados 1041. summary(cru$data) 1042. hist(cru$data) 1043. boxplot(cru$data)

1044. 1045. # e um resumo geral fornecido pelo pacote geoR 1046. summary(cru) 1047. 1048. # um primeiro grfico com descrio espacial dos dados 1049. plot(cru) 1050. # vendo outros argumentos desta funo 1051. args(plot.geodata) 1052. # adicionando as bordas 1053. plot(cru, bor=cru.b) 1054. 1055. ## 1056. ## Verificando tendncias nos dados 1057. ## 1058. 1059. ## inpecionando outro conjunto de dados 1060. data(parana) 1061. ## vendo informaes sobre os dados 1062. help(parana) 1063. 1064. ## visualizando os dados 1065. plot(parana) 1066. plot(parana, bord=parana$bor) 1067. ## "removendo" a tendncia e visualizando os resduos 1068. plot(parana, trend="1st", bord=parana$bor) 1069. 1070. ## 1071. ## verificando necessidade de transformao 1072. ## 1073. ## dados cru so muito assimtricos 1074. ## buscando uma transformao adequada 1075. hist(log(cru$data)) 1076. length(cru$data) 1077. boxplot(log(cru$data)) 1078. plot(cru, lam=0, bor=cru.b) 1079. 1080. ## transformao BoxCox para dados independentes 1081. require(MASS) 1082. boxcox(cru$data~1) 1083. # dando um zoom na regio de interesse 1084. boxcox(cru$data~1, lam=seq(-1,1,l=21)) 1085. 1086. ## outra funo para visualizao dos dados 1087. points(cru, lambda=0, bor=cru.b) 1088. # e ilustrando algumas outras opes desta funo 1089. points(cru, lambda=0, pt.div="equal", bor=cru.b) 1090. points(cru, lambda=0, pt.div="equal", col=gray(seq(1,0, l=11)), bor=cru.b) 1091. # examinando todos os argumentos da funo 1092. args(points.geodata) 1093. 1094. ## 1095. ## Variogramas 1096. ## 1097. 1098. ## dados originais 1099. cru.vario <- variog(cru)

1100. plot(cru.vario) 1101. 1102. ## dados transformados (log) 1103. cru.vario <- variog(cru, lam=0) 1104. plot(cru.vario) 1105. 1106. ## mudando outros argumentos 1107. args(variog) 1108. 1109. # reduzindo a distncia mxima e experimentando com outras classes de distncia 1110. cru.vario <- variog(cru, lam=0, max.dist=10) 1111. plot(cru.vario) 1112. 1113. cru.vario <- variog(cru, lam=0, uvec=seq(0,10,l=11)) 1114. plot(cru.vario) 1115. 1116. cru.vario <- variog(cru, lam=0, uvec=c(0, 1, 2, 4, 7, 10)) 1117. plot(cru.vario) 1118. 1119. cru.vario <- variog(cru, lam=0, max.dist=10) 1120. plot(cru.vario) 1121. 1122. # verificando se h algum efeito de tendncia 1123. cru.vario <- variog(cru, lam=0, max.dist=10, trend="1st") 1124. plot(cru.vario) 1125. 1126. ## variogramas sem e com com remoo de tendncia nos dados do Paran 1127. parana.vario <- variog(parana, max.dist=400) 1128. plot(parana.vario) 1129. 1130. parana.vario <- variog(parana, max.dist=400, trend="1st") 1131. plot(parana.vario) 1132. 1133. ## envelope de variograma para testar a presenca de dependencia espacial 1134. parana.vario.env <- variog.mc.env(parana, obj.v=parana.vario) 1135. plot(parana.vario, env=parana.vario.env) 1136. 1137. ## Um teste Monte Carlo para verificar evidncia da existncia de dependncia espacial 1138. ## nos dados de condutividade saturada 1139. cru.vario <- variog(cru, lam=0, max.dist=10) 1140. plot(cru.vario) 1141. cru.vario.env <- variog.mc.env(cru, obj.v=cru.vario) 1142. plot(cru.vario, env=cru.vario.env) 1143. 1144. 1145. ## Variogramas direcionais para dados do Parana' 1146. var2 <- variog(parana, max.dist=400, dir=pi/2, trend="1st", uvec=seq(0,400,l=8)) 1147. plot(var2, col="blue", ty="l") 1148. var1 <- variog(parana, max.dist=400, dir=0, trend="1st", uvec=seq(0,400,l=8)) 1149. lines(var1)

1150. ## fazendo vrias direes de uma s vez 1151. plot(variog4(parana, max.d=500, trend="1st", uvec=seq(0, 350, l=10))) 1152. 1153. ## 1154. ## "Estimando" parmetros usando o variograma 1155. ## 1156. ## variograma escolhido 1157. cru.vario <- variog(cru, lam=0, max.dist=10) 1158. plot(cru.vario) 1159. 1160. ## ajustando (estimando parmetros) "pelo olho" ("a sentimento") 1161. ## Alguns exemplos 1162. args(lines.variomodel.default) 1163. lines.variomodel(seq(0,10,l=100), nug=0.5, cov.pars=c(1.5, 2.5), cov.model="exp", max.dist=10) 1164. lines.variomodel(seq(0,10,l=100), nug=0.5, cov.pars=c(2, 2.5), cov.model="exp", max.dist=10, lty=2) 1165. lines.variomodel(seq(0,10,l=100), nug=0, cov.pars=c(2.3, 2.5), cov.model="exp", max.dist=10, lwd=2) 1166. lines.variomodel(seq(0,10,l=100), nug=0, cov.pars=c(2.3, 7.5), cov.model="sph", max.dist=10, lwd=2, lty=2) 1167. 1168. ## ou ento use a funo interativa eyefit() 1169. #cru.ef <- eyefit(cru.vario) 1170. 1171. ## ajustando (estimando parmetros) usando mnimos quadrados 1172. args(variofit) 1173. 1174. ## mnimos quadrados ordinrios 1175. cru.ols <- variofit(cru.vario, ini=c(2.3, 2.5), wei="equal", min="optim") 1176. cru.ols 1177. 1178. ## mnimos quadrados ponderados 1179. cru.wls <- variofit(cru.vario, ini=c(2.3, 2.5)) 1180. cru.wls 1181. 1182. plot(cru.vario) 1183. lines(cru.ols, lty=2, col="blue", lwd=3) 1184. lines(cru.wls, col="red", lwd=2) 1185. 1186. # Validao cruzada 1187. args(xvalid) 1188. cru.xvm1 <- xvalid(cru, model=cru.wls) 1189. names(cru.xvm1) 1190. summary(cru.xvm1) 1191. par(mfcol=c(5,2), mar=c(2.5,2.5,0.5, .5), mgp=c(1.7,.8,0)) 1192. plot(cru.xvm1) 1193. par(par.ori) 1194. 1195. ## envelope para modelo ajustado 1196. args(variog.model.env) 1197. cru.vario.env.mod <- variog.model.env(cru, model.pars=cru.wls, obj.variog=cru.vario)

1198. plot(cru.vario, env=cru.vario.env.mod) 1199. 1200. ## ajustando um outro modelo: modelo Matrn com kappa=2 1201. cru.wls.mat <- variofit(cru.vario, ini=c(2.3, 2.5), cov.model="matern", kappa=2) 1202. cru.wls.mat 1203. plot(cru.vario) 1204. lines(cru.wls.mat, col="darkgreen", lwd=2) 1205. 1206. ## 1207. ## Predio espacial 1208. ## 1209. ## definindo 2 pontos a serem preditos 1210. loci <- matrix(c(10,15,5,5), ncol=2) 1211. loci 1212. 1213. points(cru, lambda=0, pt.div="equal", bor=cru.b) 1214. text(loci, lab=1:2) 1215. 1216. ## usando a funo de krigagem (interpolao espacial) 1217. args(krige.conv) 1218. help(krige.conv) 1219. 1220. cru.kc <- krige.conv(cru, loc=loci, krige=krige.control(obj=cru.wls)) 1221. # examinando valores preditos e varincias de predio 1222. cru.kc[1:2] 1223. 1224. ## Outros resultados que podem ser obtidos: predio de probabilidades e quantis 1225. ## Probabilidade de Ksat ser maior que 1.5 em cada um dos pontos 1226. OC <- output.control(thres=1.5, quan=c(.25,.5,.75)) 1227. cru.kc <- krige.conv(cru, loc=loci, krige=krige.control(obj=cru.wls), output=OC) 1228. # valores preditos 1229. cru.kc$pred 1230. # probabilidades estimados do valor ser menor que 1.5 nos pontos selecionados 1231. cru.kc$prob 1232. # probabilidades estimados do valor ser maior que 1.5 nos pontos selecionados 1233. 1-cru.kc$prob 1234. # quantis (quartis neste caso) 1235. cru.kc$quant 1236. # examinando o objeto retornado pela funo 1237. names(cru.kc) 1238. # examinando a dimenso da matrix que guarda as simulaes 1239. dim(cru.kc[[5]]) 1240. 1241. ## Predizendo em uma malha de pontos cobrindo a rea de interesse 1242. ## definindo um grid regular 1243. points(cru, lambda=0, pt.div="equal", bor=cru.b) 1244. 1245. loci0 <- expand.grid(seq(0,24,l=40), seq(-0.5,13, l=30)) 1246. points(loci0, pch="+")

1247. 1248. loci1 <- polygrid(seq(0,24,l=40), seq(-0.5,13, l=30), cru.b) 1249. points(loci1, col="blue", pch="+") 1250. 1251. cru.loci1 <- krige.conv(cru, loc=loci1, krige=krige.control(obj=cru.wls)) 1252. 1253. # examinando resultados 1254. names(cru.loci1) 1255. cru.loci1$pred 1256. cru.loci1$krige.var 1257. 1258. ## mapeando valores preditos 1259. args(image.kriging) 1260. image(cru.loci1, border=cru.b, loc=loci0) 1261. #fix(prepare.graph.kriging) 1262. image(cru.loci1, border=cru.b, loc=loci0) 1263. ## adicionando legenda 1264. image(cru.loci1, border=cru.b, loc=loci0, x.leg=c(2,21), y.leg=c(-4,-2)) 1265. ## trocando padro de cores 1266. image(cru.loci1, border=cru.b, loc=loci0, col=gray(seq(1,0,l=21)), x.leg=c(2,21), y.leg=c(-4,-2)) 1267. ## e agora incluindo a localizao dos dados 1268. image(cru.loci1, border=cru.b, loc=loci0, col=gray(seq(1,0,l=21)), x.leg=c(2,21), y.leg=c(-4,-2), coords.dat=cru$coords) 1269. 1270. ## Outros tipo de grfico para visualizao dos resultados 1271. persp(cru.loci1, border=cru.b, loc=loci0) 1272. persp(cru.loci1, border=cru.b, loc=loci0, theta=20) 1273. persp(cru.loci1, border=cru.b, loc=loci0, theta=20, phi=30) 1274. 1275. contour(cru.loci1, border=cru.b, loc=loci0) 1276. 1277. image(cru.loci1, border=cru.b, loc=loci0, col=gray(seq(1,0,l=21)), x.leg=c(2,21), y.leg=c(-4,-2)) 1278. contour(cru.loci1, border=cru.b, loc=loci0, add=T, lwd=2) 1279. 1280. # mapeando os erros padro de predio 1281. image(cru.loci1, border=cru.b, loc=loci0, col=gray(seq(1,0,l=21)), val=sqrt(cru.loci1$krige.var), x.leg=c(2,21), y.leg=c(-4,-2)) 1282. ## incluindo as localizaes dos dados 1283. image(cru.loci1, border=cru.b, loc=loci0, col=gray(seq(1,0,l=21)), val=sqrt(cru.loci1$krige.var), x.leg=c(2,21), y.leg=c(-4,-2), coords.dat=cru$coords) 1284. 1285. 1286. ## re-fazendo a krigagem agora com o modelo de Matrn 1287. cru.loci1.mat <- krige.conv(cru, loc=loci1, krige=krige.control(obj=cru.wls.mat)) 1288. image(cru.loci1.mat, border=cru.b, loc=loci0, col=gray(seq(1,0,l=21)), coords.dat=cru$coords) 1289.

1290. ## comparando as predies dos dois modelos 1291. par(mfrow=c(1,2), mar=c(2.5,2.5,0,0), mgp=c(1.2, .5, 0)) 1292. ## limites comuns para comparar resultados 1293. limis <- range(c(cru.loci1.mat$pred, cru.loci1$pred)) 1294. image(cru.loci1, border=cru.b, loc=loci0, col=gray(seq(1,0,l=21)), coords.dat=cru$coords, x.leg=c(2,21), y.leg=c(-4,-2), zlim=limis) 1295. title("modelo exponencial", line=0.5) 1296. image(cru.loci1.mat, border=cru.b, loc=loci0, col=gray(seq(1,0,l=21)), coords.dat=cru$coords, x.leg=c(2,21), y.leg=c(-4,-2), zlim=limis) 1297. title("modelo Matern", line=0.5) 1298. 1299. par(mfrow=c(1,1)) 1300. plot(cru.loci1$pred, cru.loci1.mat$pred) 1301. abline(0,1, lwd=2) 1302. 1303. ## 1304. ## Outras opes no output 1305. ## 1306. ## Gerando simulaes em 2 pontos a serem preditos 1307. cru.kc <- krige.conv(cru, loc=loci, krige=krige.control(obj=cru.wls), out=output.control(n.pred=1000)) 1308. 1309. names(cru.kc) 1310. 1311. ## visualizando os valores simulados 1312. cru.kc$simul 1313. dim(cru.kc$simul) 1314. # distribuio preditiva no primeiro ponto 1315. hist(cru.kc$simul[1,],prob=T) 1316. 1317. ## distribuio baseada nas simulaes em um ponto de predio 1318. ## (estimao de densidade) 1319. plot(density(cru.kc$simul[1,], bw=0.75), main="Ponto 1") 1320. hist(cru.kc$simul[1,],prob=T,add=T) 1321. 1322. ## probabilidade de exceder certo valor em cada um dos pontos 1323. sum(cru.kc$simul[1,] > 3)/1000 1324. sum(cru.kc$simul[2,] > 3)/1000 1325. 1326. ## opo para calcular prob. de superar valor de referncia (threshold) 1327. cru.kc <- krige.conv(cru, loc=loci, krige=krige.control(obj=cru.wls), out=output.control(n.pred=1000, thre=2)) 1328. names(cru.kc) 1329. cru.kc$prob 1330. 1331. ## calculando probabilidades e quantis no grid de predio 1332. cru.loci1 <- krige.conv(cru, loc=loci1, krige=krige.control(obj=cru.wls), out=output.control(n.pred=1000, thre=1.5, quant=c(0.10, .5, .9))) 1333. names(cru.loci1) 1334. dim(cru.loci1$simul)

1335. ## representando probabilidades em um mapa 1336. par(mfrow=c(1,1)) 1337. image(cru.loci1, border=cru.b, loc=loci0, col=gray(seq(1,0,l=21)), coords.dat=cru$coords, val=1cru.loci1$prob, x.leg=c(2,21), y.leg=c(-4,-2)) 1338. 1339. ## mapeando quantis 1340. dim(cru.loci1$quant) 1341. ## mapeando o quantil 0.1 1342. image(cru.loci1, border=cru.b, loc=loci0, col=gray(seq(1,0,l=21)), coords.dat=cru$coords, val=cru.loci1$quan[,1], x.leg=c(2,21), y.leg=c(-4,-2)) 1343. ## mapeando a mediana 1344. image(cru.loci1, border=cru.b, loc=loci0, col=gray(seq(1,0,l=21)), coords.dat=cru$coords, val=cru.loci1$quan[,2], x.leg=c(2,21), y.leg=c(-4,-2)) 1345. ## mapeando o quantil 0.9 1346. image(cru.loci1, border=cru.b, loc=loci0, col=gray(seq(1,0,l=21)), coords.dat=cru$coords, val=cru.loci1$quan[,3], x.leg=c(2,21), y.leg=c(-4,-2)) 1347. 1348. ## 1349. ## Outra forma de visualizar os resultados: 1350. ## usando funes do pacote lattice 1351. require(lattice) 1352. ## uma imagem "ruim" (sem preservar escala) 1353. levelplot(cru.loci1$pred ~ loci1$x * loci1$y, col.regions=gray(seq(1,0,l=20))) 1354. ## fazendo na escala correta 1355. levelplot(cru.loci1$pred ~ loci1$x * loci1$y, col.regions=gray(seq(1,0,l=20)),aspect="iso") 1356. 1357. ## colocando fundo branco 1358. trellis.par.set(theme = col.whitebg()) 1359. levelplot(cru.loci1$pred ~ loci1$x * loci1$y, col.regions=gray(seq(1,0,l=20)),aspect="iso") 1360. 1361. ## mudando posio da legenda 1362. levelplot(cru.loci1$pred ~ loci1$x * loci1$y, col.regions=gray(seq(1,0,l=20)),aspect="iso", colorkey=list(space="top")) 1363. 1364. ## vendo outras opes a funo 1365. args(levelplot) 1366. help(levelplot) 1367. 1368. ## Nota: exportando resultados como arquivo texto 1369. ## (para usar resultados em outro software) 1370. ## exportando as coordenadas dos pontos de predio 1371. write(t(loci1), file="pp.txt", ncol=2) 1372. ## exportando os valores preditos 1373. write(cru.loci1$pred, file="pppred.txt", ncol=1) 1374. ## exportando as varincias de krigagem 1375. write(cru.loci1$krige.var, file="ppvar.txt", ncol=1) 1376. 1377. ## 1378. ## Alternativas para anlise:

1379. ## Estimando parmetros por mxima verossimilhana 1380. ## 1381. cru.ml <- likfit(cru, ini=c(2,1.5), lambda=0) 1382. cru.ml 1383. summary(cru.ml) 1384. plot(cru.vario) 1385. lines(cru.ml, lty=2, col="red", lwd=2) 1386. 1387. ## estimando com outros valores iniciais para checar 1388. ## estabilidade da optimizao numrica 1389. cru.ml <- likfit(cru, ini=c(3,0.5), lambda=0) 1390. cru.ml 1391. cru.ml <- likfit(cru, ini=c(1,2.5), lambda=0) 1392. cru.ml 1393. cru.ml <- likfit(cru, ini=c(1,0.5), lambda=0) 1394. cru.ml 1395. 1396. ## fornecendo mltiplos valores iniciais 1397. ini.m <- expand.grid(c(1, 1.5, 2, 2.5), c(0,1,2)) 1398. ini.m 1399. cru.ml <- likfit(cru, ini=c(2,1.5), lambda=0, nug=c(0, 0.5, 1)) 1400. cru.ml 1401. 1402. ## estimando o parmetro de transformao 1403. cru.ml <- likfit(cru, ini=c(2,1.5), lambda=0, fix.lam=F) 1404. cru.ml 1405. 1406. ## estimando com modelo de Matrn com kappa=1 1407. cru.ml.k1 <- likfit(cru, ini=c(2,1.5), lambda=0, cov.model="matern", kappa=1) 1408. cru.ml.k1 1409. 1410. ## estimando com modelo de Matrn com kappa=2 1411. cru.ml.k2 <- likfit(cru, ini=c(2,1.5), lambda=0, cov.model="matern", kappa=2) 1412. cru.ml.k2 1413. 1414. ## estimando com modelo esfrico 1415. cru.ml.sp <- likfit(cru, ini=c(2, 1.5), lambda=0, cov.model="sph") 1416. cru.ml.sp 1417. lines(cru.ml.sp) 1418. 1419. ## comparando estimativas dos parmetros 1420. cru.ml 1421. cru.ml.k1 1422. cru.ml.k2 1423. cru.ml.sp 1424. 1425. plot(cru.vario) 1426. lines(cru.ml) 1427. lines(cru.ml.k1, lty=2) 1428. lines(cru.ml.k2, lty=2, lwd=2) 1429. lines(cru.ml.sp, lwd=2) 1430.

1431. ## estimando parmetros de um modelo com covariveis (tendncia) 1432. parana.ml <- likfit(parana, ini=c(1200, 100), trend="1st") 1433. parana.ml 1434. summary(parana.ml) 1435. 1436. ## agora ajustando sem tendncia 1437. parana.ml0 <- likfit(parana, ini=c(1200, 100)) 1438. summary(parana.ml0) 1439. 1440. ## comparando os ajustes 1441. summary(parana.ml) 1442. summary(parana.ml0) 1443. 1444. ## agora ajustando com tendncia de 2o grau 1445. parana.ml2 <- likfit(parana, ini=c(1200, 100), trend="2nd") 1446. parana.ml2 1447. summary(parana.ml2) 1448. 1449. ## comparando os ajustes 1450. summary(parana.ml) 1451. summary(parana.ml2) 1452. 1453. ## validao cruzada 1454. args(xvalid) 1455. cru.xv <- xvalid(cru, model=cru.ml) 1456. par(mfcol=c(5,2), mar=c(2.8, 2.8, .5, .5), mgp=c(1.7,.8,1)) 1457. plot(cru.xv) 1458. par(par.ori) 1459. 1460. ## Inferncia Bayesiana 1461. ## definindo modelo e seus termos fixos 1462. cru.mod <- model.control(kappa=1, lambda=0) 1463. cru.mod 1464. ## definindo a distribuio priori 1465. cru.prior <- prior.control(phi.dis=seq(0,15, l=16), tausq.rel.prior="unif", tausq.rel.disc=seq(0,1,l=11)) 1466. cru.prior 1467. 1468. ## rodando as anlises 1469. cru.bayes <- krige.bayes(cru, loc=loci, model=cru.mod, prior=cru.prior) 1470. cru.bayes 1471. 1472. ## examinando os resultados 1473. names(cru.bayes) 1474. names(cru.bayes$post) 1475. names(cru.bayes$post$beta) 1476. cru.bayes$post$beta 1477. cru.bayes$post$sigmasq 1478. 1479. cru.bayes$post$joint 1480. dim(cru.bayes$post$joint) 1481. 1482. cru.bayes$post$samp

1483. 1484. 1485. 1486. 1487. 1488. 1489. 1490. 1491. 1492. 1493. 1494. 1495. 1496. 1497.

par(mfrow=c(1,2)) plot(cru.bayes) par(par.ori) apply(cru.bayes$post$sampl, 2, mean) names(cru.bayes$pred) cru.bayes$pred$mean cru.bayes$pred$variance cru.bayes$pred$dist cru.bayes$pred$med cru.bayes$pred$variabil cru.bayes$pred$simul dim(cru.bayes$pred$sim)

1. An introduction to geoR: web page with the main functionality of the package geoR.

geoR : Package for Geostatistical Data Analysis


An illustrative session Paulo J. Ribeiro Jr. & Peter J. Diggle Last update: 26/Dez/2003

Cross-validation

Getting started

Exploratory Tools

Parameter Estimation

Spatial Interpolation

Bayesian analysis

Simulation

citing geoR

The package geoR provides functions for geostatistical data analysis using the software R. This document illustrates some (but not all !) of the capabilities of the package. The objective is to familiarise the reader with the geoR's commands for data analysis and show some of the graphical outputs which can be produced. The commands used here are just illustrative, providing basic examples of the

package handling. We did not attempt to perform a definitive analysis of this data. In what follows the R commands are shown in
typewriter fonts like this.

Typically, default arguments are used for the function calls and the user is encouraged to inspect other arguments of the functions. For example, to see all the arguments for the function variog type:
args(variog)

The commands shown in this page are also available in the file geoRintro.R. We refer to the geoR documentation for more details on the functions included in the package geoR.

1. STARTING A SESSION AND LOADING DATA

After starting an R session, load geoR with the command:


library(geoR)

If the installation directory for the package is the default location for R packages, type:
library(geoR, lib.loc="PATH_TO_geoR")

where "PATH_TO_geoR" is the path to the directory where geoR was installed. If the package is loaded correctly the following message will be displayed:
-----------------------------------------------geoR: functions for geostatistical data analysis geoR version 1.3-17 is now loaded ------------------------------------------------

Typically, data are stored as an object (a list) of the class "geodata". An object of this class contains at least the coordinates of data locations and the data values.

Click for information on how to read data from an ASCII (text) file. We refer to the documentation for the functions as.geodata and read.geodata for more information on how to import/convert data and on the definitions for the class "geodata". For the examples included in this document we use the data set s100 included in the geoR distribution. To load this data type:
data(s100)

2. EXPLORATORY TOOLS
1. A quick summary for the geodata object A quick summary of the data can be obtained typing
summary(s100)

which will return a summary of the coordinates and data values like this
$coords.summary [,1] [,2] min 0.005638006 0.01091027 max 0.983920544 0.99124979 $data.summary Min. 1st Qu. -1.1680 0.2730 Median 1.1050 Mean 3rd Qu. 0.9307 1.6100 Max. 2.8680

Elements covariate, borders and or units.m will be also summarized if present in the geodata object. 2. Plotting data locations and values The function plot.geodata shows a 2 x 2 display with data locations (top plots) and data versus coordinates (bottom plots). For an object of the class "geodata" the plot is produced by the command:
plot(s100)

Notice that the top-right plot is produced using the package scatterplot3d. If this package is not installed a histogram of the data will replace this plot. The function points.geodata produces a plot showing the data locations. Alternatively, points indicating the data locations can be added to a current plot. There are options to specify point sizes, patterns and colors, which can be set to be proportional to the data values or specified quantiles. Some examples of graphical outputs are illustrated by the commands and corresponding plots as shown below. We start saving the current graphical parameters.
par.ori <- par(no.readonly = TRUE) par(mfrow = c(2,2)) points(s100, xlab = "Coord X", ylab = "Coord Y")

points(s100, "rank.prop") points(s100, 1.7, col = points(s100, "Coord Y") par(par.ori)

xlab = "Coord X", ylab = "Coord Y", pt.divide = xlab = "Coord X", ylab = "Coord Y", cex.max = gray(seq(1, 0.1, l=100)), pt.divide = "equal") pt.divide = "quintile", xlab = "Coord X", ylab =

3. Empirical variograms Empirical variograms are calculated using the function variog. There are options for the classical or modulusestimator. Results can be returned as variogram clouds, binned or smoothed variograms.
cloud1 <- variog(s100, option = "cloud", max.dist=1) cloud2 <- variog(s100, option = "cloud", estimator.type = "modulus", max.dist=1) bin1 <- variog(s100, uvec=seq(0,1,l=11))

bin2 <- variog(s100, uvec=seq(0,1,l=11), estimator.type= "modulus") par(mfrow=c(2,2)) plot(cloud1, main plot(cloud2, main plot(bin1, main = plot(bin2, main = par(par.ori) = "classical estimator") = "modulus estimator") "classical estimator") "modulus estimator")

Furthermore, the points of the variogram clouds can be grouped into classes of distances ("bins") and displayed with a box-plot for each bin.
bin1 <- variog(s100,uvec = seq(0,1,l=11), bin.cloud = T) bin2 <- variog(s100,uvec = seq(0,1,l=11), estimator.type = "modulus", bin.cloud = T)

par(mfrow = c(1,2)) plot(bin1, bin.cloud = T, main = "classical estimator") plot(bin2, bin.cloud = T, main = "modulus estimator") par(par.ori)

Theoretical and empirical variograms can be plotted and visually compared. For example, the figure below shows the theoretical variogram model used to simulate the data s100 and two estimated variograms.
bin1 <- variog(s100, uvec = seq(0,1,l=11)) plot(bin1) lines.variomodel(cov.model = "exp", cov.pars = c(1,0.3), nugget = 0, max.dist = 1, lwd = 3) smooth <- variog(s100, option = "smooth", max.dist = 1, n.points = 100, kernel = "normal", band = 0.2) lines(smooth, type ="l", lty = 2) legend(0.4, 0.3, c("empirical", "exponential model", "smoothed"), lty = c(1,1,2), lwd = c(1,3,1))

Directional variograms can also be computed by the function variog using the arguments direction andtolerance. For example, to compute a variogram for the direction 60 degrees with the default tolerance angle (22.5 degrees) the command would be:
vario60 <- variog(s100, max.dist = 1, direction=pi/3) plot(vario60) title(main = expression(paste("directional, angle = ", 60 * degree)))

and the plot is shown on the left panel of the figure below. For a quick computation in four directions we can use the function variog4 and the corresponding plot is shown on the right panel of the next figure.
vario.4 <- variog4(s100, max.dist = 1) plot(vario.4, lwd=2)

3. PARAMETER ESTIMATION
The model parameters can be estimated:
o o

by "eye": trying different models over empirical variograms (using the function lines.variomodel), by least squares fit of empirical variograms: with options for ordinary (OLS) and weighted (WLS) least squares (using the function variofit), by likelihood based methods: with options for maximum likelihood (ML) and restricted maximum likelihood (REML) (using the function likfit), Bayesian methods are also implemented and will be presented in Section 5 (using the function krige.bayes).

The following commands show how to add a line with a variogram model to a variogram plot.
plot(variog(s100, max.dist=1)) lines.variomodel(cov.model="exp", cov.pars=c(1,.3), nug=0, max.dist=1) lines.variomodel(cov.model="mat", cov.pars=c(.85,.2), nug=0.1, kappa=1,max.dist=1, lty=2) lines.variomodel(cov.model="sph", cov.pars=c(.8,.8), nug=0.1,max.dist=1, lwd=2)

In the parameter estimation functions variofit and likfitthe nugget effect parameter can either be estimated or set to a fixed value. The same applies for smoothness, anisotropy and transformation parameters. Options for taking trends into account are also included. Trends can be specified as polynomial functions of the coordinates and/or linear functions of given covariates. The commands below shows models fitted by different methods with options for fixed or estimated nugget parameter. Features not illustrated here include estimation of trends, anisotropy, smoothness and Box-Cox transformation parameter.
# Fitting models with nugget fixed to zero ml <- likfit(s100, ini = c(1,0.5), fix.nugget = T) reml <- likfit(s100, ini = c(1,0.5), fix.nugget = T, method = "RML") ols <- variofit(bin1, ini = c(1,0.5), fix.nugget = T, weights="equal") wls <- variofit(bin1, ini = c(1,0.5), fix.nugget = T) # Fitting models with a fixed value for the nugget ml.fn <- likfit(s100, ini = c(1,0.5), fix.nugget = T, nugget = 0.15) reml.fn <- likfit(s100, ini = c(1,0.5), fix.nugget = T, nugget = 0.15, method = "RML") ols.fn <- variofit(bin1,ini = c(1,0.5), fix.nugget = T, nugget = 0.15, weights="equal") wls.fn <- variofit(bin1, ini = c(1,0.5), fix.nugget = T, nugget = 0.15) # Fitting models estimated nugget ml.n <- likfit(s100, ini = c(1,0.5), nug = 0.5) reml.n <- likfit(s100, ini = c(1,0.5), nug = 0.5, method = "RML") ols.n <- variofit(bin1, ini = c(1,0.5), nugget=0.5, weights="equal")

wls.n <- variofit(bin1, ini = c(1,0.5), nugget=0.5) # Now, plotting fitted models against empirical variogram par(mfrow = c(1,3)) plot(bin1, main = expression(paste("fixed ", tau^2 == 0))) lines(ml, max.dist = 1) lines(reml, lwd = 2, max.dist = 1) lines(ols, lty = 2, max.dist = 1) lines(wls, lty = 2, lwd = 2, max.dist = 1) legend(0.5, 0.3, legend=c("ML","REML","OLS","WLS"),lty=c(1,1,2,2),lwd=c(1,2,1,2), cex=0.7) plot(bin1, main = expression(paste("fixed ", tau^2 == 0.15))) lines(ml.fn, max.dist = 1) lines(reml.fn, lwd = 2, max.dist = 1) lines(ols.fn, lty = 2, max.dist = 1) lines(wls.fn, lty = 2, lwd = 2, max.dist = 1) legend(0.5, 0.3, legend=c("ML","REML","OLS","WLS"), lty=c(1,1,2,2), lwd=c(1,2,1,2), cex=0.7) plot(bin1, main = expression(paste("estimated ", tau^2))) lines(ml.n, max.dist = 1) lines(reml.n, lwd = 2, max.dist = 1) lines(ols.n, lty = 2, max.dist = 1) lines(wls.n, lty =2, lwd = 2, max.dist = 1) legend(0.5, 0.3, legend=c("ML","REML","OLS","WLS"), lty=c(1,1,2,2), lwd=c(1,2,1,2), cex=0.7) par(par.ori)

Summary methods have been written to summarize the resulting objects. For example, for the model with estimated nugget fitted by maximum likelihood, typing:
ml.n

will produce the output:


likfit: estimated model parameters: beta tausq sigmasq phi 0.7766 0.0000 0.7517 0.1827 likfit: maximised log-likelihood = -83.5696

whilst a more detailed summary is obtained with:


> summary(ml.n) Summary of the parameter estimation ----------------------------------Estimation method: maximum likelihood Parameters of the mean component (trend): beta 0.7766 Parameters of the spatial component: correlation function: exponential (estimated) variance parameter sigmasq (partial sill) = 0.7517 (estimated) cor. fct. parameter phi (range parameter) = 0.1827 anisotropy parameters: (fixed) anisotropy angle = 0 ( 0 degrees ) (fixed) anisotropy ratio = 1 Parameter of the error component: (estimated) nugget = 0 Transformation parameter: (fixed) Box-Cox parameter = 1 (no transformation) Maximised Likelihood: log.L n.params AIC BIC -83.5696 4.0000 175.1391 185.5598 Call: likfit(geodata = s100, ini.cov.pars = c(1, 0.5), nugget = 0.5)

Two kinds of variogram envelopes computed by simulation are illustrated in the figure below. The plot on the left-hand side shows an envelope based on permutations of

the data values across the locations, i.e. envelopes built under the assumption of no spatial correlation. The envelopes shown on the right-hand side are based on simulations from a given set of model parameters, in this example the parameter estimates from the WLS variogram fit. This envelope shows the variability of the empirical variogram.
env.mc <- variog.mc.env(s100, obj.var=bin1) env.model <- variog.model.env(s100, obj.var=bin1, model=wls) par(mfrow=c(1,2)) plot(bin1, envelope=env.mc) plot(bin1, envelope=env.model) par(par.ori)

Profile likelihoods (1-D and 2-D) are computed by the function proflik. Here we show the profile likelihoods for the covariance parameters of the model without nugget effect previously fitted by likfit. WARNING: RUNNING THE NEXT COMMAND CAN BE TIMECONSUMING
prof <- proflik(ml, geodata = s100, sill.val = seq(0.48, 2, l=11), range.val = seq(0.1, 0.52, l=11), uni.only = FALSE) par(mfrow=c(1,3)) plot(prof, nlevels=16) par(par.ori)

4. CROSS-VALIDATION
The function xvalid performs cross-validation either using the leavingone-out strategy or using a different set of locations provided by the user. For the first strategy, data points are removed one by one and predicted by kriging using the remaining data. The commands below illustrates cross-validation for the models fitted by maximum likelihood and weighted least squares. In the first two calls the model parameters remains the same for the prediction at each location. In the next two calls the model parameters are re-estimated each time a point is removed from the data-set. Graphical results are shown for the cross-validation results where the leaving-one-out strategy combined with the wls estimates for the parameters was used.
xv.ml <- xvalid(s100, model=ml) xv.wls <- xvalid(s100, model=wls)

WARNING: RUNNING THE NEXT COMMAND CAN BE TIME-CONSUMING


xvR.ml <- xvalid(s100, model=ml, reest=TRUE) xvR.wls <- xvalid(s100, model=wls, reest=TRUE, variog.obj=bin1) par(mfcol = c(5,2), mar=c(3,3,.5,.5), mgp=c(1.5,.7,0)) plot(xv.wls)

par(par.ori)

5. SPATIAL INTERPOLATION
Conventional geostatistical spatial interpolation (kriging) can be performed with options for:
o o o o

Simple kriging Ordinary kriging Trend (universal) kriging External trend kriging

There are additional options for Box-Cox transformation (and back transformation of the results) and anisotropic models. Simulations can be drawn from the resulting predictive distributions if requested. As a first example consider the prediction at four locations labeled 1, 2, 3, 4 and indicated in the figure below.
plot(s100$coords, xlim=c(0,1.2), ylim=c(0,1.2), xlab="Coord X", ylab="Coord Y") loci <- matrix(c(0.2, 0.6, 0.2, 1.1, 0.2, 0.3, 1.0, 1.1), ncol=2) text(loci, as.character(1:4), col="red") polygon(x=c(0,1,1,0), y=c(0,0,1,1), lty=2)

The command to perform ordinary kriging using the parameters estimated by weighted least squares with nugget fixed to zero would be:
kc4 <- krige.conv(s100, locations = loci, krige = krige.control(obj.m = wls))

The output is a list including the predicted values (kc4$predict) and the kriging variances (kc4$krige.var). Consider now a second example. The goal is to perform prediction on a grid covering the area and to display the results. Again, we use ordinary kriging. The commands are:
# defining the grid pred.grid <- expand.grid(seq(0,1, l=51), seq(0,1, l=51)) # kriging calculations kc <- krige.conv(s100, loc = pred.grid, krige = krige.control(obj.m = ml)) # displaying predicted values image(kc, loc = pred.grid, col=gray(seq(1,0.1,l=30)), xlab="Coord X", ylab="Coord Y")

6. BAYESIAN ANALYSIS
Bayesian analysis for Gaussian models is implemented by the function krige.bayes. It can be performed for different "degrees of uncertainty", meaning that model parameters can be treated as fixed or random. As an example consider a model without nugget and including uncertainty in the mean, sill and range parameters. Prediction at the four locations indicated above is performed by typing a command like:
WARNING: RUNNING THE NEXT COMMAND CAN BE TIME-CONSUMING bsp4 <- krige.bayes(s100, loc = loci, prior = prior.control(phi.discrete = seq(0,5,l=101), phi.prior="rec"), output=output.control(n.post=5000))

Histograms showing posterior distribution for the model parameters can be plotted by typing:
par(mfrow=c(1,3), mar=c(3,3,.5,.5), mgp=c(2,1,0)) hist(bsp4$posterior$sample$beta, main="", xlab=expression(beta), prob=T) hist(bsp4$posterior$sample$sigmasq, main="", xlab=expression(sigma^2), prob=T) hist(bsp4$posterior$sample$phi, main="", xlab=expression(phi), prob=T) par(par.ori)

Using summaries of these posterior distributions (means, medians or modes) we can check the "estimated Bayesian variograms" against the empirical variogram, as shown in the next figure. Notice that it is also possible to compare these estimates with other fitted variograms such as the ones computed in Section 3.
plot(bin1, ylim = c(0,1.5)) lines(bsp4, max.dist = 1.2, summ = mean) lines(bsp4, max.dist = 1.2, summ = median, lty = 2) lines(bsp4, max.dist = 1.2, summ = "mode", post="par",lwd = 2, lty = 2) legend(0.25, 0.4, legend = c("variogram posterior mean", "variogram posterior median", "parameters posterior mode"), lty = c(1,2,2), lwd = c(1,1,2), cex = 0.8)

The next figure shows predictive distributions at the four selected locations. Dashed lines show Gaussian distributions with mean and variance given by results of ordinary kriging obtained in Section 4. The full lines correspond to the Bayesian prediction. The plot shows results of density estimation using samples from the predictive distributions.
par(mfrow=c(2,2), mar=c(3,3,.5,.5), mgp=c(1.5,.7,0))

for(i in 1:4){ kpx <- seq(kc4$pred[i] - 3*sqrt(kc4$krige.var[i]), kc4$pred[i] +3*sqrt(kc4$krige.var[i]), l=100) kpy <- dnorm(kpx, mean=kc4$pred[i], sd=sqrt(kc4$krige.var[i])) bp <- density(bsp4$predic$sim[i,]) rx <- range(c(kpx, bp$x)) ry <- range(c(kpy, bp$y)) plot(cbind(rx, ry), type="n", xlab=paste("Location", i), ylab="density", xlim=c(-4, 4), ylim=c(0,1.1)) lines(kpx, kpy, lty=2) lines(bp) } par(par.ori)

Consider now, under the same model assumptions, obtaining simulations from the predictive distributions on a grid of points covering the area. The commands to define the grid and perform Bayesian prediction are:

pred.grid <-

expand.grid(seq(0,1, l=31), seq(0,1, l=31))

WARNING: RUNNING THE NEXT COMMAND CAN BE TIME-CONSUMING


bsp <- krige.bayes(s100, loc = pred.grid, prior = prior.control(phi.discrete = seq(0,5,l=51)), output=output.control(n.predictive=2))

Maps with the summaries and simulations of the predictive distribution can be plotted as follows.
par(mfrow=c(2,2)) image(bsp, loc = pred.grid, main = "predicted", col=gray(seq(1,0.1,l=30))) image(bsp, val ="variance", loc = pred.grid, main = "prediction variance", col=gray(seq(1,0.1,l=30))) image(bsp, val = "simulation", number.col = 1, loc = pred.grid, main = "a simulation from\nthe predictive distribution", col=gray(seq(1,0.1,l=30))) image(bsp, val = "simulation", number.col = 2,loc = pred.grid, main = "another simulation from \n the predictive distribution", col=gray(seq(1,0.1,l=30))) par(par.ori)

Note: Further examples for the function krige.bayes are given in the file examples.krige.bayes.R

7. Simulation of Gaussian Random Fields


The function grf generates simulations of Gaussian random fields on regular or irregular sets of locations Some of its functionality is illustrated by the next commands.
sim1 <- grf(100, cov.pars=c(1, .25)) points.geodata(sim1, main="simulated locations and values") plot(sim1, max.dist=1, main="true and empirical variograms")

sim2 <- grf(441, grid="reg", cov.pars=c(1, .25)) image(sim2, main="a small-ish simulation", col=gray(seq(1, .1, l=30)))

sim3 <- grf(40401, grid="reg", cov.pars=c(10, .2), met="circ") image(sim3, main="simulation on a fine grid", col=gray(seq(1, .1, l=30)))

NOTE: we recommend the package RandomFields for a more comprehensive implementation for simulation of Gaussian Random Fields.

8. Citing geoR
The function cite.geoR() shows information on how to cite geoR in publications.
> cite.geoR() To cite geoR in publications, use RIBEIRO Jr., P.J. & DIGGLE, P.J. (2001) geoR: A package for geostatistical analysis. R-NEWS, Vol 1, No 2, 15-18. ISSN 16093631. Please cite geoR when using it for data analysis! A BibTeX entry for LaTeX users is @Article{, title analysis}, author journal = {{geoR}: a package for geostatistical = {Ribeiro Jr., P.J. and Diggle, P.J.}, = {R-NEWS},

year volume number pages issn url }

= = = = = =

{2001}, {1}, {2}, {15--18}, {1609-3631}, {http://cran.R-project.org/doc/Rnews}

Site maintained by: Paulo J. Ribeiro Jr. (Paulo.Ribeiro@est.ufpr.br)

2. Commands file with contents shown during a pratical session during a short course on geostatistics.
1498. ## Loading geoR 1499. require(geoR) 1500. 1501. ## loading the s100 data-set 1502. data(s100) 1503. 1504. ## visualising data points 1505. points(s100) 1506. 1507. v <- variog(s100) 1508. plot(v) 1509. 1510. summary(s100) 1511. #args(plot.variogram) 1512. args(getS3method("plot", "variogram")) 1513. plot(v, pts=c(0.7, 2.1)) 1514. names(v) 1515. v$n 1516. 1517. v <- variog(s100, max.d=1) 1518. plot(v) 1519. v$n 1520. 1521. v$bins.lim 1522. 1523. v <- variog(s100, uvec=seq(0, 1, by=0.1)) 1524. plot(v) 1525. 1526. args(variog) 1527. 1528. v <- variog(s100, op="cloud") 1529. plot(v) 1530. 1531. #cru <read.geodata("http://www.leg.ufpr.br/geoR/tutorials/datasets/Crucia ni.dat", head=T, coords.col=2:3, data.col=4) 1532. cru <- read.geodata("Cruciani.dat", head=T, coords.col=2:3, data.col=4) 1533. plot(cru)

1534. 1535. cru.v <- variog(cru, max.d=9, lam=0) 1536. plot(cru.v) 1537. 1538. data(parana) 1539. pr.v <- variog(parana, max.d=450, trend="1st") 1540. plot(pr.v) 1541. 1542. data(ca20) 1543. summary(ca20) 1544. ca20.v <- variog(ca20, max.d=700, trend=~area) 1545. plot(ca20.v) 1546. 1547. #ca20.ef <- eyefit(ca20.v) 1548. #summary(ca20.ef) 1549. 1550. ca20.vf <- variofit(ca20.v, ini=c(60, 100), nug=40) 1551. ca20.vf 1552. plot(ca20.v) 1553. lines(ca20.vf) 1554. 1555. plot(ca20.v, pts=c(0,7, 2.1)) 1556. 1557. args(variog) 1558. 1559. plot(ca20.v, ylim=c(0, 180)) 1560. 1561. ca20.v0 <- variog(ca20, max.d=700, dir=0) 1562. lines(ca20.v0, col=2) 1563. 1564. ca20.v45 <- variog(ca20, max.d=700, dir=pi/4) 1565. lines(ca20.v45, col=3) 1566. 1567. ca20.v90 <- variog(ca20, max.d=700, dir=pi/2) 1568. lines(ca20.v90, col=4) 1569. 1570. ca20.v135 <- variog(ca20, max.d=700, dir=3*pi/4) 1571. lines(ca20.v135, col=5) 1572. 1573. ca20.v.env <- variog.model.env(ca20, obj=ca20.v, model=ca20.vf) 1574. lines(ca20.vf, lwd=2) 1575. lines(ca20.v.env) 1576. 1577. ca20.v.mc <- variog.mc.env(ca20, obj=ca20.v) 1578. plot(ca20.v) 1579. lines(ca20.v.mc) 1580. 1581. ca20.v4 <- variog4(ca20, max.d=700) 1582. plot(ca20.v4) 1583. plot(ca20.v4, omni=T) 1584. 1585. 1586. ml <- likfit(s100, ini=c(1, .2)) 1587. ml 1588. 1589. args(krige.conv)

1590. loc1 <- c(0.5, 0.5) 1591. points(t(loc1), pch="?", cex=1.5, col=2) 1592. 1593. kr1 <- krige.conv(s100, locations=loc1, krige=krige.control(obj.m=ml)) 1594. 1595. loc2 <- expand.grid(seq(0,1,l=31), seq(0,1, l=31)) 1596. points(loc2, pch="+", col=3) 1597. 1598. kr2 <- krige.conv(s100, locations=loc2, krige=krige.control(obj.m=ml)) 1599. names(kr2) 1600. kr2$pred 1601. 1602. image(kr2) 1603. points(s100, add=T) 1604. image(kr2, col=gray(seq(1,0, l=31))) 1605. image(kr2, col=gray(seq(1,0, l=4))) 1606. image(kr2, col=terrain.colors(30)) 1607. 1608. image(kr2, val=sqrt(kr2$krige.var)) 1609. image(kr2, val=sqrt(kr2$krige.var), coords.d=s100$coords) 1610. 1611. args(krige.conv) 1612. args(output.control) 1613. 1614. OC <- output.control(thres=2) 1615. kr2 <- krige.conv(s100, locations=loc2, krige=krige.control(obj.m=ml), out=OC) 1616. names(kr2) 1617. image(kr2, val=1-kr2$prob, col=gray(seq(1,0, l=21))) 1618. 1619. OC <- output.control(thres=2, quant=c(0.1, 0.5, 0.9)) 1620. kr2 <- krige.conv(s100, locations=loc2, krige=krige.control(obj.m=ml), out=OC) 1621. names(kr2) 1622. dim(kr2$quant) 1623. image(kr2, val=1-kr2$quan[,1], col=gray(seq(1,0, l=21))) 1624. 1625. image(kr2, val=1-kr2$quan[,3], col=gray(seq(1,0, l=21)), x.l=c(0, .4), y.leg=c(-.1, 0)) 1626. image(kr2, val=1-kr2$quan[,3], col=gray(seq(1,0, l=5)), x.l=c(0, .4), y.leg=c(-.1, 0)) 1627. 1628. ## 1629. 1630. args(xvalid) 1631. 1632. ml1 <- likfit(s100, ini=c(1, .2), cov.model="sph") 1633. ml.xv <- xvalid(s100, model=ml, reest=T) 1634. ml.xv1 <- xvalid(s100, model=ml1) 1635. 1636. par(mfcol=c(5,2), mar=c(2.5, 2.5, 0,0), mgp=c(1.7,.7,0)) 1637. plot(ml.xv) 1638. 1639. #X11() 1640. par(mfcol=c(5,2), mar=c(2.5, 2.5, 0,0), mgp=c(1.7,.7,0))

1641. plot(ml.xv1) 1642. 1643. args(krige.bayes) 1644. 1645. args(model.control) 1646. MC <- model.control() 1647. args(prior.control) 1648. PC <- prior.control(phi.disc=seq(0, 2, l=21), tausq.rel.disc=seq(0,2, l=21), tausq.rel.prior="unif") 1649. 1650. s100.post <- krige.bayes(s100, prior=PC, model=MC) 1651. 1652. names(s100.post) 1653. names(s100.post$post) 1654. 1655. s100.post$post$beta 1656. s100.post$post$sigma 1657. s100.post$post$phi 1658. s100.post$post$tausq.rel 1659. 1660. par(mfrow=c(2,1), mar=c(3.5,3.5, .5, .5)) 1661. plot(s100.post) 1662. 1663. x0 <- c(0.5, 0.5) 1664. 1665. s100.pred <- krige.bayes(s100, prior=PC, model=MC, loc=x0) 1666. names(s100.pred) 1667. 1668. names(s100.pred$pred) 1669. s100.pred$pred 1670. 1671. ml <- likfit(s100, ini=c(1, .3)) 1672. s100.kc <- krige.conv(s100, loc=x0, krige=krige.control(obj=ml)) 1673. s100.kc[1:2] 1674. 1675. s100.pred$pred[1:2] 1676. s100.pred$pred[5:6] 1677. 1678. PC <- prior.control(phi.disc=seq(0, 2, l=21), phi.prior="exp", phi=0.2, tausq.rel.disc=seq(0,2, l=21), tausq.rel.prior="unif") 1679. s100.pred <- krige.bayes(s100, prior=PC, model=MC, loc=x0) 1680. 1681. plot(s100.pred) 1682. s100.pred$pred[1:2] 1683. 1684. s100.v <- variog(s100, max.dist=1.1) 1685. plot(s100.v) 1686. 1687. #lines(s100.pred) 1688. #help(lines.variomodel.krige.bayes) 1689. 1690. lines(s100.pred, sum=mean) 1691. lines(s100.pred, sum=median, lty=2) 1692. lines(s100.pred, sum="mode", lty=2, post="parameters") 1693.

1694. 1695.

hist(s100.pred$pred$simul, prob=T) lines(density(s100.pred$pred$simul))

1. A basic example of a geostatistical analysis of a soil conductivity data-set using R/geoR.


1696. ## 1697. ## An example of a geostatistical analysis using the package geoR 1698. ## ============================================================== 1699. ## 1700. ## Data: Hydraulic conductivity measurements 1701. ## 1702. ## files: (they must be copied to your working directory) 1703. ## - Cruciani.dat (coordinates of the data locations and K measurements) 1704. ## - Cruciani.borders (coordinates defining the limits of the area) 1705. ## 1706. require(geoR) 1707. ## 1708. ## 1. Reading data 1709. ## 1710. cru <- read.geodata("Cruciani.dat", head=T, coords.col=2:3, data.col=4) 1711. cru.borda <- read.table("Cruciani.border", head=T)[,2:3] 1712. cru.borda <- rbind(cru.borda, cru.borda[1,]) 1713. ## 1714. ## 2. Exploratory plots 1715. ## 1716. ## Plotting original data 1717. plot(cru, bord=cru.borda) 1718. points(cru, bord=cru.borda, cex.min=1, cex.max=1, col=gray(seq(0.9,0,l=length(cru$data))), xla="Coord X", ylab="Coord Y") 1719. ## 1720. ## now transforming the data (log) 1721. plot(cru, bord=cru.borda, lambda = 0) 1722. points(cru, bord=cru.borda, cex.min=1, cex.max=1, lambda = 0, col=gray(seq(0.9,0,l=length(cru$data))), xlab="Coord X", ylab="Coord Y") 1723. ## 1724. ## histograms for original and transformed data 1725. par.ori <- par(no.readonly = TRUE) 1726. 1727. par(mfrow=c(1,2), mar=c(3.5,3.5,1,1)) 1728. hist(cru$data, main="", xlab="K data") 1729. hist(log(cru$data), main="", xlab="log(K) data") 1730. par(par.ori) 1731. ## 1732. ## 3. Parameter estimation via maximum likelihood 1733. ##

1734. ## defining a grid of initial values for the numerical maximisation of the likelihood 1735. ini.fit <- expand.grid((0:12)/2, (0:12)/2) 1736. ## 1737. ## Estimating parameters, including the transformation parameter lambda, under different correlation models 1738. ## 1739. cru.ml <- list() 1740. ## 1741. cru.ml$mat1 <- likfit(cru, ini=ini.fit, fix.nugget=F, fix.lambda=F, cov.model="mat", kappa=1) 1742. summary(cru.ml$mat1) 1743. cru.ml$gau <- likfit(cru, ini=ini.fit, fix.nugget=F, fix.lambda=F, cov.model="gau") 1744. summary(cru.ml$gau) 1745. cru.ml$sph <- likfit(cru, ini=ini.fit, fix.nugget=F, fix.lambda=F, cov.model="sph") 1746. summary(cru.ml$sph) 1747. cru.ml$exp <- likfit(cru, ini=ini.fit, fix.nugget=F, fix.lambda=F, cov.model="exp") 1748. summary(cru.ml$exp) 1749. ## 1750. ## Now fixing lambda = 0 1751. ## 1752. cru.ml0 <- list() 1753. ## 1754. cru.ml0$mat1 <- likfit(cru, ini=ini.fit, fix.nugget=F, lambda=0, cov.model="mat", kappa=1) 1755. summary(cru.ml0$mat1) 1756. cru.ml0$gau <- likfit(cru, ini=ini.fit, fix.nugget=F, lambda=0, cov.model="gau") 1757. summary(cru.ml0$gau) 1758. cru.ml0$sph <- likfit(cru, ini=ini.fit, fix.nugget=F, lambda=0, cov.model="sph") 1759. summary(cru.ml0$sph) 1760. cru.ml0$exp <- likfit(cru, ini=ini.fit, fix.nugget=F, lambda=0, cov.model="exp") 1761. summary(cru.ml0$exp) 1762. ## 1763. ## 4. Profile likelihoods 1764. ## 1765. #prof.lambda <- proflik(cru.ml$gau,cru,sill.val=NULL, range.val=NULL, nugget.val=NULL, lambda.val=seq(-.4,.4,l=21)) 1766. #plot(prof.lambda) 1767. ## 1768. #cru.prof <- list() 1769. #cru.prof$ml.gau <proflik(cru.ml$gau,cru,sill.val=seq(.5, 4, l=21), range.val=seq(.8, 5, l=21), nugget.val=seq(0,2,l=21), lambda.val=seq(-.4,.4,l=21)) 1770. #par(mfrow=c(2,2)) 1771. #plot(cru.prof$ml.gau) 1772. #par(mfrow=c(1,1)) 1773. ## 1774. #cru.prof$ml0.gau <proflik(cru.ml0$gau,cru,sill.val=seq(.5, 4, l=21), range.val=seq(.8, 5, l=21), nugget.val=seq(0,1.5,l=21)) 1775. #par(mfrow=c(2,2))

1776. #plot(cru.prof$ml0.gau) 1777. #par(mfrow=c(1,1)) 1778. ## 1779. ## 5. Empirical variogram 1780. ## 1781. cru.vario <- variog(cru, uvec=seq(0,9, l=8), lambda=0) 1782. plot(cru.vario) 1783. lines(cru.ml0$mat1) 1784. lines(cru.ml0$gau, col="blue") 1785. lines(cru.ml0$sph, col="red") 1786. lines(cru.ml0$exp, col="green") 1787. ## 1788. ## 6. Kriging 1789. ## 1790. apply(cru.borda,2,range) 1791. cru.grid <- expand.grid(seq(0,22.5,by=0.125), seq(0,13,by=0.125)) 1792. 1793. cru.sk <- krige.conv(cru, loc=cru.grid, krige=krige.control(cov.model="gau", obj.model=cru.ml0$gau, lambda=0, type="sk"), border=cru.borda) 1794. 1795. image(cru.sk, border=cru.borda, loc=expand.grid(seq(0,22.5,by=0.125), seq(0,13,by=0.125)), col=gray(seq(.9,0,l=21)), x.leg=c(3,18), y.leg=c(-3, -2), coords.data=cru$coords, xlab="Coord X", ylab="Coord Y") 1796. 1797. contour(cru.sk, coords.data=cru$coords, xlab="Coord X", ylab="Coord Y") 1798. 1799. contour(cru.sk, coords.data=cru$coords, xlab="Coord X", ylab="Coord Y", filled=TRUE) 1800. 1801. image(cru.sk, col=gray(seq(.9,0,l=21)), x.leg=c(3,18), y.leg=c(-3, -2), coords.data=cru$coords, xlab="Coord X", ylab="Coord Y") 1802. 1803. image(cru.sk, values=cru.sk$krige.var, col=gray(seq(.9,0,l=21)), x.leg=c(3,18), y.leg=c(-3, -2), coords.data=cru$coords, xlab="Coord X", ylab="Coord Y") 1804. ## 1805. ## 7. Difficulties when using variograms for inference on model parameters 1806. ## (just for illustration) 1807. ## 1808. cru.v1 <- variog(cru, uvec=seq(0,9, l=8), lambda=0) 1809. cru.v1.exp <- variofit(cru.v1, ini=ini.fit, cov.model="exp") 1810. cru.v1.gau <- variofit(cru.v1, ini=ini.fit, cov.model="gau") 1811. cru.v2 <- variog(cru, uvec=seq(0,12, l=8), lambda=0) 1812. cru.v2.exp <- variofit(cru.v2, ini=ini.fit, cov.model="exp") 1813. cru.v2.gau <- variofit(cru.v2, ini=ini.fit, cov.model="gau") 1814. cru.v3 <- variog(cru, uvec=seq(1,15, l=8), lambda=0)

1815. cru.v3.exp <- variofit(cru.v3, ini=ini.fit, cov.model="exp") 1816. cru.v3.gau <- variofit(cru.v3, ini=ini.fit, cov.model="gau") 1817. cru.v4 <- variog(cru, uvec=seq(0,9, l=12), lambda=0) 1818. cru.v4.exp <- variofit(cru.v4, ini=ini.fit, cov.model="exp") 1819. cru.v4.gau <- variofit(cru.v4, ini=ini.fit, cov.model="gau") 1820. cru.v5 <- variog(cru, uvec=seq(0,12, l=12), lambda=0) 1821. cru.v5.exp <- variofit(cru.v5, ini=ini.fit, cov.model="exp") 1822. cru.v5.gau <- variofit(cru.v5, ini=ini.fit, cov.model="gau") 1823. cru.v6 <- variog(cru, uvec=seq(1,15, l=12), lambda=0) 1824. cru.v6.exp <- variofit(cru.v6, ini=ini.fit, cov.model="exp") 1825. cru.v6.gau <- variofit(cru.v6, ini=ini.fit, cov.model="gau") 1826. ## 1827. par(mfrow=c(3,2), mar=c(3,3,.5,.5), mgp=c(1.8,.8,0)) 1828. plot(cru.v1, ylim=c(0, 3.6)) 1829. lines(cru.v1.exp) 1830. lines(cru.v1.gau) 1831. plot(cru.v2, ylim=c(0, 3.6)) 1832. lines(cru.v2.exp) 1833. lines(cru.v2.gau) 1834. plot(cru.v3, ylim=c(0, 3.6)) 1835. lines(cru.v3.exp) 1836. lines(cru.v3.gau) 1837. plot(cru.v4, ylim=c(0, 3.6)) 1838. lines(cru.v4.exp) 1839. lines(cru.v4.gau) 1840. plot(cru.v5, ylim=c(0, 3.6)) 1841. lines(cru.v5.exp) 1842. lines(cru.v5.gau) 1843. plot(cru.v6, ylim=c(0, 3.6)) 1844. lines(cru.v6.exp) 1845. lines(cru.v6.gau) 1846. par(par.ori)

1. Yet another example of the steps of a data analysis.


1847. 1848. 1849. 1850. 1851. 1852. 1853. 1854. 1855. 1856. 1857. 1858. ## carregando a geoR require(geoR) ## vendo os conjuntos de dados disponveis na geoR data(package="geoR") ## carregando o conjunto de dados ksat data(Ksat) ## obtendo informaes sobre os dados # help(ksat) ## carregando o conjunto de dados ca20

1859. data(ca20) 1860. ## vendo informaes sobre os dados 1861. # help(ca20) 1862. ## resumo dos dados 1863. summary(Ksat) 1864. ## vendo os elementos do objeto Ksat 1865. names(Ksat) 1866. ## visualizando os dados 1867. points(Ksat, bor=borders) 1868. # outra visualizao dos dados 1869. plot(Ksat, bor=borders) 1870. # os dados so fortemente assimtricos e alm disto a transformao 1871. # log() deve ser razovel dada a natureza da varivel 1872. # Novo plot com dados transformados 1873. plot(Ksat, bor=borders, lambda=0) 1874. 1875. 1876. ## carregando o conjunto de dados parana 1877. data(parana) 1878. ## vendo informaes sobre os dados 1879. # help(parana) 1880. names(parana) 1881. summary(parana) 1882. points(parana, bor=borders) 1883. plot(parana, bor=borders) 1884. 1885. ## os dados exibem uma clara tendncia com as coordenadas 1886. ## reexaminando os dados, descontando o efeito da tendncia 1887. plot(parana, bor=borders, trend="1st") 1888. 1889. 1890. ## dados de clcio do solo 1891. data(ca20) 1892. # help(ca20) 1893. summary(ca20) 1894. names(ca20) 1895. points(ca20, bor=borders) 1896. polygon(ca20$reg1) 1897. polygon(ca20$reg2) 1898. 1899. plot(ca20, bor=borders) 1900. ## investigando relao com covariveis 1901. with(ca20, plot(covariate$altitude, data)) 1902. with(ca20, plot(covariate$area, data)) 1903. 1904. ## refazendo plot removend efeito das reas 1905. plot(ca20, bor=borders, trend=~area) 1906. 1907. ## Buscando a melhor transformao para os dados 1908. require(MASS) 1909. 1910. boxcox(Ksat) 1911. boxcox(Ksat, lambda=seq(-1,1,lam=101)) 1912. 1913. boxcox(parana, trend="1st")

1914. 1915. boxcox(ca20, trend=~area) 1916. 1917. data(SIC) 1918. boxcox(sic.100) 1919. boxcox(sic.all, lambda=seq(0, 1, l=101)) 1920. 1921. plot(Ksat, bor=borders) 1922. plot(Ksat, bor=borders, lam=0) 1923. 1924. plot(parana, bor=borders, trend="1st") 1925. plot(parana, bor=borders, trend="1st", lam=0.5) 1926. 1927. plot(sic.all, bor=sic.borders, lambda=0.5) 1928. 1929. ## explorando a presena de dependncia espacial 1930. ## compare os grficos dos dados originais com os obtidos 1931. ## aps permutar os dados nas diferentes localizaes 1932. par(mfrow=c(1,2), mar=c(3,3,.5, .5), mgp=c(2,1,0)) 1933. points(Ksat, lam=0, bor=borders, cex.max=1, cex.min=1, pt.div="quint") 1934. points(Ksat, lam=0, bor=borders, cex.max=1, cex.min=1, pt.div="quint", permute=T) 1935. par(mfrow=c(1,1)) 1936. 1937. par(mfrow=c(1,2)) 1938. points(parana, lam=0.5, bor=borders, trend="1st", cex.min=1,cex.max=1, 1939. pt.div="quint") 1940. points(parana, lam=0.5, bor=borders, trend="1st", cex.min=1,cex.max=1, 1941. pt.div="quint", permute=T) 1942. par(mfrow=c(1,1)) 1943. 1944. par(mfrow=c(1,2)) 1945. points(ca20, bor=borders, trend=~area, cex.min=1, cex.max=1, 1946. pt.div="quint") 1947. points(ca20, bor=borders, trend=~area, cex.min=1, cex.max=1, 1948. pt.div="quint", permute=T) 1949. par(mfrow=c(1,1)) 1950. 1951. 1952. ## variogramas 1953. Ksat.v <- variog(Ksat, max.dist=8.5, uvec=seq(0,8,by=1.25), lam=0) 1954. plot(Ksat.v) 1955. 1956. parana.v <- variog(parana, trend="1st", lam=0.5, max.dist=500) 1957. plot(parana.v) 1958. 1959. ca20.v <- variog(ca20, trend=~area, max.dist=450, op="cloud") 1960. plot(ca20.v) 1961. ca20.v <- variog(ca20, trend=~area, max.dist=450)

1962. plot(ca20.v) 1963. 1964. 1965. ## ajusta variogramas "a olho" com a funo eyefit 1966. ca20.ef <- eyefit(ca20.v) 1967. ca20.ef 1968. 1969. Ksat.ef <- eyefit(Ksat.v) 1970. Ksat.ef 1971. 1972. 1973. ## Interpolando valores 1974. 1975. ## definindo um grid de pontos cobrindo a rea 1976. grid0 <- expand.grid(seq(0, 22.5, by=0.25) , seq(0, 13, by=0.25)) 1977. ## visualizando o grid 1978. points(Ksat, bor=borders) 1979. points(grid0, pch="+", col="green") 1980. 1981. # selecionando os pontos dentro das bordas da area 1982. grid1 <- polygrid(grid0, borders=Ksat$borders) 1983. points(grid1, pch="+", col="blue") 1984. 1985. ## procedendo a interpolao 1986. Ksat.in <- krige.conv(Ksat, loc=grid0, borders=Ksat$borders, 1987. krige=krige.control(obj=Ksat.ef)) 1988. # visualizando resultados 1989. image(Ksat.in, col=gray(seq(1, 0.1, l=21))) 1990. image(Ksat.in, val=sqrt(Ksat.in$krige.var),col=gray(seq(1, 0.1, l=21))) 1991. 1992. save.image() 1993. 1994. ## estimativa por ajuste de mnimos quadrados 1995. Ksat.vf <- variofit(Ksat.v, ini=c(1.7, 3), nug=0.5) 1996. ## ou pode usar o eyefit como valor inicial... 1997. Ksat.vf <- variofit(Ksat.v, ini=Ksat.ef) 1998. 1999. ## estimativa de parametros pela verossimilhanca 2000. Ksat.ml <- likfit(Ksat, ini=c(1.7, 3), nug=0.5, lam=0) 2001. ## ou pode usar o eyefit como valor inicial... 2002. Ksat.ml <- likfit(Ksat, ini=Ksat.ef, lam=0) 2003. 2004. ## comparando modelos 2005. Ksat.ml1 <- likfit(Ksat, ini=c(1.7, 3), nug=0.5, lam=0, cov.model="sph") 2006. Ksat.ml2 <- likfit(Ksat, ini=c(1.7, 3), nug=0.5, lam=0, kappa=1) 2007. Ksat.ml3 <- likfit(Ksat, ini=c(1.7, 3), nug=0.5, lam=0, kappa=1.5) 2008. Ksat.ml4 <- likfit(Ksat, ini=c(1.7, 3), nug=0.5, lam=0, kappa=2.5) 2009. Ksat.ml5 <- likfit(Ksat, ini=c(1.7, 3), nug=0.5, lam=0, kappa=3.5)

2010. Ksat.ml6 <- likfit(Ksat, ini=c(1.7, 3), nug=0.5, lam=0, kappa=4.5) 2011. 2012. options(digits=7) 2013. Ksat.ml$loglik 2014. Ksat.ml2$loglik 2015. Ksat.ml3$loglik 2016. Ksat.ml4$loglik 2017. Ksat.ml5$loglik 2018. Ksat.ml6$loglik 2019. 2020. kps <- c(0.5, 1, 1.5, 2.5, 3.5, 4.5) 2021. llk <c(Ksat.ml$loglik,Ksat.ml2$loglik,Ksat.ml3$loglik,Ksat.ml4$loglik,Ks at.ml5$loglik, Ksat.ml6$loglik) 2022. plot(kps, llk) 2023. 2024. ## selecionando modelo para ca20 2025. ca20.ml1 <- likfit(ca20, ini=c(60, 100), nug=30) 2026. ca20.ml2 <- likfit(ca20, ini=c(60, 100), nug=30, trend=~area) 2027. ca20.ml3 <- likfit(ca20, ini=c(60, 100), nug=30, trend=~area) 2028. ca20.ml4 <- likfit(ca20, ini=c(60, 100), nug=30, trend=~area+altitude) 2029. ca20.ml5 <- likfit(ca20, ini=c(60, 100), nug=30, trend=~area+ca20$coords[,2]) 2030. ca20.ml6 <- likfit(ca20, ini=c(60, 100), nug=30, trend=~area+altitude+ca20$coords[,2]) 2031. 2032. ca20.fits <- rbind( 2033. c(ca20.ml1$npars,ca20.ml2$npars,ca20.ml3$npars,ca20.ml4$np ars,ca20.ml5$npars,ca20.ml6$npars), 2034. c(ca20.ml1$loglik,ca20.ml2$loglik,ca20.ml3$loglik,ca20.ml4 $loglik,ca20.ml5$loglik,ca20.ml6$loglik), 2035. c(ca20.ml1$AIC,ca20.ml2$AIC,ca20.ml3$AIC,ca20.ml4$AIC,ca20 .ml5$AIC,ca20.ml6$AIC), 2036. c(ca20.ml1$BIC,ca20.ml2$BIC,ca20.ml3$BIC,ca20.ml4$BIC,ca20 .ml5$BIC,ca20.ml6$BIC)) 2037. colnames(ca20.fits) <- paste("modelo", 1:6) 2038. rownames(ca20.fits) <- c("np", "ll", "AIC", "BIC") 2039. ca20.fits 2040. 2041. ## mais opes na interpolao espacial 2042. args(krige.conv) 2043. args(output.control) 2044. OC <- output.control(n.post=500, quantile=c(0.1, 0.9), thres=2) 2045. Ksat.in <- krige.conv(Ksat, loc=grid0, borders=Ksat$borders, 2046. krige=krige.control(obj=Ksat.ef), 2047. output=OC) 2048. names(Ksat.in) 2049. dim(Ksat.in$simulations) 2050. dim(Ksat.in$quantile) 2051. 2052. par(mfcol=c(2,3), mar=c(2,2,0,0), mgp=c(1.8, .8, 0))

2053. image(Ksat.in, col=gray(seq(1, 0, l=21)), x.leg=c(5,20), 2054. y.leg=c(-4,-2.5)) 2055. text(12, 15, "Mapa de E[Y_0|y]") 2056. image(Ksat.in, val=sqrt(Ksat.in$krige.var),col=gray(seq(1, 0, l=21)), x.leg=c(5,20), 2057. y.leg=c(-4,-2.5)) 2058. text(12, 15, "Mapa de SD[Y_0|y]") 2059. image(Ksat.in, val=Ksat.in$simulations[,23],col=gray(seq(1, 0, l=21)), x.leg=c(5,20), 2060. y.leg=c(-4,-2.5)) 2061. text(12, 15, "Mapa de uma simulao de [Y_0|y]") 2062. image(Ksat.in, val=Ksat.in$prob,col=gray(seq(1, 0.2, l=21)), x.leg=c(5,20), 2063. y.leg=c(-4,-2.5)) 2064. text(12, 15, "Mapa de probabilidade P[Y_0 < 2]") 2065. image(Ksat.in, val=Ksat.in$quant[,1],col=gray(seq(1, 0, l=21)), x.leg=c(5,20), y.leg=c(-4,-2.5)) 2066. text(12, 15, "Mapa de do quantil 0.10") 2067. image(Ksat.in, val=Ksat.in$quant[,2],col=gray(seq(1, 0, l=21)), x.leg=c(5,20), y.leg=c(-4,-2.5)) 2068. text(12, 15, "Mapa de do quantil 0.90")

2069.
2070. 2071. 2072. 2073. 2074. 2075. 2076. 2077. 2078. 2079. 2080. 2081. 2082. 2083. 2084. 2085. 2086. 2087. 2088. 2089. 2090. 2091. 2092.

More on Bayesian analysis 1. An example of Bayesian prediction: Illustrates the usage of the geoR's function krige.bayes()
## Exemplificando uma anlise Bayesiana require(geoR) set.seed(345) data(s100) summary(s100) plot(s100) points(s100) plot(variog(s100, max.dist=1.1)) ## Usando a funo krige.bayes() args(krige.bayes) args(model.control) args(prior.control) args(output.control) ## MC MC ## PC PC ## Definindo o modelo <- model.control() Definindo a priori <- prior.control(phi.discrete=seq(0,1,l=11)) Definindo a resultados a serem retornados

2093. ## - quantis (quartis) .25, .50 e .75 2094. ## - Probabilidade da varivel ser menor que 1.5 2095. OC <- output.control(n.pos=500, n.pred=100,quantile=c(.25,.5, .75), thres=1.5) 2096. OC 2097. ## Definindo o grid de predio 2098. gp <- expand.grid(seq(0,1,l=30), seq(0,1,l=30)) 2099. ## Rodando a funo 2100. s100.kb <- krige.bayes(s100, loc=gp, model=MC, prior=PC, out=OC) 2101. ## examinando os resultados 2102. names(s100.kb) 2103. ## examinando a posteriori: 2104. names(s100.kb$posterior) 2105. s100.kb$posterior$beta 2106. s100.kb$posterior$sigmasq 2107. s100.kb$posterior$phi 2108. s100.kb$posterior$samples 2109. 2110. ## examinando a preditiva 2111. names(s100.kb$predictive) 2112. ## fazendo um mapa da mdia 2113. image(s100.kb) 2114. ## fazendo um mapa da mdia 2115. image(s100.kb) 2116. ## fazendo um mapa dos erros padro de predio 2117. image(s100.kb, val=sqrt(s100.kb$pred$variance)) 2118. image(s100.kb, val=sqrt(s100.kb$pred$variance), coords.data=s100$coords) 2119. 2120. ## examinando e mapeando os quartis 2121. s100.kb$pred$quant[1:5,] 2122. ## mapa do 1o quartil 2123. image(s100.kb, val=s100.kb$pred$quant[,1]) 2124. ## mapa do 2o quartil (mediana) 2125. image(s100.kb, val=s100.kb$pred$quant[,2]) 2126. ## mapa do 3o quartil 2127. image(s100.kb, val=s100.kb$pred$quant[,3]) 2128. 2129. ## Mapeando a probabilidade P[S < 1.5] 2130. image(s100.kb, val=s100.kb$pred$prob) 2131. ## Mapeando a probabilidade P[S > 1.5] 2132. image(s100.kb, val=1-s100.kb$pred$prob) 2133. 2134. ## inspecionando as simulaes da preditiva 2135. dim(s100.kb$pred$simul) 2136. ## fazendo mapas de 4 simulaes de [S|y] 2137. par.ori <- par(no.readonly=T) 2138. par(mfrow=c(2,2), mar=c(1,1,0,0), mgp=c(1.5,.8,0)) 2139. image(s100.kb, val=s100.kb$pred$simul[,1]) 2140. image(s100.kb, val=s100.kb$pred$simul[,2]) 2141. image(s100.kb, val=s100.kb$pred$simul[,3]) 2142. image(s100.kb, val=s100.kb$pred$simul[,4]) 2143. par(par.ori) 2144. 2145. ## Obtendo a distribuio a posterior de [T|y] 2146. ## onde T a porcentagem da rea que na qual o

2147. 2148. 2149. 2150. 2151. 2152. 2153. 2154. 2155. 2156. 2157. 2158. 2159.

## valor do atributo est acima de 2 ## calculando para 1 simulao apenas sum(s100.kb$pred$simul[,3] > 2)/900 ## agora definindo uma funo para aplicar em todas ## as simulaes farea2 <- function(x) sum(x>2)/length(x) ## Aplicando a funo para obter a preditiva de [T|y] t.y <- apply(s100.kb$pred$simul,2,farea2) hist(t.y) ## obtendo um intervalo de credibilidade a 95% quantile(t.y, prob=c(0.025, 0.975))

1. Further examples for the function krige.bayes are given in the file examples.krige.bayes.R

equire(geoR) set.seed(285) ## ## Further examples for the usage of krige.bayes() ## ----------------------------------------------## ## Before reading this see documentation for krige.bayes ## > help(krige.bayes) ## ## WARNING: THESE CALLS CAN BE TIME AND SPACING DEMMANDING ## Simulating data ex.data <- grf(50, cov.pars=c(10, .25)) ## ## Basic usage ## ## a basic and simple call to the function ex.post <- krige.bayes(ex.data) ex.post names(ex.post) ## different input and output options ex1 <- krige.bayes(ex.data, prior = list(phi.prior = "fixed", phi = 0.3)) ex1 <- krige.bayes(ex.data, model = list(cov.model="spherical")) ex1 <- krige.bayes(ex.data, output = list(n.posterior = 100)) ## now performing prediction ex.grid <- as.matrix(expand.grid(seq(0,1,l=6), seq(0,1,l=6))) ex.bayes <- krige.bayes(ex.data, loc=ex.grid, prior = prior.control(phi.discrete=seq(0, 2, l=3), tausq.rel.discrete=seq(0, 2, l=3)), output=output.control(n.post=100))

names(ex.bayes) ## some graphical output ... ## ... for the posterior ... plot(ex.data) lines(ex.bayes, sum = mean) plot(ex.bayes) lines(ex.bayes, summ="median", lty=2, post="par") lines(ex.bayes, summ="mean", lwd=2, lty=2, post="par") ## ... and for the predictive op <- par(no.readonly = TRUE) par(mfrow=c(2,2)) par(mar=c(3,3,1,1)) par(mgp = c(2,1,0)) image(ex.bayes, main="predicted values") image(ex.bayes, val="variance", main="prediction variance") image(ex.bayes, val= "simulation", number.col=1, main="a simulation from the \npredictive distribution") image(ex.bayes, val= "simulation", number.col=2, main="another simulation from \nthe predictive distribution") ## Now including tausq (the nugget variance term) PC <- prior.control(phi.prior = c(.1, .2, .3, .2, .1, .1), phi.disc=seq(0.1, 0.6, l=6), tausq.rel.prior=c(.1, .4, .3, .2), tausq.rel.discrete=c(0,.1,.2,.3)) ex.user <- krige.bayes(ex.data, prior = PC) ex.user ## ## Example for "sequential analysis" ## ## Simulating data at 2 different "times" ap1 <- grf(50, cov.pars=c(1, .3)) ap2 <- grf(70, cov.pars=c(1, .3)) ## A initial "usual" analysis ap1.kb <- krige.bayes(ap1) ap1.kb ## and now using the previously obtained posterior as prior for next call ap2.kb <- krige.bayes(ap2, prior=post2prior(ap1.kb)) ap2.kb ## ## Another example with a "user defined" prior ## PC <- prior.control(phi.prior=c(.2,.3,.2,.1,.1,.1), phi.discrete = seq(0,.5,l=6)) ##ap3.kb <- krige.bayes(ap1, prior = PC) ap3.kb <- krige.bayes(ap1, prior = PC) ap3.kb ## ap4.kb <- krige.bayes(ap2, prior=post2prior(ap3.kb)) ap4.kb ## ## Now including tausq

## PC <- prior.control(tausq.rel.prior = "uni", tausq.rel.discrete = seq(0, .5, l=6)) ap5.kb <- krige.bayes(ap1) ap5.kb ## using the previous posterior as prior for next call ap6.kb <- krige.bayes(ap2, prior=post2prior(ap5.kb)) ap6.kb ## ## ## PC <- prior.control(phi.prior=c(.2,.3,.2,.1,.1,.1), phi.discrete = seq(0,.5,l=6), tausq.rel.prior=c(.3,.4,.3), tausq.rel.discrete = c(0, .1, .2)) ap7.kb <- krige.bayes(ap1, prior = PC) ap7.kb ap8.kb <- krige.bayes(ap2, prior=post2prior(ap7.kb)) ap8.kb ## ## An example with a trend term in the model and different priors ## data(s100) prior2.b9 <- prior.control(beta.prior = "normal", beta = c(0,0,0), beta.var = cbind(c(2,1.5,0),c(1.5,1.8,.2),c(0,0.2,1.5)), phi.prior = "exponential", phi = 2.5, phi.discrete = c(2.5,3), sigmasq.prior = "sc.inv.chisq", df.sigmasq = 5, sigmasq = 0.5) ap <- krige.bayes(s100, prior=prior2.b9, model=model.control(trend.d = "1st")) ap prior2.b9 <- prior.control(beta.prior = "normal", beta = c(0,0,0), beta.var = cbind(c(2,1.5,0),c(1.5,1.8,0.5),c(0,0.5,1.5)), phi.prior = "fixed", phi = 2.5, sigmasq.prior = "sc.inv.chisq", df.sigmasq = 5, sigmasq = 0.5) ap <- krige.bayes(s100, prior=prior2.b9, model=model.control(trend.d="1st")) ap prior2.b9 <- prior.control(beta.prior = "normal", beta = 0,beta.var =1, phi.prior = "fixed", phi = 2.5,sigmasq.prior ="sc.inv.chisq", df.sigmasq = 5, sigmasq = 0.5) ap <- krige.bayes(s100, prior=prior2.b9) ap par(op)

2160.

Bivariate geostatistical data 1. Simulating bivariate proccess:

2161. ## 2162. ## Simulating bivariate data 2163. ## 2164. require(geoR) 2165. set.seed(475) 2166. 2167. ## definindo grid de simulao 2168. gs <- expand.grid((0:50)/50, (0:50)/50) 2169. n <- nrow(gs) 2170. 2171. ## Modelo 1: variveis no correlacionadas 2172. ## Y1 = mu1 + S1 + e1 = mu1 + sigma1 R1 + e1 2173. ## Y1 = mu2 + S2 + e2 = mu2 + sigma2 R2 + e2 2174. mu1 <- mu2 <- 0 2175. sigmasq1 <- 1 ; sigmasq2 <- 3 2176. phi1 <- 0.15 ; phi2 <- 0.3 2177. tausq1 <- 0 ; tausq2 <- 0 2178. 2179. ## Simulating Y1 2180. ## simulation in a single step using grf() 2181. var1 <- grf(grid=gs, cov.pars=c(sigmasq1,phi1), nug=tausq1, mean=mu1) 2182. ## equivalent simulation step by step: 2183. R1 <- grf(grid=gs, cov.pars=c(1, phi1)) ## nug=0 and mean=0 (defaults) 2184. e1 <- rnorm(n, sd=sqrt(tausq1)) 2185. var1 <- list(coords=gs, data=mu1+sqrt(sigmasq1)*R1$data+e1) 2186. class(var1) <- "geodata" 2187. 2188. ## Simulating Y2 2189. var2 <- grf(grid=gs, cov.pars=c(sigmasq2,phi2), nug=tausq2, mean=mu2) 2190. 2191. par(mfrow=c(2,2), mar=c(2,2,0,0), mgp=c(1.5,.6,0)) 2192. imae(var1) 2193. image(var2) 2194. plot(var1$data, var2$data) 2195. 2196. ## Modelo 2: variveis com correlao componente espacial comum 2197. ## Y1 = mu1 + S1 + e1 = mu1 + sigmasq1 R + e1 2198. ## Y1 = mu2 + S2 + e2 = mu2 + sigmasq2 R + e2 2199. mu1 <- mu2 <- 0 2200. sigmasq1 <- 1 ; sigmasq2 <- 3 2201. phi <- 0.25 2202. 2203. ## Simulao 2.1 : sem rudo (e1 = e2 = 0) 2204. tausq1 <- 0 ; tausq2 <- 0 2205. 2206. ## simulando o componente comum (R) 2207. var <- grf(grid=gs, cov.pars=c(1,phi)) 2208. 2209. ## montando a varivel 1

2210. var1 <- var 2211. var1$data <- mu1 + sqrt(sigmasq1) * var1$data 2212. 2213. ## montando a varivel 2 2214. var2 <- var 2215. var2$data <- mu2 + sqrt(sigmasq2) * var2$data 2216. 2217. par(mfrow=c(2,2), mar=c(2,2,0,0), mgp=c(1.5,.6,0)) 2218. image(var1) 2219. image(var2) 2220. plot(var1$data, var2$data) 2221. 2222. ## Simulao 2.2 : com rudo 2223. ## varincia de ruido 20% da varincia do sinal 2224. tausq1 <- 0.2 ; tausq2 <- 0.6 2225. 2226. ## simulando o componente comum (R) 2227. var <- grf(grid=gs, cov.pars=c(1,phi)) 2228. 2229. ## montando a varivel 1 2230. var1 <- var 2231. var1$data <- sqrt(sigmasq1) * var1$data 2232. var1$data <- mu1 + var1$data + rnorm(n, mean=0, sd=sqrt(tausq1)) 2233. 2234. ## montando a varivel 2 2235. var2 <- var 2236. var2$data <- sqrt(sigmasq2) * var2$data 2237. var2$data <- mu2 + var2$data + rnorm(n, 0, sqrt(tausq2)) 2238. 2239. par(mfrow=c(2,2), mar=c(2,2,0,0), mgp=c(1.5,.6,0)) 2240. image(var1) 2241. image(var2) 2242. plot(var1$data, var2$data) 2243. 2244. ## Modelo 3: variveis com correlao componente espacial parcialmente comum 2245. ## Y1 = mu1 + S11 + S12 + e1 = mu1 + sigmasq01 R + sigmasq11 R11 + e1 2246. ## Y1 = mu2 + S21 + S22 + e2 = mu2 + sigmasq02 R + sigmasq21 R21 + e2 2247. mu1 <- mu2 <- 0 2248. ## parmetros do componente comum 2249. sigmasq01 <- 0.7 ; sigmasq02 <- 2.1 2250. phi0 <- 0.25 2251. ## parmetros dos componentes individuais 2252. sigmasq11 <- 0.3 ; sigmasq21 <- 0.9 2253. phi1 <- 0.15 ; phi2 <- 0.30 2254. ## rudos 2255. tausq1 <- 0 ; tausq2 <- 0 2256. 2257. ## simulando o componente comum (R) 2258. var <- grf(grid=gs, cov.pars=c(1,phi0)) 2259. ## adicionando o componente comum s variveis 2260. var1$data <- sqrt(sigmasq01) * var$data 2261. var2$data <- sqrt(sigmasq02) * var$data 2262.

2263. ## adicionando os componentes individuais (R11 and R21) e rudos (e1 e e1) 2264. var1$data <- var1$data + grf(grid=gs, cov.pars=c(sigmasq11,phi1), nug=tausq1)$data 2265. var2$data <- var2$data + grf(grid=gs, cov.pars=c(sigmasq21,phi2), nug=tausq2)$data 2266. 2267. par(mfrow=c(2,2), mar=c(2,2,0,0), mgp=c(1.5,.6,0)) 2268. image(var1) 2269. image(var2) 2270. plot(var1$data, var2$data)

1. Inference and prediction for the bivariate Gaussian common component model:
2271. require(geoR) 2272. ## 2273. ## Testing code: Inference in the 2274. ## Bivariate Gaussian Common Component Model 2275. ## 2276. ## 1. Simulating Y1 and Y2 with some (but not all) locations in common 2277. ## 2278. ## 1.1. Coordinates 2279. set.seed(30) 2280. all.coords <- round(cbind(runif(200), runif(200)), dig=3) 2281. set.seed(111); ind1 <- sample(1:200, 120) 2282. coords1 <- all.coords[ind1,] 2283. set.seed(222); ind2 <- sample(1:200, 80) 2284. coords2 <- all.coords[ind2,] 2285. ## 2286. ## Model parameters: 2287. ## m1 = 10, mu2 = 50 2288. ## sigma01 = 2, sigma1 = 1.5, sigma02 = 8, sigma2 = 6 2289. ## phi0 = 0.2, phi1=0.15, phi2=0.25 2290. mu1 <- 10; mu2 <- 50 2291. sigma01 <- 2; sigma1 <- 1.5; sigma02 <- 8; sigma2 <- 6 2292. phi0 <- 0.2; phi1 <- 0.15; phi2 <- 0.25 2293. ## 2294. ## Model parameters (reparametrised) 2295. ## sigma = 2, nu1 = 0.75, eta = 4, sigma2 = 3 2296. sigma <- sigma01 2297. nu1 <- sigma1/sigma 2298. eta <- sigma02/sigma; nu2 <- sigma2/sigma 2299. ## 2300. ## Simulating model components 2301. ## 2302. S0 <- grf(grid=all.coords, cov.pars=c(sigma, phi0))$data 2303. S1 <- grf(grid=coords1, cov.pars=c(sigma1, phi1))$data 2304. S2 <- grf(grid=coords2, cov.pars=c(sigma2, phi2))$data 2305. ## 2306. ## Y1 and Y2 data

2307. ## 2308. y1 <- as.geodata(cbind(coords1,10+S0[ind1]+S1)) 2309. y2 <- as.geodata(cbind(coords2,50+S0[ind2]+S2)) 2310. ## 2311. ## maximum likelihood estimation 2312. ## 2313. fit12 <- likfitBGCCM(y1, y2, ini.s=c(2,1.5,8,6), ini.phi=c(.2,.15,.25), control=list(trace=T)) 2314. fit12 2315. fit12a <- likfitBGCCM(y1, y2, ini.s=c(2,1.5,8,6), ini.phi=c(.2,.15,.25), method="BFGS") 2316. fit12a 2317. fit12b <- likfitBGCCM(y1, y2, ini.s=c(2,1.5,8,6), ini.phi=c(.2,.15,.25), method="L-BFGS-B", control=list(trace=T)) 2318. fit12b 2319. fit12c <- likfitBGCCM(y1, y2, ini.s=c(2,1.5,8,6), ini.phi=c(.2,.15,.25), fc.min="nlminb") 2320. fit12c 2321. ## 2322. ## Prediction 2323. ## 2324. locs <- cbind(c(0.2, 0.5, 0.7, 0.2), c(0.3, 0.5, 0.2, 0.8)) 2325. pred1 <- predict(fit12, loc=locs) 2326. pred1[1:2] 2327. pred2 <- predict(fit12, loc=locs, var=2) 2328. pred2[1:2]

2329. Other data-sets (in addition to the ones already included in the package) Data this is a directory with data-sets used in some of the tutorials. Typically there are two files corresponding to each data set:
o

files with extension .dat are the data files.

files with extension .txt are files with a brief explanation on the

ndex of

/geoR/tutorials/datasets
Name Parent Directory Last modified Size Description -

Cruciani.border 08-Nov-2004 12:30 121

Cruciani.dat Cruciani.txt ca20.dat ca20.txt

08-Nov-2004 12:30 531 08-Nov-2004 12:30 279 22-Aug-2002 23:55 7.2K 22-Aug-2002 23:55 297

concavidade.dat 08-Nov-2004 12:30 16K concavidade.txt 08-Nov-2004 12:30 401 ctc.dat ctcborda.dat gravity.dat gravity.txt landim1.dat landim1.txt maxtemp.dat maxtemp.txt nemat.dat ouro.dat ouro.txt profund020.txt 09-Nov-2004 14:53 3.6K 09-Nov-2004 14:53 171 08-Nov-2004 12:30 308 08-Nov-2004 12:30 510 08-Nov-2004 12:30 809 08-Nov-2004 12:30 252 08-Nov-2004 12:30 6.1K 08-Nov-2004 12:30 273 10-Oct-2002 23:24 726 16-Nov-2004 17:54 1.4K 22-Nov-2004 22:18 54 23-Apr-2008 15:34 6.0K

profund020.xls 08-Nov-2004 12:31 37K profund2040.txt 23-Apr-2008 15:34 6.2K regioes.dat sonda.dat sonda.txt xy.dat xy.txt 08-Nov-2004 12:32 8.5K 08-Nov-2004 12:32 153 08-Nov-2004 12:32 351 13-Feb-2006 06:47 33 25-Nov-2004 11:01 213

Apache/2.2.16 (Debian) Server at leg.ufpr.br Port 80


o

data.

2330.

geoR and sp: converting data formats

0. Converting kriging results to SpatialGridDataFrame and SpatialPixelsDataFrame formats


2331. require(geoR) 2332. ## 2333. ## Converting krige.conv() output do SpatialGridDataFrame and SpatialPixelDataFrame 2334. ## formats defined by the "sp" package 2335. ## 2336. 2337. ## A conversion function will soon be added to the geoR package 2338. 2339. ## 2340. ## Example 1: converting a rectangular prediction grid 2341. ## 2342. 2343. data(s100) 2344. ml.s <- likfit(s100, ini=c(1, .20)) 2345. gr.s <- expand.grid(seq(0,1,l=15), seq(0,1,len=15)) 2346. kc.s <- krige.conv(s100, loc=gr.s, krige=krige.control(obj=ml.s)) 2347. 2348. ## Option 1: converting krige.conv() prediction grid results to SpatialGridDataFrame 2349. ## building the grid topology step-by-step 2350. cs <- c(min(abs(diff(unique(gr.s[,1])))), min(abs(diff(unique(gr.s[,2]))))) 2351. cs 2352. cco <- unname(apply(gr.s,2,min)) 2353. cco 2354. cd <- ceiling(diff(t(bbox(as.matrix(gr.s))))/cs) 2355. GT.s <- GridTopology(cellcentre.offset = cco, cellsize=cs, cells.dim=cd) 2356. GT.s 2357. 2358. ## or ... a more direct/straightforward way 2359. GT.s <- points2grid(SpatialPoints(as.matrix(gr.s))) 2360. GT.s 2361. 2362. ## notice the need to reorder coordinates 2363. reorder <- as.vector(matrix(1:nrow(gr.s), nc=slot(GT.s, "cells.dim")[2])[,slot(GT.s, "cells.dim")[2]:1]) 2364. SGDF.s <- SpatialGridDataFrame(grid=GT.s, 2365. data=as.data.frame(kc.s[1:2])[reorder,]) 2366. rm(reorder) 2367. 2368. ## comparing image plot of the objects 2369. par(mfrow=c(2,2), mar=c(1.5,1.5,1,0.5), mgp=c(1.8,0,0)) 2370. image(kc.s) 2371. image(SGDF.s, axes=T) 2372. image(kc.s, val=kc.s$krige.var) 2373. image(SGDF.s['krige.var'], axes=T) 2374. 2375. ## Option 2: converting to SpatialPixelDataFrame and then to SpatialGridDataFrame

2376. SPxDF.s <- SpatialPixelsDataFrame(gr.s, data = as.data.frame(kc.s[1:2])) 2377. SGDF.s1 <- as(SPxDF.s, "SpatialGridDataFrame") 2378. 2379. ## image plots 2380. par(mfrow=c(2,3), mar=c(1.5,1.5,1,0.5), mgp=c(1.8,0,0)) 2381. image(kc.s, main="krige.conv") 2382. image(SPxDF.s, axes=T) 2383. image(SGDF.s, axes=T) 2384. image(kc.s, val=kc.s$krige.var, main="krige.conv") 2385. image(SPxDF.s['krige.var'], axes=T) 2386. image(SGDF.s['krige.var'], axes=T) 2387. 2388. 2389. ## Option 3: yet another way by converting the prediction grid to SpatialPixelDataFrame 2390. gr.s$predict <- NA 2391. coordinates(gr.s) <- names(gr.s)[1:2] 2392. gridded(gr.s) <- TRUE 2393. class(gr.s) 2394. gr.s$predict <- kc.s$pred 2395. gr.s$krige.var <- kc.s$krige.var 2396. 2397. gr.sSGDF <- as(gr.s, "SpatialGridDataFrame") 2398. class(gr.sSGDF) 2399. image(gr.sSGDF) 2400. 2401. ## 2402. ## Example 2: converting a grid defined in a nonrectangular region 2403. ## 2404. data(parana) 2405. ml.pr <- likfit(parana, ini=c(100, 40), trend="1st") 2406. ## defining a rectangular grid over the study area 2407. gr.pr <- pred_grid(parana$borders, by=20) 2408. points(parana) 2409. points(gr.pr, pch=19, cex=0.25, col=2) 2410. kc.pr <- krige.conv(parana, loc = gr.pr, krige=krige.control(obj.model=ml.pr, 2411. trend.d="1st", trend.l="1st")) 2412. 2413. ## Option 1: converting directly to SpatialGridDataFrame 2414. GT.pr <- points2grid(SpatialPoints(as.matrix(gr.pr))) 2415. GT.pr 2416. ind <- .geoR_inout(gr.pr, parana$borders) 2417. reorder <- as.vector(matrix(1:nrow(gr.pr), nc=slot(GT.pr, "cells.dim")[2])[,slot(GT.pr, "cells.dim")[2]:1]) 2418. 2419. df.pr <- data.frame(predict=rep(NA, nrow(gr.pr)), krige.var=rep(NA, nrow(gr.pr))) 2420. df.pr[ind,] <- as.data.frame(kc.pr[1:2]) 2421. SGDF.pr <- SpatialGridDataFrame(grid=GT.pr, data=df.pr[reorder,]) 2422. 2423. ## comparing image plot of the objects 2424. par(mfrow=c(2,2), mar=c(1.5,1.5,1,0.5), mgp=c(1.8,0,0))

2425. image(kc.pr) 2426. image(SGDF.pr, axes=T) 2427. image(kc.pr, val=kc.pr$krige.var) 2428. image(SGDF.pr['krige.var'], axes=T) 2429. 2430. 2431. ## 2432. ## Option 2: Converting to SpatialPixelDataFrame and then to SpatialGridDataFrame 2433. ## 2434. SPxDF.pr <- SpatialPixelsDataFrame(locations.inside(gr.pr, parana$borders), 2435. data = as.data.frame(kc.pr[1:2])) 2436. SGrDF.pr <- as(SPxDF.pr, "SpatialGridDataFrame") 2437. 2438. ## image plots 2439. par(mfrow=c(2,3), mar=c(1.5,1.5,1,0.5), mgp=c(1.8,0,0)) 2440. image(kc.pr) 2441. image(SPxDF.pr, axes=TRUE) 2442. image(SGrDF.pr, axes=TRUE) 2443. image(kc.pr, val=kc.pr$krige.var) 2444. image(SPxDF.pr["krige.var"], axes=TRUE) 2445. image(SGrDF.pr["krige.var"], axes=TRUE) 2446. 2447. ## Option 3: yet another way by converting the prediction grid to SpatialPixelDataFrame 2448. gr.pr$predict <- NA 2449. coordinates(gr.pr) <- names(gr.pr)[1:2] 2450. gridded(gr.pr) <- TRUE 2451. class(gr.pr) 2452. gr.pr$predict[ind] <- kc.pr$pred 2453. gr.pr$krige.var[ind] <- kc.pr$krige.var 2454. 2455. image(gr.pr) 2456. 2457. gr.prSGDF <- as(gr.pr, "SpatialGridDataFrame") 2458. class(gr.prSGDF) 2459. image(gr.prSGDF)

2460.

Other tutorials 0. An introduction to geoRglm: web page with the main aspects of the package geoRglm.

geoRglm Introductory session


Ole F. Christensen & Paulo J. Ribeiro Jr. 27 May 2005

The package geoRglm provides functions for inference in generalised linear spatial models using the software R. This document illustrates some of the capabilities of the package. We assume that the user has a substantial knowledge about geostatistics, is familar with the geoRpackage (see the introductory session for geoR), and has a basic knowledge about Markov chain Monte Carlo methods. We also encourage the reader to study the literature about generalised linear spatial models. A list of books, articles and a short FAQ can be found here. The main introduction to geoRglm is the package vignette, which is shown by typingvignette("geoRglmintro") in a session, and with R commands available in the inst/doc/ directory of the package.

ADDITIONAL INFORMATION
1. An old introductory page for geoRglm. See here.

2. The commands for the analysis in Christensen (2004) [JCGS] for MCMC-MLE are found here.

3. The commands for the example in Diggle, Ribeiro Jr and Christensen (2003) [bookchapter], and Christensen and Ribeiro Jr (2002) [Rnews] are found here.

4. The empirical covariogram derived in Christensen, Moller and Waagepetersen (2000) for the Poisson-log normal model. See here.

5. A short demonstration shown at my Ph.D. viva is found here.

ADDITIONAL INFORMATION
1. An old introductory page for geoRglm. See here.

geoRglm : a package for generalised linear spatial models


introductory session
Ole F. Christensen & Paulo J. Ribeiro Jr. 24 March 2003

The package geoRglm provides functions for inference in generalised linear spatial models using the software R. This document illustrates some of the capabilities of the package. We assume that the user has a substantial knowledge about geostatistics, is familar with the geoR package (see the introductory session for geoR), and has a basic knowledge about Markov chain Monte Carlo methods. We also encourage the reader to study the literature about generalised linear spatial models. A list of books, articles and a short FAQ can be found here). The objective of this page is to introduce the reader to the geoRglm commands and show how they can be used. The commands used here are basic examples of the package handling, where we typically use default arguments for the function calls. We encourage the user also to inspect other function arguments. For further details on the functions included in geoRglm, we refer to the geoRglm documentation.

1. STARTING A SESSION AND LOADING DATA


After starting an R session, we first load geoR and geoRglm with the commands:
library(geoR)

library(geoRglm)

If the installation directories for the packages are not the default locations for R packages, type:
library(geoR, lib.loc = "PATH_TO_geoR") library(geoRglm, lib.loc = "PATH_TO_geoRglm")

where "PATH_TO_geoR" and "PATH_TO_geoRglm" are the paths to the directories where geoR and geoRglm are installed, respectively. If the packages are correctly loaded the following messages will be displayed:
---------------------------------------------------geoR - a package for geostatistical analysis in R geoR version 1.3-10 (2003-03-10) is now loaded -------------------------------------------------------------------------------------------------------------geoRglm - a package for generalised linear spatial models geoRglm version 0.6-2 (2003-03-24) is now loaded -----------------------------------------------------------

Typically, data are stored as an object (a list) of class "geodata" (see the geoR introductory session for more details on this). For the data sets considered here, the object will sometimes include a vector units.m consisting of observation times (for the Poisson distribution) or numbers N in bi(N,p) (for the binomial distribution). We use the data sets p50 and rongelap included in the geoRglm distribution for the examples presented in this document. These data sets can be loaded by typing:
data(p50) data(rongelap)

Helpfiles are available for geoRglm. For getting help on the function pois.krige, just type:
help(pois.krige)

2. CONDITIONAL SIMULATION and SPATIAL PREDICTION


Here we describe conditional simulation using MCMC and spatial prediction in the Poisson-log normal model, when covariance parameters

are fixed. Full Bayesian methods are also implemented and will be presented in Section 3. The nugget effect parameter (microscale variation) in the underlying Gaussian field can be set to a fixed value. The same applies for the smoothness and anisotropy parameters. Options for taking covariates (trends) into account are also included. Conditional simulation and prediction with fixed covariance parameters in the Poisson-log normal model can be performed with options for either fixed beta (OK) or flat prior on beta (SK). The function uses a LangevinHastings MCMC algorithm for simulating from the conditional distribution. An example where all parameters are fixed is shown below (for illustration purposes, some parameter values are just taken). First we need to tune the algorithm by scaling the proposal variance so that acceptance rate is approximately 60 percent (optimal acceptance rate for Langevin-Hastings algorithm). This is done by trial and error.
model2 <- krige.glm.control(cov.pars = c(1,1), beta = 1) test2.tune <- pois.krige(p50, krige = model2, mcmc.input = list(S.scale = 0.2, thin = 1))

After a few tryouts we decide to use S.scale how well the chain is mixing.

= 0.5.

We also need to study

test2.tune <- pois.krige(p50, krige = model2, mcmc.input = list(S.scale = 0.5, thin = 1)) plot(log(test2.tune$intensity[45,]), type = "l") acf(log(test2.tune$intensity[45,]), type = "correlation", plot = TRUE)

Here the functions in the coda package would be useful for assessing the convergence and inspect the mixing of the MCMC algorithm. For a small demonstration of a few CODA functions used on the output above, see here.

To reduce the autocorrelation of the samples we decide to subsample every 10 iterations (default); when working with real data sets we may need to make a more enxtensive subsampling, say, storing only every 1000 iterations. Now we make (minimal mean square error) prediction of the intensity at the two locations (0.5, 0.5) and (1, 0.4).
test2 <- pois.krige(p50, locations = cbind(c(0.5,0.5),c(1,0.4)), krige = model2, mcmc.input = mcmc.control(S.scale = 0.5), output = output.glm.control(sim.predict = TRUE))

The output is a list including the predicted values (test2$predict), the prediction variances (test2$krige.var) and the estimated Monte Carlo standard errors on the predicted values (test2$mcmc.error). Please consider printing out the predicted values and the associated Monte Carlo standard errors:
test2$predict test2$mcmc.error

Note that the Monte Carlo standard errors (the errors due to the MCMCsimulation) are small compared to predicted values, which is very satisfactory. (Monte Carlo standard errors on the prediction variances is not implemented yet). By specifying sim.predict = TRUE, simulations are drawn from the predictive intensity at the two prediction locations (test2$simulations). These simulations are plotted below.
par(mfrow = c(1,2)) hist(test2$simulations[1,], main = "(0.5, 0.5) ") hist(test2$simulations[2,], main ="(1, 0.4)")

The way to specify that beta should follow a uniform prior would be:
model2.u <- krige.glm.control(cov.pars = c(1,1), beta = 1, type.krige = "ok") test2.unif.beta <- pois.krige(p50, krige = model2.u, mcmc.input = list(S.scale = 0.5))

3. BAYESIAN ANALYSIS
Bayesian analysis for the Poisson-log normal model and the binomial-logit model is implemented by the functionspois.krige.bayes and binom.krige.bayes, respectively. Model parameters can be treated as fixed or random. As an example consider first a model without nugget and including uncertainty in the beta and sigmasq parameters (mean and variance of the random effects S, respectively). A Bayesian analysis is made by typing commands like:
prior5 <- prior.glm.control(phi.prior = "fixed", phi = 0.1) mcmc5.tune <- mcmc.control(S.scale = 0.01, thin = 1) test5.tune <- pois.krige.bayes(p50, prior = prior5, mcmc.input = mcmc5.tune)

Now chose S.scale (Acc-rate=0.60 is preferable). After having adjusted the parameters for the MCMC algorithm and checking the output we run an analysis.
mcmc5 <- mcmc.control(S.scale = 0.075, thin = 100) test5 <- pois.krige.bayes(p50, locations = t(cbind(c(2.5,3),c(6050,-3270))), prior = prior5, mcmc.input = mcmc5, output = list(threshold = 10, quantile = c(0.05,0.99)))

The output is a list which contains the five arguments posterior, predictive, model, prior and mcmc.input. The posterior contains information on the posterior distribution of the parameters, and the conditional simulations of the signal g^{-1}(S) at the data locations. The predictive contains information on the predictions, where predictive$median is the predicted signal andpredictive$uncertainty is the associated uncertainty. The threshold = 10 argument gives probabilities of the predictive distribution of the signal being less than 10 (test5$predictive$probability). The quantiles = c(0.05,0.99) gives the 0.05 and 0.99 quantiles of the predictive distribution of the signal (test5$predictive$quantiles). Below we show the simulations from the posterior distribution of the signal at a few data locations.
par(mfrow = c(1,3)) hist(test5$posterior$simulations[10,], main = "(9, 0)") hist(test5$posterior$simulations[23,], main = "(2,2)") hist(test5$posterior$simulations[36,], main ="(5,3)")

Now we consider an example with a random correlation scale parameter phi and a positive nugget for the random effects S. The program is using a discretised prior fpr phi, where the discretisation is given by the argumentphi.discrete). The argument (tausq.rel = 0.05 gives the relative nugget for S, i.e. the relative microscale variation).
mcmc6.tune <- mcmc.control(S.scale = 0.075, n.iter = 2000, thin = 100, phi.scale = 0.01) prior6 <- prior.glm.control(phi.prior = "uniform", phi.discrete = seq(0.02, 1, 0.02), tausq.rel = 0.05) test6.tune <- pois.krige.bayes(p50, prior = prior6, mcmc.input = mcmc6.tune)

Acc-rate=0.60 , acc-rate-phi = 0.25-0.30 are preferable. After having adjusted the parameters for the MCMC algorithm and checking the output we run an analysis. WARNING: RUNNING THE NEXT COMMAND CAN BE TIMECONSUMING
mcmc6 <- mcmc.control(S.scale = 0.075, n.iter = 400000, thin = 200, burn.in = 5000, phi.scale = 0.12, phi.start = 0.5) test6 <- pois.krige.bayes(p50, locations = t(cbind(c(2.5,3.5),c(60,-37))), prior = prior6, mcmc.input = mcmc6)

Below we show the posterior distribution of the two covariance parameters and the beta parameter.
par(mfrow = c(1,3))

hist(test6$posterior$beta$sample, main ="beta") hist(test6$posterior$sigmasq$sample, main = "sigmasq") hist(test6$posterior$phi$sample, main = "phi")

To calculate the Monte Carlo standard errors on the posterior means of the parameters, we use the functionasympvar.
sqrt(asympvar(test6$posterior$beta$sample)/2000) sqrt(asympvar(test6$posterior$sigmasq$sample)/2000) sqrt(asympvar(test6$posterior$phi$sample)/2000) sqrt(asympvar(log(test6$posterior$simulations))/2000)

Exercise

Construct similar commands using binom.krige.bayes on the data set b50 yourself (you load the data set by typing data(b50)).

4. SIMULATION of a GENERALISED LINEAR SPATIAL MODEL


The geoR function grf generates a simulation from a Gaussian random field. This function can be used to generate a simulation from a generalised linear spatial model as follows.
sim <- grf(grid = expand.grid(x = seq(1, 10, l = 10), y = seq(1, 10, l = 10)), cov.pars = c(0.1, 0.2))

attr(sim,"class") <- "geodata" sim$units.m <- c(rep(1, 50), rep(5, 50)) sim$data <- rpois(100, lambda = sim$units.m*exp(sim$data)) plot(sim$coords[,1], sim$coords[,2], type = "n") text(sim$coords[,1], sim$coords[,2], format(sim$data))

Observe that the upper part of the figure corresponds to observation times equal to 5. Therefore the simulated counts are larger.
Exercise

Generate a simulation from a spatial binomial random field.

5. ADDITIONAL INFORMATION
1. Empirical covariogram for the Poisson-log normal model. See here.

2. A short demonstration shown at my Ph.D. viva is found here.

3. The commands from the example in Diggle, Ribeiro Jr and Christensen (2003) [bookchapter], and Christensen and Ribeiro Jr (2002) [R-news] are found here.

4. Empirical covariogram for the Poisson-log normal model. See here. 6. The empirical covariogram in Christensen, Mller and Waagepetersen (2000) for the Poisson-log normal model can be calculated using the function covariog. 7. covar <- covariog(rongelap, uvec = c((1:20)*40)) 8. Theoretical and empirical variograms can be plotted and visually compared. For example, the figure below shows the estimated covariogram, a theoretical covariogram model (which in fact was estimated) and an envelope (2.5 and 97.5 percent quantiles) based on simulating the covariogram for the model having parameters given in parms.R. 9. parmR <- list(cov.model = "powered.exponential", kappa = 0.843,
cov.pars = c(0.31,6702/61.9), beta = 1.836)

10. class(parmR) <- "covariomodel" 11. konvol <- covariog.model.env(rongelap,


model.pars = parmR)

obj.covariog = covar,

12. plot(covar, envelope.obj = konvol) 13. lines(parmR, max.dist = 800, lty =

1)

14. 15. 16. 17. 18. ------------- last change: 24 March 2003.

1. A short demonstration shown at my Ph.D. viva is found here. 19. The following short demonstation was presented at my Ph.D. viva in Aalborg, December 2001. [a few minor changes have been made since then, to accomodate changes in R, geoR and geoRglm]. 20. WARNING: RUNNING THIS IS VERY TIME-CONSUMING > ## > ## 1. Simulating data > ## > sim <- grf(grid = expand.grid(x = seq(1.1, 7.1, l = 7), y = seq(1.1, 7.1, l = 7)), cov.pars = c(0.1, 2)) grf: process with 1 covariance structure(s) grf: nugget effect is: tausq= 0 grf: covariance model 1 is: exponential(sigmasq=0.1, phi=2) grf: decomposition algorithm used is: cholesky grf: simulation(s) on a grid provided by the user

grf: End of simulation procedure. Number of realizations: 1 > sim$units.m <- rep(4,49) > sim$data <- rbinom(49, size = rep(4,49), prob = exp(sim$data)/(1+exp(sim$data))) > > ## 2. Visualising the data > ## > plot(sim$coords, type = "n") > text(sim$coords[,1], sim$coords[,2], format(sim$data), cex=1.5)

21. 22. > ## 3. Setting input options > > sim.pr <- prior.glm.control(phi.discrete = seq(0.05, 3, l=60)) > sim.mcmc <- mcmc.control(S.scale = 0.1, phi.scale = 0.04, burn.in=10000) > grid <- expand.grid(x = seq(1, 7, l = 51), y = seq(1, 7, l = 51)) > > run.sim <- binom.krige.bayes(sim, locations = grid, prior = sim.pr, mcmc.input = sim.mcmc) binom.krige.bayes: analysis assuming constant mean burn-in = 10000 is finished; Acc.-rate = 0.67 ; Acc-rate-phi = 0.72 iter. numb. 11000 ; Acc.-rate = 0.77 ; Acc-rate-phi = 0.69

iter. numb. 12000 ; Acc.-rate = 0.69 ; Acc-rate-phi = 0.67 iter. numb. 13000 ; Acc.-rate = 0.71 ; Acc-rate-phi = 0.65 iter. numb. 14000 ; Acc.-rate = 0.24 ; Acc-rate-phi = 0.59 iter. numb. 15000 ; Acc.-rate = 0.67 ; Acc-rate-phi = 0.70 iter. numb. 16000 ; Acc.-rate = 0.78 ; Acc-rate-phi = 0.76 iter. numb. 17000 ; Acc.-rate = 0.75 ; Acc-rate-phi = 0.73 iter. numb. 18000 ; Acc.-rate = 0.71 ; Acc-rate-phi = 0.67 iter. numb. 19000 ; Acc.-rate = 0.71 ; Acc-rate-phi = 0.67 iter. numb. 20000 ; Acc.-rate = 0.67 ; Acc-rate-phi = 0.64 MCMC performed: n.iter. = 10000 ; thinning = 10 ; burn.in = 10000 binom.krige.bayes: Prediction performed binom.krige.bayes: done! > > ## inspecting output > names(run.sim) [1] "posterior" "predictive" "model" "prior" "mcmc.input" [6] ".Random.seed" "call" > names(run.sim$posterior) [1] "phi" "beta" "sigmasq" "simulations" > names(run.sim$posterior$phi) [1] "sample" "mean" "var" > > par(mfrow=c(2,2), mar=c(2.3,2.5,.5,.7), mgp=c(1.5,.6,0), cex=0.6) > plot(run.sim$posterior$phi$sample,type="l") > acf(run.sim$posterior$phi$sample) > plot(run.sim$posterior$sim[1,],type="l") > acf(run.sim$posterior$sim[1,])

23. 24. > ## Predictions > ## > > names(run.sim$predictive)

[1] "simulations" "median" "uncertainty" "quantiles" > > ## > ## Plotting predictions > ## > image.kriging(locations = grid, values = run.sim$pred$median ) 25.

1. The commands from the example in Diggle, Ribeiro Jr and Christensen (2003) [bookchapter], and Christensen and Ribeiro Jr (2002) [R-news] are found here. 26. The commands for the example in Diggle, Ribeiro Jr and Christensen (2003) [bookchapter], and Christensen and Ribeiro Jr (2002) [R-news]. 27. WARNING: RUNNING THIS IS VERY TIME-CONSUMING AND MEMORY-DEMANDING ## Simulating data sim <- grf(grid = expand.grid(x = seq(0.0555, 0.944444, l = 8), y = seq(0.0555, 0.944444, l = 8)), cov.pars = c(0.5, 0.2)) attr(sim,"class") <- "geodata"

sim$units.m <- rep(4,64) sim$prob <- exp(sim$data)/(1+exp(sim$data)) sim$data <- rbinom(64, size = sim$units.m, prob = sim$prob) ## Visualising the data and the (unobserved) random effects par(mfrow = c(1,2), mar=c(2.3,2.5,.5,.7), mgp=c(1.5,.6,0), cex=0.6) plot(c(0, 1), c(-0.1, 1), type="n", xlab="Coordinate X", ylab="Coordinate Y") text(sim$coords[,1], sim$coords[,2], format(round(sim$prob,digits=2)),cex=0.9) plot(c(0, 1), c(-0.1, 1), type="n", xlab="Coordinate X", ylab="Coordinate Y") text(sim$coords[,1], sim$coords[,2], format(sim$data),cex=1.1) points(sim$coords[c(1,29),],cex=5.5) ## Setting input options and running the function prior.sim <- prior.glm.control(beta.prior = "normal", beta = 0, beta.var = 1, phi.prior = "exponential", phi = 0.2, 28. phi.discrete = seq(0.005,0.3, l=60), sigmasq.prior = "sc.inv.chisq", df.sigmasq = 5, sigmasq = 0.5) mcmc.sim <- mcmc.control(S.scale = 0.05, phi.scale = 0.015, thin = 100, burn.in = 10000) pred.grid <- expand.grid(x = seq(0.0125, 0.9875, l = 40), y = seq(0.0125, 0.9875, l = 40)) out.sim <- output.glm.control(sim.predict = TRUE) run.sim <- binom.krige.bayes(sim, locations = pred.grid, prior = prior.sim, mcmc.input = mcmc.sim, output=out.sim) ## Autocorrelations par(mfrow=c(3,2), mar=c(2.3,2.5,.5,.7), mgp=c(1.5,.6,0), cex=0.6) plot(run.sim$posterior$sim[1,],type="l", ylab = "S(0.056, 0.056)") acf(run.sim$posterior$sim[1,], main ="") plot(run.sim$posterior$sim[29,],type="l", ylab = "S(0.563, 0.436)") acf(run.sim$posterior$sim[29,], main ="") plot(run.sim$posterior$phi.s,type="l", ylab = "phi") acf(run.sim$posterior$phi.s, main ="") ## Plot of timeseries par(mfrow=c(3,1), mar=c(2.3,2.5,.5,.7), mgp=c(1.5,.6,0), cex=0.6) plot(run.sim$posterior$sim[1,],type="l", ylab = "S(0.056, 0.056)") plot(run.sim$posterior$sim[29,],type="l", ylab = "S(0.563, 0.436)")

plot(run.sim$posterior$phi.s,type="l", ylab = "phi") ## Predictions sim.predict <- apply(run.sim$pred$sim,1,mean) sim.predict.var <- apply(run.sim$pred$sim,1,var) par(mfrow=c(1,2), mar=c(2.3,2.5,.5,.7), mgp=c(1.5,.6,0), cex=0.6) image.kriging(x= sim.predict, locations = pred.grid,values = sim.predict, col=gray(seq(1,0,l=30)), 29. x.leg=c(0.1,0.9), y.leg = c(-0.12,-0.07), cex.leg=0.7, xlab="Coordinate X", ylab="Coordinate Y") image.kriging(x= sim.predict.var, locations = pred.grid,values = sim.predict.var, col=gray(seq(1,0,l=30)), 30. x.leg=c(0.1,0.9), y.leg = c(-0.12,-0.07), cex.leg=0.7, xlab="Coordinate X", ylab="Coordinate Y")
The commands for the analysis in Christensen (2004) [JCGS] for MCMC- ## ## 03-01-2005 ## ## This page contains the commands used for the article ## Ole F. Christensen (2004) ``Monte Carlo maximum likelihood in modelbased geostatistics'' ## Journal of computational and graphical statistics 13, 702-718. library(geoR) library(geoRglm) data(rongelap) MCmle.input.fixed <- glsm.mcmc(rongelap, model=list(family="poisson", link = "boxcox", cov.pars=c(2.40, 340), beta = 6.2, nugget = 2.075*2.40, lambda=1), mcmc.input = mcmc.control(S.scale = 0.5,thin=20,burn.in=10000)) ## investigating mixing and convergence library(coda) MCmle.coda <- create.mcmc.coda(MCmle.input.fixed, mcmc.input = list(thin = 20, burn.in=10000)) plot(MCmle.coda) autocorr.plot(MCmle.coda) ## maximum likelihood mcmcobj <- prepare.likfit.glsm(MCmle.input.fixed) lik.boxcox.1.expon <- likfit.glsm(mcmcobj, ini.phi=10, fix.nugget.rel=TRUE) lik.boxcox.1.expon.nugget <- likfit.glsm(mcmcobj, ini.phi=100)

lik.boxcox.1.nospatial <- likfit.glsm(mcmcobj, fix.nugget.rel=TRUE,cov.model="pure.nugget") lik.boxcox.1.matern1.nugget <- likfit.glsm(mcmcobj, ini.phi=40,cov.model="matern",kappa=1) lik.boxcox.1.spherical.nugget <- likfit.glsm(mcmcobj, ini.phi=400,cov.model="spherical", nugget.rel=2) ## investigating other link functions mcmcobj.mu <- prepare.likfit.glsm(MCmle.input.fixed, use.intensity = TRUE) lik.expon.boxcox1.0 <- likfit.glsm(mcmcobj.mu, ini.phi=150.9130915, nugget.rel=0.1266416,lambda = 0) lik.gaussian.boxcox1.0 <- likfit.glsm(mcmcobj.mu, cov.model="gaussian", ini.phi=100, nugget.rel=0.13,lambda = 0) lik.expon.boxcox1.05 <- likfit.glsm(mcmcobj.mu, ini.phi=196, nugget.rel=0.787,lambda = 0.5) lik.expon.boxcox1.lambda <- likfit.glsm(mcmcobj.mu, ini.phi=339.6, nugget.rel=2.075,lambda = 1,fix.lambda=FALSE) ## profile likelihood for (phi,tausq.rel) [two-dimensional figure]. pr.lik.rongelap <- proflik.glsm(mcmcobj, lik.boxcox.1.expon.nugget, phi.values = seq(8,40)*20, nugget.rel.values=seq(1,3,l=21)) par(cex=0.9) plot(pr.lik.rongelap, levels = seq(-5, 1, by=0.1), labcex=0.55) dev.off() # Figure 1: postscript("profile.phitausq.ps", height = 5, width = 6.5, horizontal=FALSE) par(cex=0.9) plot(pr.lik.rongelap, levels = seq(-5, 1, by=0.1), labcex=0.55) dev.off()

############ grid for prediction being defined: ## Paulo har fjernet afhaengigheden af splancs. ## library(splancs) ngx <- 126 ngy <- 75 temp.grid <- polygrid(seq(-6250,0,l=ngx),seq(-3600,100,l=ngy), borders=rongelap$borders, vec.inout=TRUE) ng <- ngx * ngy grid <- temp.grid$xypoly grid.ind <- temp.grid$vec.inout rm(temp.grid) ## prediction :

emp.mean <- apply(MCmle.input.fixed$sim,1,mean) emp.var <- cov(t(MCmle.input.fixed$sim)) nug.value <lik.boxcox.1.expon.nugget$nugget.rel*lik.boxcox.1.expon.nugget$cov.pars[1 ] resultat <- krige.conv(data=emp.mean-lik.boxcox.1.expon.nugget$beta, coords=rongelap$coords, locations = grid, krige=krige.control(type.krige = "sk", beta=0, cov.pars=lik.boxcox.1.expon.nugget$cov.pars, nugget = nug.value)) rongelap.mean <- resultat$predict + lik.boxcox.1.expon.nugget$beta + 1 d0 <- loccoords(coords = rongelap$coords, locations = grid) v0 <- ifelse(d0 > 1e-10, cov.spatial(obj = d0, cov.pars = lik.boxcox.1.expon.nugget$cov.pars), lik.boxcox.1.expon.nugget$cov.pars[1] + nug.value) invcov <- varcov.spatial(coords = rongelap$coords, nugget = nug.value, cov.pars = lik.boxcox.1.expon.nugget$cov.pars, inv = TRUE) AA <- t(v0)%*%invcov$inverse rongelap.var <- diag(AA%*%emp.var%*%t(AA))+ resultat$krige.var #### Figure 2 : postscript("predict.ps", height=9.0, width=7.15, horizontal=FALSE) par(mfrow=c(2,1), mar=c(3,3,1,.75), mgp=c(2,.6,0), cex=0.8) grid.mat <- rep(NA,ng) grid.mat[grid.ind == TRUE] <- rongelap.mean plot(cbind(c(-6200, - 100), c(-3500, 50)), type="n", xlab="Coordinate X (m)", ylab="Coordinate Y (m)") image.kriging(locations = expand.grid(seq(-6250, 0,l=ngx),seq(-3600, 100,l=ngy)), values = grid.mat, col=gray(seq(1,0,l=30)), x.leg=c(-6000,-3000), y.leg = c(-700,-300), cex.leg=0.7, add = TRUE, zlim=c(3,13)) lines(rongelap$borders, lwd=2.5) grid.mat[grid.ind == TRUE] <-sqrt(rongelap.var) plot(cbind(c(-6200, -100), c(-3500, 50)), type="n", xlab="Coordinate X (m)", ylab="Coordinate Y (m)") image.kriging(locations = expand.grid(seq(-6250, 0,l=ngx),seq(-3600, 100,l=ngy)), values = grid.mat, col=gray(seq(1,0,l=30)), x.leg=c(-6000,-3000), y.leg = c(-700,-300), cex.leg=0.7, add = TRUE, zlim=c(0,5)) lines(rongelap$borders, lwd=2.5) dev.off() ## the prediction uncertainties for data locations : sqrt(rongelap.var)[sqrt(rongelap.var)<1] summary(sqrt(rongelap.var)[sqrt(rongelap.var)<1]) ## the prediction uncertainties for other locations : sqrt(rongelap.var)[sqrt(rongelap.var)>1] summary(sqrt(rongelap.var)[sqrt(rongelap.var)>1])

### prediction when assuming that tausq is measurement error resultat2 <- krige.conv(data=emp.mean-lik.boxcox.1.expon.nugget$beta, coords=rongelap$coords, locations = grid, krige=krige.control(type.krige = "sk",beta=0, cov.pars=lik.boxcox.1.expon.nugget$cov.pars, nugget = nug.value),output=output.control(signal=TRUE)) rongelap.mean2 <- resultat2$predict + lik.boxcox.1.expon.nugget$beta + 1 v0.2 <- cov.spatial(obj = d0, cov.pars = lik.boxcox.1.expon.nugget$cov.pars) AA.2 <- t(v0.2)%*%invcov$inverse rongelap.var2 <- diag(AA.2%*%emp.var%*%t(AA.2))+ resultat2$krige.var #### Figure 3 : postscript("predict2.ps", height=9.0, width=7.15, horizontal=FALSE) par(mfrow=c(2,1), mar=c(3,3,1,.75), mgp=c(2,.6,0), cex=0.8) grid.mat <- rep(NA,ng) grid.mat[grid.ind == TRUE] <- rongelap.mean2 plot(cbind(c(-6200, - 100), c(-3500, 50)), type="n", xlab="Coordinate X (m)", ylab="Coordinate Y (m)") image.kriging(locations = expand.grid(seq(-6250, 0,l=ngx),seq(-3600, 100,l=ngy)), values = grid.mat, col=gray(seq(1,0,l=30)), x.leg=c(-6000,-3000), y.leg = c(-700,-300), cex.leg=0.7, add = TRUE, zlim=c(3,13)) lines(rongelap$borders, lwd=2.5) grid.mat[grid.ind == TRUE] <-sqrt(rongelap.var2) plot(cbind(c(-6200, -100), c(-3500, 50)), type="n", xlab="Coordinate X (m)", ylab="Coordinate Y (m)") image.kriging(locations = expand.grid(seq(-6250, 0,l=ngx),seq(-3600, 100,l=ngy)), values = grid.mat, col=gray(seq(1,0,l=30)), x.leg=c(-6000,-3000), y.leg = c(-700,-300), cex.leg=0.7, add = TRUE, zlim=c(0,5)) lines(rongelap$borders, lwd=2.5) dev.off()

## the prediction uncertainties for data locations : sqrt(rongelap.var2)[sqrt(rongelap.var)<1] summary(sqrt(rongelap.var2)[sqrt(rongelap.var)<1]) ## the prediction uncertainties for other locations : sqrt(rongelap.var2)[sqrt(rongelap.var)>1] summary(sqrt(rongelap.var2)[sqrt(rongelap.var)>1]) 2. MLE are found here.

3. The commands for the example in Diggle, Ribeiro Jr and Christensen

(2003) [bookchapter], and Christensen and Ribeiro Jr (2002) [R-news]. 4. WARNING: RUNNING THIS IS VERY TIME-CONSUMING AND MEMORY-DEMANDING

## Simulating data sim <- grf(grid = expand.grid(x = seq(0.0555, 0.944444, l = 8), y = seq(0.0555, 0.944444, l = 8)), cov.pars = c(0.5, 0.2)) attr(sim,"class") <- "geodata" sim$units.m <- rep(4,64) sim$prob <- exp(sim$data)/(1+exp(sim$data)) sim$data <- rbinom(64, size = sim$units.m, prob = sim$prob) ## Visualising the data and the (unobserved) random effects par(mfrow = c(1,2), mar=c(2.3,2.5,.5,.7), mgp=c(1.5,.6,0), cex=0.6) plot(c(0, 1), c(-0.1, 1), type="n", xlab="Coordinate X", ylab="Coordinate Y") text(sim$coords[,1], sim$coords[,2], format(round(sim$prob,digits=2)),cex=0.9) plot(c(0, 1), c(-0.1, 1), type="n", xlab="Coordinate X", ylab="Coordinate Y") text(sim$coords[,1], sim$coords[,2], format(sim$data),cex=1.1) points(sim$coords[c(1,29),],cex=5.5) ## Setting input options and running the function prior.sim <- prior.glm.control(beta.prior = "normal", beta = 0, beta.var = 1, phi.prior = "exponential", phi = 0.2, 5. phi.discrete = seq(0.005,0.3, l=60), sigmasq.prior = "sc.inv.chisq", df.sigmasq = 5, sigmasq = 0.5) mcmc.sim <- mcmc.control(S.scale = 0.05, phi.scale = 0.015, thin = 100, burn.in = 10000) pred.grid <- expand.grid(x = seq(0.0125, 0.9875, l = 40), y = seq(0.0125, 0.9875, l = 40)) out.sim <- output.glm.control(sim.predict = TRUE) run.sim <- binom.krige.bayes(sim, locations = pred.grid, prior = prior.sim, mcmc.input = mcmc.sim, output=out.sim) ## Autocorrelations par(mfrow=c(3,2), mar=c(2.3,2.5,.5,.7), mgp=c(1.5,.6,0), cex=0.6) plot(run.sim$posterior$sim[1,],type="l", ylab = "S(0.056, 0.056)") acf(run.sim$posterior$sim[1,], main ="") plot(run.sim$posterior$sim[29,],type="l", ylab = "S(0.563, 0.436)") acf(run.sim$posterior$sim[29,], main ="") plot(run.sim$posterior$phi.s,type="l", ylab = "phi") acf(run.sim$posterior$phi.s, main ="")

## Plot of timeseries par(mfrow=c(3,1), mar=c(2.3,2.5,.5,.7), mgp=c(1.5,.6,0), cex=0.6) plot(run.sim$posterior$sim[1,],type="l", ylab = "S(0.056, 0.056)") plot(run.sim$posterior$sim[29,],type="l", ylab = "S(0.563, 0.436)") plot(run.sim$posterior$phi.s,type="l", ylab = "phi") ## Predictions sim.predict <- apply(run.sim$pred$sim,1,mean) sim.predict.var <- apply(run.sim$pred$sim,1,var) par(mfrow=c(1,2), mar=c(2.3,2.5,.5,.7), mgp=c(1.5,.6,0), cex=0.6) image.kriging(x= sim.predict, locations = pred.grid,values = sim.predict, col=gray(seq(1,0,l=30)), 6. x.leg=c(0.1,0.9), y.leg = c(-0.12,-0.07), cex.leg=0.7, xlab="Coordinate X", ylab="Coordinate Y") image.kriging(x= sim.predict.var, locations = pred.grid,values = sim.predict.var, col=gray(seq(1,0,l=30)), 7. x.leg=c(0.1,0.9), y.leg = c(-0.12,-0.07), cex.leg=0.7, xlab="Coordinate X", ylab="Coordinate Y")
8. The commands for the example in Diggle, Ribeiro Jr and Christensen (2003) [bookchapter], and Christensen and Ribeiro Jr (2002) [R-news] are found here.

9. he empirical covariogram in Christensen, Mller and Waagepetersen

(2000) for the Poisson-log normal model can be calculated using the function covariog. 10. covar <- covariog(rongelap, uvec = c((1:20)*40)) 11. Theoretical and empirical variograms can be plotted and visually compared. For example, the figure below shows the estimated covariogram, a theoretical covariogram model (which in fact was estimated) and an envelope (2.5 and 97.5 percent quantiles) based on simulating the covariogram for the model having parameters given in parms.R. 12. parmR <- list(cov.model = "powered.exponential", kappa = 0.843,
cov.pars = c(0.31,6702/61.9), beta = 1.836)

13. class(parmR) <- "covariomodel" 14. konvol <- covariog.model.env(rongelap,


model.pars = parmR)

obj.covariog = covar,

15. plot(covar, envelope.obj = konvol) 16. lines(parmR, max.dist = 800, lty =

1)

17.
18. The empirical covariogram derived in Christensen, Moller and Waagepetersen (2000) for the Poisson-log normal model. See here.

19. A short demonstration shown at my Ph.D. viva is found here.

20. The following short demonstation was presented at my Ph.D. viva in

Aalborg, December 2001. [a few minor changes have been made since then, to accomodate changes in R, geoR and geoRglm]. 21. WARNING: RUNNING THIS IS VERY TIME-CONSUMING > ## > ## 1. Simulating data > ## > sim <- grf(grid = expand.grid(x = seq(1.1, 7.1, l = 7), y = seq(1.1, 7.1, l = 7)), cov.pars = c(0.1, 2)) grf: process with 1 covariance structure(s) grf: nugget effect is: tausq= 0 grf: covariance model 1 is: exponential(sigmasq=0.1, phi=2) grf: decomposition algorithm used is: cholesky grf: simulation(s) on a grid provided by the user grf: End of simulation procedure. Number of realizations: 1 > sim$units.m <- rep(4,49)

> sim$data <- rbinom(49, size = rep(4,49), prob = exp(sim$data)/(1+exp(sim$data))) > > ## 2. Visualising the data > ## > plot(sim$coords, type = "n") > text(sim$coords[,1], sim$coords[,2], format(sim$data), cex=1.5)

22. 23. > ## 3. Setting input options > > sim.pr <- prior.glm.control(phi.discrete = seq(0.05, 3, l=60)) > sim.mcmc <- mcmc.control(S.scale = 0.1, phi.scale = 0.04, burn.in=10000) > grid <- expand.grid(x = seq(1, 7, l = 51), y = seq(1, 7, l = 51)) > > run.sim <- binom.krige.bayes(sim, locations = grid, prior = sim.pr, mcmc.input = sim.mcmc) binom.krige.bayes: analysis assuming constant mean burn-in = 10000 is finished; Acc.-rate = 0.67 ; Acc-rate-phi = 0.72 iter. numb. 11000 ; Acc.-rate = 0.77 ; Acc-rate-phi = 0.69 iter. numb. 12000 ; Acc.-rate = 0.69 ; Acc-rate-phi = 0.67 iter. numb. 13000 ; Acc.-rate = 0.71 ; Acc-rate-phi = 0.65

iter. numb. 14000 ; Acc.-rate = 0.24 ; Acc-rate-phi = 0.59 iter. numb. 15000 ; Acc.-rate = 0.67 ; Acc-rate-phi = 0.70 iter. numb. 16000 ; Acc.-rate = 0.78 ; Acc-rate-phi = 0.76 iter. numb. 17000 ; Acc.-rate = 0.75 ; Acc-rate-phi = 0.73 iter. numb. 18000 ; Acc.-rate = 0.71 ; Acc-rate-phi = 0.67 iter. numb. 19000 ; Acc.-rate = 0.71 ; Acc-rate-phi = 0.67 iter. numb. 20000 ; Acc.-rate = 0.67 ; Acc-rate-phi = 0.64 MCMC performed: n.iter. = 10000 ; thinning = 10 ; burn.in = 10000 binom.krige.bayes: Prediction performed binom.krige.bayes: done! > > ## inspecting output > names(run.sim) [1] "posterior" "predictive" "model" "prior" "mcmc.input" [6] ".Random.seed" "call" > names(run.sim$posterior) [1] "phi" "beta" "sigmasq" "simulations" > names(run.sim$posterior$phi) [1] "sample" "mean" "var" > > par(mfrow=c(2,2), mar=c(2.3,2.5,.5,.7), mgp=c(1.5,.6,0), cex=0.6) > plot(run.sim$posterior$phi$sample,type="l") > acf(run.sim$posterior$phi$sample) > plot(run.sim$posterior$sim[1,],type="l") > acf(run.sim$posterior$sim[1,])

24. 25. > ## Predictions > ##

> > names(run.sim$predictive) [1] "simulations" "median" "uncertainty" "quantiles" > > ## > ## Plotting predictions > ## > image.kriging(locations = grid, values = run.sim$pred$median )

26.

1. Simulating stochastic proccesses: simulation routines to illustrate basic concepts of stochastic proccesses.
2461. 2462. 2463. 2464. 2465. 2466. 2467. 2468. ## Exemplo 1: Processo de 0 e 1's n <- 100 p <- 7/8 u <- numeric(n) u[1] <- rbinom(1,1,.5) #for(i in 2:n) # u[i] <- ifelse(sum(rbinom(3,1,.5))==3,1-u[i-1],u[i-1])

2469. 2470. 2471. 2472. 2473. 2474. 2475. 2476. 2477. 2478. 2479. 2480. 2481. 2482. 2483. 2484. 2485. 2486. 2487. 2488. 2489. 2490. 2491. 2492. 2493. 2494. 2495. 2496. 2497. 2498. 2499. 2500. 2501. 2502. 2503. 2504. 2505. 2506. 2507. 2508. 2509. 2510. 2511. 2512. 2513. 2514. 2515. 2516. 2517. 2518. 2519. 2520. 2521. 2522. 2523. 2524. 2525.

#for(i in 2:n) # u[i] <- ifelse(rbinom(1,1,1-p),1-u[i-1],u[i-1]) for(i in 2:n) u[i] <- ifelse(runif(1)<1-p,1-u[i-1],u[i-1]) u plot(u, pch=19) pe01 <- function(p, n=100){ u <- numeric(n) u[1] <- rbinom(1,1,.5) for(i in 2:n) u[i] <- ifelse(runif(1)<1-p,1-u[i-1],u[i-1]) return(u) } sim1 <- pe01(7/8) plot(sim1, pch=19) x11() par(mfrow=c(3,1)) plot(pe01(7/8), pch=19) plot(pe01(3/4), pch=19) plot(pe01(1/2), pch=19) ## Exemplo 2: Processo gerando uma varivel contnua pe02 <- function(ro, n=100){ u <- numeric(n) u[1] <- rnorm(1) for(i in 2:n) u[i] <- ro * u[i-1] + rnorm(1) return(u) } sim1 <- pe02(0.5) plot(sim1, pch=19, ty="o") par(mar=c(3.5,3.5,0,0), mgp=c(2,1,0)) par(mfrow=c(3,1)) for(i in 1:3) plot(pe02(0.5), pch=19, ty="o") x11() par(mar=c(3.5,3.5,0,0), mgp=c(2,1,0)) par(mfrow=c(3,1)) for(i in 1:3) plot(pe02(0.9), pch=19, ty="o", ylim=c(-4,4)) x11() par(mar=c(3.5,3.5,0,0), mgp=c(2,1,0)) par(mfrow=c(3,1)) for(i in 1:3) plot(pe02(0.2), pch=19, ty="o", ylim=c(-4,4)) x11() par(mar=c(3.5,3.5,0,0), mgp=c(2,1,0)) par(mfrow=c(3,1)) for(i in 1:3) plot(pe02(0), pch=19, ty="o", ylim=c(-4,4))

2526. 2527. 2528. 2529. 2530. 2531. 2532. 2533. 2534. 2535. 2536. 2537. 2538. 2539. 2540. 2541. 2542. 2543. 2544. 2545. 2546. 2547. 2548. 2549. 2550. 2551. 2552. 2553. 2554. 2555. 2556. 2557. 2558. 2559. 2560. 2561. 2562. 2563. 2564. 2565. 2566. 2567. 2568. 2569. 2570. 2571. 2572.

x11() par(mar=c(3.5,3.5,0,0), mgp=c(2,1,0)) par(mfrow=c(3,1)) plot(pe02(0.1), pch=19, ty="o", ylim=c(-4,4)) plot(pe02(0.5), pch=19, ty="o", ylim=c(-4,4)) plot(pe02(0.9), pch=19, ty="o", ylim=c(-4,4)) ## Exemplo 3: processo com mais de 1 parmetro pe03 <- function(ro, variancia=1, n=100){ u <- numeric(n) dp <- sqrt(variancia) u[1] <- rnorm(1, sd=dp) for(i in 2:n) u[i] <- ro * u[i-1] + rnorm(1, sd=dp) return(u) } sim1 <sim2 <sim3 <limites pe03(0.5) pe03(0.5, var=2) pe03(0.5, var=4) <- range(c(sim1,sim2,sim3))

par(mar=c(3.5,3.5,0,0), mgp=c(2,1,0)) par(mfrow=c(3,1)) plot(sim1, ty="o") plot(sim2, ty="o") plot(sim3, ty="o") x11() par(mar=c(3.5,3.5,0,0), mgp=c(2,1,0)) par(mfrow=c(3,1)) plot(sim1, ty="o", ylim=limites) plot(sim2, ty="o", ylim=limites) plot(sim3, ty="o", ylim=limites) # controlando a semente x11() par(mar=c(3.5,3.5,0,0), par(mfrow=c(3,1)) semente <- .Random.seed plot(pe02(0.1), pch=19, .Random.seed <- semente plot(pe02(0.5), pch=19, .Random.seed <- semente plot(pe02(0.9), pch=19, do gerador de num. aleat. mgp=c(2,1,0)) ty="o", ylim=c(-4,4)) ty="o", ylim=c(-4,4)) ty="o

paulojus AT ufpr.br Last modified: Tue Dec 16 12:03:26 BRST 2008

Вам также может понравиться