34
Chapter 4 - Spatial processes R packages and software Lecture notes Odd Kolbjørnsen and Geir Storvik February 13, 2017 STK4150 - Intro 1

Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Embed Size (px)

Citation preview

Page 1: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Chapter 4 - Spatial processes R packages andsoftware

Lecture notes

Odd Kolbjørnsen and Geir Storvik

February 13, 2017

STK4150 - Intro 1

Page 2: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Last time

General set up for ”Kriging type problems”

Introductory exampleKriging caseGeneral case

Change of support

Spatial moving average models

ConstructionCorrelation function

Non gaussian observations

Monte CarloLaplace approximationINLA

STK4150 - Intro 2

Page 3: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Today

To day computations

SoftwareComputer

RINLA

Next timel lattice models

STK4150 - Intro 3

Page 4: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Software

There is allways an existing software that ”does your job”

The challenge is to figure out how this works

Even if the software does not do excatly what you want maybe it isgood enough

Reasons not to make your own computer code

existing code is tested (less bugs)existing code is optimized (speed)often related to publicationseasier to get others to ”accept it”

Reasons to make your own computer code

understand the methodology betterimprove/develop exsiting methodologycombining with other techniquescompete with existing computer codebecause you have too much spare time

STK4150 - Intro 4

Page 5: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Software options

Wiki List of spatial analysis software

.../wiki/List_of_spatial_analysis_software/

LuciadLightspeed,Open GeoDa, CrimeStatc, SaTScan, SAGA,IDRISI, Biodiverse, ERDAS IMAGINE, TerraLens ++

ArcGIS: Geoprocessing, visualization(GIS = Geographic information system)

GeoBUGS: Bayesian analysis (WinBugs)

GeoDa: Explantory Spatial Anaysis +

STARS: Space-time

SAS/STAT: spatial analysis (limited)

SPLUS: SpatialStats

Matlab: Spatial-statistics toolbox (fast but limited)

GEOEAS, SGeMS, GSLIB ,COHIBA, CRAVA ++

R

STK4150 - Intro 5

Page 6: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Spatial analysis in R

CRAN (The Comprehensive R Archive Network)

Classes for spatial data: sp, spacetimeHandling spatial data: geosphereReading and writing spatial data: rgdalVisualisationPoint pattern analysisGeostatistics: geoR, gstat, spatialDisease mapping and areal data analysis: INLASpatial regression: nlmeEcological analysis

install.packages(...)

update.packages(...)

library(...)

STK4150 - Intro 6

Page 7: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Hierarcical and hierarcical Bayesian approach

Hierarchical model

Variable Densities Notation in bookData model: Z p(Z|Y,θ) [Z|Y,θ]Process model: Y p(Y|θ) [Y|θ]Parameter: θ

Simultaneous model: p(y, z|θ) = p(z|y,θ)p(y|θ)Marginal model: L(θ) = p(z|θ) =

∫y p(z, y|θ)dy

Inference: θ̂ = argmaxθL(θ)

Bayesian approach: Include model on θ

Variable Densities Notation in bookData model: Z p(Z|Y,θ) [Z|Y,θ]Process model: Y p(Y|θ) [Y|θ]Parameter model: θ p(θ) [θ]

Simultaneous model: p(y, z,θ)Marginal model: p(z) =

∫θ∫

y p(z, y|θ)dydθ

Inference: θ̂ =∫θ θp(θ|z)dθ

STK4150 - Intro 7

Page 8: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

INLA

INLA = Integrated nested Laplace approximation (software)C-code, R-interfaceCan be installed by

source("http://www.math.ntnu.no/inla/givemeINLA.R")

Both empirical Bayes and Bayes

STK4150 - Intro 8

Page 9: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Full presentation on the course web page

STK4150 - Intro 9

Page 10: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

STK4150 - Intro 10

Page 11: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

STK4150 - Intro 11

Page 12: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

STK4150 - Intro 12

Page 13: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

STK4150 - Intro 13

Page 14: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Example - simulated data

Assume

Y (s) =β0 + β1x(s) + σδδ(s), {δ(s)} matern correlation

Z (si ) =Y (si ) + σεε(si ), {ε(si )} independent

Simulations:

Simulation on 20× 30 grid

{x(s)} sinus curve in horizontal direction

Parametervalues (β0, β1) = (2, 0.3), σδ = 1, σε = 0.3 and(θ1, θ2) = (2, 3)

STK4150 - Intro 14

Page 15: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Latent models in INLA

STK4150 - Intro 15

Page 16: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Matern model - setup

Code on webpage

STK4150 - Intro 16

Page 17: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Matern model - setup

STK4150 - Intro 17

Page 18: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Process model mean and covariance

Mean: 20× 30 = 600, Covariance: 600× 600 = 360000

STK4150 - Intro 18

Page 19: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Matern model - parametrization

STK4150 - Intro 19

Page 20: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Matern model - parametrization

STK4150 - Intro 20

Page 21: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Output INLA

Empirical Bayes vs Bayes

STK4150 - Intro 21

Page 22: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Output INLA

Empirical Bayes

STK4150 - Intro 22

Page 23: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Matern model - parametrization

Model Range par Smoothness parBook ∝ {||h||/θ1}θ2Kθ2(||h||/θ1) θ1 θ2geoR ∝ {||h||/φ}κKκ(||h||/φ) φ κINLA ∝ {||h|| ∗ κ}νKν(||h|| ∗ κ) 1/κ ν

Note: INLA reports an estimate of another “range”, defined as

r =

√8

κ

corresponding to a distance where the covariance function isapproximately zero. An estimate of the range parameter can then beobtained by

1

κ=

r√8

Note: INLA works with precisions τy = σ−2y , τz = σ−2z .

STK4150 - Intro 23

Page 24: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Result - INLA - empirical Bayes

Parameter True value Estimate Run 1β1 2.0 1.7991β2 0.3 0.3385σz 0.3 1√

13.213= 0.275

σy 1.0 1√1.398

= 0.846

φ1 2.0 4.901√8

= 1.7328

STK4150 - Intro 24

Page 25: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Spatial prediction Run 1

Prior mean ”True” intensity Gaussian data

Empirical Bayes and Bayesian prediction

STK4150 - Intro 25

Page 26: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Result - INLA - empirical Bayes

Parameter True value Estimate Run 1 Estimate Run 2β1 2.0 1.799 1.9719β2 0.3 0.3385 0.4741σz 0.3 1√

13.213= 0.275 1√

13.6917= 0.2756

σy 1.0 1√1.398

= 0.846 1√0.0014

= 26.72*

φ1 2.0 4.901√8

= 1.7328 17.44√8

=6.16*

* Commonly seen variance vs range tradeoff.Large variance and long range vs

”on scale” variance and ”on scale” rangeAllways compare your estimates with data variance.

Here: V̂ar(Z ) = 1.03, theortically (Var(Z ) = 1.09 = 1.0 + 0.32 )This is an argument for a Bayesian approach with an informative prior

STK4150 - Intro 26

Page 27: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Spatial prediction Run 2

Prior mean ”True” intensity Gaussian data

Empirical Bayes and Bayesian prediction

STK4150 - Intro 27

Page 28: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Example - simulated data

Assume

Y (s) =β0 + β1x(s) + σδδ(s), {δ(s)} matern correlation

Z (si ) ∼Poisson(exp(Y (si ))

Simulations:

Simulation on 20× 30 grid

{x(s)} sinus curve in horizontal direction

Parametervalues (β0, β1) = (1, 0.3), σδ = 1, σε = 0.3 and(θ1, θ2) = (2, 3)

STK4150 - Intro 28

Page 29: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Spatial prediction, Code Poisson

Empirical Bayes (note constant term changed from 2 to 1, hence y-1 )

STK4150 - Intro 29

Page 30: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Parameter prediction, Poisson

STK4150 - Intro 30

Page 31: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Spatial prediction, Poisson

”True” intensity Poisson data (counts)

Estimated intensity

STK4150 - Intro 31

Page 32: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Inla engine: GMRFLib

Principle in GRMF = Gaussian Markov Random field is to invert therelation in ”smoothing of independent Gaussian variables”

Y = Lε, ε ∼ N(0, I)⇒ Y ∼ N(0,Σ = LLT )

Select the smooting kernel to be the cholesky factor: Y = Lε

Then flip the equation and use the pressision matrix Q = Σ−1:

L−1Y = ε, ε ∼ N(0, I)⇒ Y ∼ N(0,Q = L−1L−T )

The formulations are equivalent but L and L−1 differs greately. This hasmajor impact on computations.

STK4150 - Intro 32

Page 33: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Inla engine: GMRFLib

Principle in GRMF = Gaussian Markov Random field is to invert therelation in ”smoothing of independent Gaussian variables”

Y = Lε, ε ∼ N(0, I)⇒ Y ∼ N(0,Σ = LLT )

Select the smooting kernel to be the cholesky factor: Y = Lε

Then flip the equation and use the pressision matrix Q = Σ−1:

L−1Y = ε, ε ∼ N(0, I)⇒ Y ∼ N(0,Q = L−1L−T )

The formulations are equivalent but L and L−1 differs greately. This hasmajor impact on computations.

STK4150 - Intro 33

Page 34: Chapter 4 - Spatial processes R packages and software ... · PDF fileSAS/STAT: spatial analysis ... Spatial prediction, Code Poisson ... Spatial processes R packages and software Lecture

Choleskey versus inverse choleskey in AR(1)

Y1 = σ1ε1, Yi = αYi−1 + σεi

”Inverse formulation”:

ε1 =1

σ1Y1, εi =

1

σYi −

α

σYi−1, i > 1

”Direct formulation”:

Yi = αi−1σ1ε1 + σ

i∑j=2

αi−jεj

The ”Inverse formulation” can model long range dependency with asparse matrix (few non-zero elements).

Complexity inverse formulation, – solving L−1Y = ε: ∼ n

Complexity direct formulation, – multiply Y = Lε: ∼ n2

STK4150 - Intro 34