Computer exercise INLA

Similar documents
Spatial modelling using INLA

Journal of Statistical Software

Spacetime models in R-INLA. Elias T. Krainski

Statistics in Environmental Research (BUC Workshop Series) IV Problem sheet 1

Chapter 4 - Fundamentals of spatial processes Lecture notes

Chapter 4 - Spatial processes R packages and software Lecture notes

R-INLA. Sam Clifford Su-Yun Kang Jeff Hsieh. 30 August Bayesian Research and Analysis Group 1 / 14

A short introduction to INLA and R-INLA

Bayesian computation using INLA. 5: Spatial Markovian models The SPDE approach

Gaussian Processes 1. Schedule

INLA for Spatial Statistics

Machine learning: Hypothesis testing. Anders Hildeman

Data are collected along transect lines, with dense data along. Spatial modelling using GMRFs. 200m. Today? Spatial modelling using GMRFs

Spatial Statistics with Image Analysis. Outline. A Statistical Approach. Johan Lindström 1. Lund October 6, 2016

Computation fundamentals of discrete GMRF representations of continuous domain spatial models

How to solve the stochastic partial differential equation that gives a Matérn random field using the finite element method

Lecture 9: Predictive Inference

Multi-resolution models for large data sets

Lecture 9: Predictive Inference for the Simple Linear Model

Spatial Statistics with Image Analysis. Lecture L08. Computer exercise 3. Lecture 8. Johan Lindström. November 25, 2016

Generalized Linear Models in R

A tutorial in spatial and spatio-temporal models with R-INLA

Bayesian Inference for Regression Parameters

Package horseshoe. November 8, 2016

2015 SISG Bayesian Statistics for Genetics R Notes: Generalized Linear Modeling

On Gaussian Process Models for High-Dimensional Geostatistical Datasets

Explore the data. Anja Bråthen Kristoffersen

-A wild house sparrow population case study

Lecture: Gaussian Process Regression. STAT 6474 Instructor: Hongxiao Zhu

Statistical Learning

1 Bayesian Linear Regression (BLR)

Variance. Standard deviation VAR = = value. Unbiased SD = SD = 10/23/2011. Functional Connectivity Correlation and Regression.

Lecture 19. Spatial GLM + Point Reference Spatial Data. Colin Rundel 04/03/2017

Fast algorithms that utilise the sparsity of Q exist (c-package ( Q 1. GMRFlib) Seattle February 3, 2009

Bayesian Gaussian Process Regression

Explore the data. Anja Bråthen Kristoffersen Biomedical Research Group

0.1 gamma.mixed: Mixed effects gamma regression

A general mixed model approach for spatio-temporal regression data

Lecture 2: Poisson point processes: properties and statistical inference

Markov random fields. The Markov property

Spatial smoothing over complex domain

Big Data in Environmental Research

Bayesian Dynamic Modeling for Space-time Data in R

Introduction to Bayes and non-bayes spatial statistics

WinBUGS : part 2. Bruno Boulanger Jonathan Jaeger Astrid Jullion Philippe Lambert. Gabriele, living with rheumatoid arthritis

Pattern Recognition and Machine Learning

Fast approximations for the Expected Value of Partial Perfect Information using R-INLA

Centre for Biodiversity Dynamics, Department of Biology, NTNU, NO-7491 Trondheim, Norway,

Hierarchical Modeling for Univariate Spatial Data

ES-2 Lecture: More Least-squares Fitting. Spring 2017

Generalized Linear Models for Non-Normal Data

Statistics & Data Sciences: First Year Prelim Exam May 2018

Summary STK 4150/9150

georglm : a package for generalised linear spatial models introductory session

Technical Vignette 5: Understanding intrinsic Gaussian Markov random field spatial models, including intrinsic conditional autoregressive models

Political Science 6000: Beginnings and Mini Math Boot Camp

Log Gaussian Cox Processes. Chi Group Meeting February 23, 2016

Modeling Data with Linear Combinations of Basis Functions. Read Chapter 3 in the text by Bishop

CPSC 340 Assignment 4 (due November 17 ATE)

Linear Regression. Data Model. β, σ 2. Process Model. ,V β. ,s 2. s 1. Parameter Model

Multiple Linear Regression (solutions to exercises)

STA414/2104 Statistical Methods for Machine Learning II

Experiment 1: Linear Regression

Default Priors and Effcient Posterior Computation in Bayesian

The evdbayes Package

Data Assimilation Research Testbed Tutorial

x. Figure 1: Examples of univariate Gaussian pdfs N (x; µ, σ 2 ).

The Expectation-Maximization Algorithm

Introduction to Statistics and R

Linear Regression (9/11/13)

MCMC 2: Lecture 3 SIR models - more topics. Phil O Neill Theo Kypraios School of Mathematical Sciences University of Nottingham

The Bernstein Mechansim

Business Statistics. Lecture 10: Course Review

Nonparameteric Regression:

Factor Analysis (10/2/13)

Non-Parametric Bayes

Beyond MCMC in fitting complex Bayesian models: The INLA method

Nonstationary spatial process modeling Part II Paul D. Sampson --- Catherine Calder Univ of Washington --- Ohio State University

Cheng Soon Ong & Christian Walder. Canberra February June 2018

Part 6: Multivariate Normal and Linear Models

cor(dataset$measurement1, dataset$measurement2, method= pearson ) cor.test(datavector1, datavector2, method= pearson )

Dependence. Practitioner Course: Portfolio Optimization. John Dodson. September 10, Dependence. John Dodson. Outline.

1 gamma.mixed: Mixed effects gamma regression

STA 4273H: Statistical Machine Learning

Community Health Needs Assessment through Spatial Regression Modeling

The Bayesian approach to inverse problems

CS Homework 3. October 15, 2009

4 Elementary matrices, continued

Probabilistic Graphical Models

Bayesian Graphical Models

REN R 690 Lab Geostatistics Lab

Luke B Smith and Brian J Reich North Carolina State University May 21, 2013

2017 SISG Bayesian Statistics for Genetics R Notes: Generalized Linear Modeling

Computer Vision Group Prof. Daniel Cremers. 2. Regression (cont.)

Stochastic vs Deterministic Pre-stack Inversion Methods. Brian Russell

Chapter 1. Modeling Basics

coenocliner: a coenocline simulation package for R

Metric Predicted Variable on Two Groups

Multivariate Gaussian Random Fields with SPDEs

CSC2515 Assignment #2

Transcription:

SARMA 2012 Sharp statistical tools 1 2012-10-17 Computer exercise INLA Loading R-packages Load the needed packages: > library(inla) > library(plotrix) > library(geor) > library(fields) > library(maps) > library(spatstat) You might need to install INLA > source("http://www.math.ntnu.no/inla/givemeinla.r") Loading the data As an example we ll study the locations of 3605 trees in a tropical rain forest. The data comes with covariate data giving the elevation (altitude) and slope of elevation in the study region. The data could be analysed as a spatial-point process, but we ll simplify matters and compute the number of trees in each grid cell. The data is then modelled using a Poisson-process with spatially varying mean. > ##Load and examine the data > data(bei) > plot(bei) > ##extract elevation and gradient > elevation <- bei.extra$elev$v > gradient <- bei.extra$grad$v > ##compute counts in each grid-cell > counts <- matrix(0,dim(gradient)[1],dim(gradient)[2]) > ##size of each grid cell > dx <- (bei.extra$elev$xcol[2] - bei.extra$elev$xcol[1])/2 > dy <- (bei.extra$elev$yrow[2] - bei.extra$elev$yrow[1])/2 > ##loop over the grid cells, and count points > for(i in 1:dim(counts)[1]){ Y <- bei.extra$grad$yrow[i] for(j in 1:dim(counts)[2]){ X <- bei.extra$grad$xcol[j] counts[i,j] <- sum( (X-dx)<bei$x & bei$x<=(x+dy) & (Y-dy)<bei$y & bei$y<=(y+dx) ) } } Lets plot the resulting gridded data, > par(mfrow=c(2,2)) > image.plot(elevation) > image.plot(gradient) > image.plot(counts) and collect the data into a suitable data-frame > bei <- data.frame(counts=c(counts), x=rep(bei.extra$elev$xcol, each=length(bei.extra$elev$yrow)), y=rep(bei.extra$elev$yrow, length(bei.extra$elev$xcol)), elevation=c(elevation), gradient=c(gradient))

SARMA 2012 Sharp statistical tools 2 Triangulation The first step in creating a Gaussian Markov random field solution to the Stochastic Partial Differential Equation, as described in the lecture notes, is to create a triangular mesh over the observation locations. We could create a mesh for the lattice > mesh.lattice <- inla.mesh.lattice(x=bei.extra$elev$xcol, y=bei.extra$elev$yrow) > mesh.lattice <- inla.mesh.create( lattice=mesh.lattice ) > plot( mesh.lattice ) > summary( mesh.lattice ) Howeverthismesh ismuch denserthanneeded, insteadwe computeamesh with a suitable internal triangulation (max edge length 50) and coarser extension. > loc <- as.matrix(bei[,c("x","y")]) > mesh <- inla.mesh.create.helper(points.domain=loc, offset = c(-0.05, -0.2), n=c(8,16), max.edge = c(25,150)) > summary(mesh) > ##plot > plot(mesh) > ##add the points for reference (note the possible aliasing effect) > points(loc[,1], loc[,2], col="red", pch=19, cex=.1) Precision matrices Given the mesh the next step is to compute the matrices needed to create the precision matrix Q, and the observation matrix needed to link the mesh to our observations locations. > spde <- inla.spde2.matern(mesh, alpha=2) > A <- inla.spde.make.a(mesh, loc) The resulting matrices are sparse. > image( spde$param.inla$m0 ) > image( spde$param.inla$m1 ) > image( spde$param.inla$m2 ) > image(a) > ##or only the first 1000 observations > image(a[1:1000,]) In addition to these we also need an index vector for the latent field > spde.idx <- inla.spde.make.index("spatial", n.mesh=mesh$n) Setting up the model Thenextstepistocreatealistof effectsandaninla-stackobjectcontaining the relevant components of the model. For numerical reasons we select to put the intercept with the field; the second component of the effects contains all possible covariates. The stack object combines the observations, observations matrices, effects, and a tag which can be used to identify components of the posterior. > effects <- list(c(list(intercept=rep(1,mesh$n)), spde.idx), covar=bei[,c("x","y","elevation","gradient")]) > stack.obs <- inla.stack(data=list(y=bei$counts), A=list(A, 1), effects=effects, tag="obs") The stack must contain one observation matrix for each effect. Here the 1 indicates an identity matrix linking the covariates (second element in effects) with the observations. Predictions at unobserved locations can be added by creating a stack without observations, i.e. Y = NA.

SARMA 2012 Sharp statistical tools 3 The model The model defined in stack.obs above can be written as y i Po(e zi ) Z = Aw +IBβw N(0,Q)β N ( 0,I 10 2) Thus we have Poissonobservations, y i ; with intensity, z i ; determined by a latent field, w; linked to the observations location through an observation matrix, A; and with a mean consisting of covariates, B; and with a Normal-prior for the regresison coefficients. Strictly speaking the model only defines things up to z i, the type of observations are defined in the call to inla in a few paragraphs. In principal the latent field is expanded to Z = [ A B ][ ] w β [ ( [0 ] [ ] 1 ) w Qspde 0 N, β] 0 0 I 10 2, Priors The default INLA priors can be extracted, and changing the values is straight forward. For the poission observations we have > hyper.obs <- inla.models()$likelihood$poisson$hyper > hyper.obs Which is rather uninteresting. The prior for Gaussian observations has a little more to offer > hyper.gaussian <- inla.models()$likelihood$gaussian$hyper > str(hyper.gaussian) Even the spatial model (the spde-object) comes with a default prior; the range parameter has been chosen based on the coordinates of the mesh. > spde$param.inla$theta.mu > spde$param.inla$theta.q Finally the precision of the regression coefficients, β, can be altered from the predetermined values through the control.fixed option in the call to inla, see. >?control.fixed Running INLA Having defined the model, data structure, initial values, and prior distributions we are ready to estimate the model. We try both a simple, and a more complex mean. In the following the model is defined using the standard notation in R with the names refering to elements in stack.obs. family="poisson" tells INLA that our data should be considered as Poisson observations of the latent field. Other possible observation models are: > names(inla.models()$likelihood) Finally control.family allows us alter the priors of the observation likelihood. DO NOT RUN!!! Estimation of the two models. > res1 <- inla(y ~ -1 + intercept + f(spatial, model=spde), family="poisson", data = inla.stack.data(stack.obs), control.family = list(hyper = hyper.obs), control.predictor = list(a=inla.stack.a(stack.obs),

SARMA 2012 Sharp statistical tools 4 compute=true), control.results=list(return.marginals.random=false, return.marginals.predictor=false), verbose=true) > res2 <- inla(y ~ -1 + intercept + elevation + gradient + x + y + f(spatial, model=spde), family="poisson", data = inla.stack.data(stack.obs), control.family = list(hyper = hyper.obs), control.predictor = list(a=inla.stack.a(stack.obs), compute=true), control.results=list(return.marginals.random=false, return.marginals.predictor=false), verbose=true) Run this instead Load precompute values instead. > load("inlares.rdata") We extract parameters for the latent field using > spde.res1 <- inla.spde2.result(res1, "spatial", spde) > spde.res2 <- inla.spde2.result(res2, "spatial", spde) If the option return.marginals.random=true had been chosen above these structures would also include posteriors for the field-parameters, rather than only summaries. Analysing INLA output Estimated parameters As a first step in analysing the output we compare the estimated regression parameters for the two models, > res1$summary.fixed > res2$summary.fixed and for the hyper-parameters of the latent field. > res1$summary.hyperpar > res2$summary.hyperpar Or the equivalent range and variances > spde.res1$summary.log.range.nominal > spde.res2$summary.log.range.nominal > spde.res1$summary.log.variance.nominal > spde.res2$summary.log.variance.nominal We can also plot the posterior distributions of the hyper-parameters, along with their priors (in red) > par(mfcol=c(2,1)) > theta.mu <- spde$param.inla$theta.mu > theta.q <- spde$param.inla$theta.q > for(i in 1:2){ ##posterior plot(res1$marginals.hyperpar[[i]], type="l", main=names(res1$marginals.hyperpar)[i]) ##prior x <- res1$marginals.hyperpar[[i]][,1] lines(x, dnorm(x, theta.mu[i], sqrt(1/theta.q[i,i])), col="red") ##and for the second model lines(res2$marginals.hyperpar[[i]], col="blue") } Marginals for the regression parameters can be found in

SARMA 2012 Sharp statistical tools 5 > str(res2$marginals.fixed) Latent field Lets first plot the observations and the mean component of the second field > par(mfrow=c(2,1)) > image.plot( as.image(bei$counts, x=bei[,c("x","y")]),asp=1 ) > ##compute the mean, and plot > B <- as.matrix( cbind(1,bei[,rownames(res2$summary.fixed)[-1]]) ) > mu <- B %*% res2$summary.fixed[,"mean"] > image.plot( as.image(mu, x=bei[,c("x","y")]),asp=1 ) To plot the GMRF component of the fields we need to define a projector from the field to a suitable grid that can be used for plotting > mesh.projector <- inla.mesh.projector(mesh, dims=c(200,200)) We can now plot the expectation and standard deviation of the field component w > ##study component > str(res1$summary.random) > ##extract mean and sd > EZ.1 <- res1$summary.random$spatial[,"mean"] > EZ.2 <- res2$summary.random$spatial[,"mean"] > sd.1 <- res1$summary.random$spatial[,"sd"] > sd.2 <- res2$summary.random$spatial[,"sd"] > ##plot the fields > par( mfrow=c(2,2) ) inla.mesh.project(mesh.projector, EZ.1), main="ez.1") inla.mesh.project(mesh.projector, EZ.2), main="ez.2") inla.mesh.project(mesh.projector, sd.1), main="sd.1") inla.mesh.project(mesh.projector, sd.2), main="sd.2") > ##add the mesh for reference to one of the plots > plot(mesh,add=true,col="grey") A better alternative is to limit the plotting to the same are as the observations inla.mesh.project(mesh.projector, EZ.1), xlim=range(bei$x), ylim=range(bei$y)) We have now plotted the mean component and the random component of the latent field, we would also like to plot the full latent field. Predictions for the full field are contained in > str(res1$summary.linear.predictor) with indecies for the observed points given by > I <- inla.stack.index(stack.obs, "obs") > str(i) > ##and extract the relevant components > E1 <- res1$summary.linear.predictor[i$data,] > E2 <- res2$summary.linear.predictor[i$data,] Lets plot the field and uncertainties for the two fields

SARMA 2012 Sharp statistical tools 6 > par( mfrow=c(3,3) ) > zlim <- range( c(e1[,"mean"],e2[,"mean"]) ) > image.plot( as.image(e1[,"mean"], x=bei[,c("x","y")]),asp=1, zlim=zlim, main="ez first field") > image.plot( as.image(e2[,"mean"], x=bei[,c("x","y")]),asp=1, zlim=zlim, main="ez second field") > image.plot( as.image(bei$counts, x=bei[,c("x","y")]), asp=1, main="observations") > ##just the regression part > image.plot( as.image(rep(res1$summary.fixed[,"mean"],dim(bei)[1]), x=bei[,c("x","y")]), asp=1, zlim=zlim, main="mean first field") > image.plot( as.image(mu, x=bei[,c("x","y")]), asp=1, zlim=zlim, main="mean second field") > plot(0,0,type="n") > ##the uncertainties > zlimv <- range( c(e1[,"sd"],e2[,"sd"]) ) > image.plot( as.image(e1[,"sd"], x=bei[,c("x","y")]),asp=1, zlim=zlimv, main="vz first field") > image.plot( as.image(e2[,"sd"], x=bei[,c("x","y")]),asp=1, zlim=zlimv, main="vz second field") Comparing the covariance to Matérn We could also compare dependence in the SPDE model to those of the Matérn covariance we re approximating. First we compute the precision matrix given the estimated parameters > Q <- inla.spde2.precision(spde, res2$summary.hyperpar[,"mean"]) and study the sparsity > image(q) Inverting the precision matrix gives the covariance matrix, and correlation matrix (this takes several seconds). > S <- as.matrix(solve(q)) > S <- diag(1/sqrt(diag(s))) %*% S %*% diag(1/sqrt(diag(s))) > ##we also need the distance matrix > D <- as.matrix(dist(mesh$loc[,1:2])) We now need the covariance function and variograms for the SPDE model. We obtain the approximate posterior means for the variance τ and range κ. > tau <- exp( spde.res2$summary.log.tau[,"mean"] ) > kappa <- exp( spde.res2$summary.log.kappa[,"mean"] ) We also compute the theoretical Matérn correlation function > d <- seq(0, max(d), len=1e3) > S.theory <- (d*kappa)*besselk(d*kappa,1) > S.theory[1] <- 1 and compare the two > par(mfrow=c(2,1)) > ##using only a few interior points

SARMA 2012 Sharp statistical tools 7 > plot(d[,1:10], as.matrix(s[,1:10]), pch=19, cex=.1, xlab="dist", ylab="") > lines(d, S.theory, col=2) > ##or only the edge points > I <- unique(c(mesh$segm$bnd$idx)) > plot(d[,i], as.matrix(s[,i]), pch=19, cex=.1, xlab="dist", ylab="") > lines(d, S.theory, col=2) The rather larger spread around the theoretical function in the second plot is due to edge effects and the mesh size being reasonable large compared to the range. The first plot gives the correlation between 10 central points and the rest of the field, reducing the edge effects. The end!