spatial data analysis with geor - biostatisticsspatial data analysis with geor ris an increasingly...

45
Spatial data analysis with geoR R is an increasingly popular freeware alternative to S-plus, available from the web at www.r-project.org. geoR Examples – p. 1/11

Upload: vuonglien

Post on 19-Mar-2018

232 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Spatial data analysis withgeoRR is an increasingly popular freeware alternative toS-plus , available from the web atwww.r-project.org .

geoR Examples – p. 1/11

Page 2: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Spatial data analysis withgeoRR is an increasingly popular freeware alternative toS-plus , available from the web atwww.r-project.org .

geoR is an R add-on package with many functions forgeostatistical analysis

geoR Examples – p. 1/11

Page 3: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Spatial data analysis withgeoRR is an increasingly popular freeware alternative toS-plus , available from the web atwww.r-project.org .

geoR is an R add-on package with many functions forgeostatistical analysis

geoR (and the akima library) can be easily installedfrom http://cran.us.r-project/org

in Windows click on Packages and Install fromCRANto select geoR

in Linux/Unix use the install.package functionfrom within R; see also help(install.package)

geoR Examples – p. 1/11

Page 4: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Spatial data analysis withgeoRR is an increasingly popular freeware alternative toS-plus , available from the web atwww.r-project.org .

geoR is an R add-on package with many functions forgeostatistical analysis

geoR (and the akima library) can be easily installedfrom http://cran.us.r-project/org

in Windows click on Packages and Install fromCRANto select geoR

in Linux/Unix use the install.package functionfrom within R; see also help(install.package)

This tutorial describes methods for kriging and relatedgeostatistical operations available in geoR; online at:

www.biostat.umn.edu/ ∼brad/data/booknew.RgeoR Examples – p. 1/11

Page 5: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Exploratory tools in R

Consider scallops data in data frame myscallops

geoR Examples – p. 2/11

Page 6: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Exploratory tools in R

Consider scallops data in data frame myscallops

Recall: Often helpful to create image plots and placecontour lines on the plot.

geoR Examples – p. 2/11

Page 7: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Exploratory tools in R

Consider scallops data in data frame myscallops

Recall: Often helpful to create image plots and placecontour lines on the plot.

Invoke akima library: library( akima )

geoR Examples – p. 2/11

Page 8: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Exploratory tools in R

Consider scallops data in data frame myscallops

Recall: Often helpful to create image plots and placecontour lines on the plot.

Invoke akima library: library( akima )

int.scp < − interp.new (myscallops$long,myscallops$lat, myscallops$lgcatch)

geoR Examples – p. 2/11

Page 9: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Exploratory tools in R

Consider scallops data in data frame myscallops

Recall: Often helpful to create image plots and placecontour lines on the plot.

Invoke akima library: library( akima )

int.scp < − interp.new (myscallops$long,myscallops$lat, myscallops$lgcatch)

image ( int.scp ,xlim=range(myscallops$long),ylim=range(myscallops$lat))

geoR Examples – p. 2/11

Page 10: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Exploratory tools in R

Consider scallops data in data frame myscallops

Recall: Often helpful to create image plots and placecontour lines on the plot.

Invoke akima library: library( akima )

int.scp < − interp.new (myscallops$long,myscallops$lat, myscallops$lgcatch)

image ( int.scp ,xlim=range(myscallops$long),ylim=range(myscallops$lat))

contour ( int.scp , add=T)

geoR Examples – p. 2/11

Page 11: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Exploratory tools in R

Consider scallops data in data frame myscallops

Recall: Often helpful to create image plots and placecontour lines on the plot.

Invoke akima library: library( akima )

int.scp < − interp.new (myscallops$long,myscallops$lat, myscallops$lgcatch)

image ( int.scp ,xlim=range(myscallops$long),ylim=range(myscallops$lat))

contour ( int.scp , add=T)

persp(int.scp, xlim=..., ylim=...)

geoR Examples – p. 2/11

Page 12: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Image plot with contour lines

−73.5 −73.0 −72.5 −72.0

39.0

39.5

40.0

40.5

Longitude

Latit

ude

geoR Examples – p. 3/11

Page 13: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Variogram fitting with geoR

myscallops <- (myscallops$long,myscallops$lat, myscallops$lgcatch)

geoR Examples – p. 4/11

Page 14: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Variogram fitting with geoR

myscallops <- (myscallops$long,myscallops$lat, myscallops$lgcatch)

Crucial: Create a geodata object

geoR Examples – p. 4/11

Page 15: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Variogram fitting with geoR

myscallops <- (myscallops$long,myscallops$lat, myscallops$lgcatch)

Crucial: Create a geodata object

scallops.geo <- as.geodata(myscallops,coords.col=1:2, data.col=3)

geoR Examples – p. 4/11

Page 16: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Variogram fitting with geoR

myscallops <- (myscallops$long,myscallops$lat, myscallops$lgcatch)

Crucial: Create a geodata object

scallops.geo <- as.geodata(myscallops,coords.col=1:2, data.col=3)

Next a variogram object is created.

geoR Examples – p. 4/11

Page 17: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Variogram fitting with geoR

myscallops <- (myscallops$long,myscallops$lat, myscallops$lgcatch)

Crucial: Create a geodata object

scallops.geo <- as.geodata(myscallops,coords.col=1:2, data.col=3)

Next a variogram object is created.

scallops.var <- variog ( scallops.geo ,estimator.type=‘‘classical’’)

geoR Examples – p. 4/11

Page 18: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Variogram fitting with geoR

myscallops <- (myscallops$long,myscallops$lat, myscallops$lgcatch)

Crucial: Create a geodata object

scallops.geo <- as.geodata(myscallops,coords.col=1:2, data.col=3)

Next a variogram object is created.

scallops.var <- variog ( scallops.geo ,estimator.type=‘‘classical’’)

A robust variogram is obtained by settingestimator.type = “robust”

geoR Examples – p. 4/11

Page 19: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Variogram fitting with geoR

myscallops <- (myscallops$long,myscallops$lat, myscallops$lgcatch)

Crucial: Create a geodata object

scallops.geo <- as.geodata(myscallops,coords.col=1:2, data.col=3)

Next a variogram object is created.

scallops.var <- variog ( scallops.geo ,estimator.type=‘‘classical’’)

A robust variogram is obtained by settingestimator.type = “robust”

Plots: plot (scallops.var)

geoR Examples – p. 4/11

Page 20: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Variograms: classical and robust

0.0 0.5 1.0 1.5 2.0 2.5 3.0

02

46

distance

sem

ivar

ianc

e

(a)

0.0 0.5 1.0 1.5 2.0 2.5 3.0

02

46

8

distance

sem

ivar

ianc

e

(b)

geoR Examples – p. 5/11

Page 21: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Variogram fitting in geoR

geoR provides a wide range of covariance functions forfitting variograms:

geoR Examples – p. 6/11

Page 22: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Variogram fitting in geoR

geoR provides a wide range of covariance functions forfitting variograms:

exponential, gaussian, spherical,matérn etc.

geoR Examples – p. 6/11

Page 23: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Variogram fitting in geoR

geoR provides a wide range of covariance functions forfitting variograms:

exponential, gaussian, spherical,matérn etc.

The function variofit estimates the sill, the range,and the nugget parameters under a specifiedcovariance model.

geoR Examples – p. 6/11

Page 24: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Variogram fitting in geoR

geoR provides a wide range of covariance functions forfitting variograms:

exponential, gaussian, spherical,matérn etc.

The function variofit estimates the sill, the range,and the nugget parameters under a specifiedcovariance model.

Example: scallops.var.fit <-variofit (scallops.var, ini.cov.pars =c(1.0,5.0), cov.model=’’exponential’’,fix.nugget=FALSE, nugget=1.0)

geoR Examples – p. 6/11

Page 25: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Likelihood model fitting

Both ML and REML are implemented through geoRfunction likfit .

geoR Examples – p. 7/11

Page 26: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Likelihood model fitting

Both ML and REML are implemented through geoRfunction likfit .

scallops.lik.fit <- likfit (scallops.geo,ini.cov.pars=c(1.0,2.0), cov.model =‘‘exponential’’, trend = ‘‘cte’’,fix.nugget = FALSE, nugget = 1.0,nospatial = TRUE, method.lik = ‘‘ML’’)

geoR Examples – p. 7/11

Page 27: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Likelihood model fitting

Both ML and REML are implemented through geoRfunction likfit .

scallops.lik.fit <- likfit (scallops.geo,ini.cov.pars=c(1.0,2.0), cov.model =‘‘exponential’’, trend = ‘‘cte’’,fix.nugget = FALSE, nugget = 1.0,nospatial = TRUE, method.lik = ‘‘ML’’)

The option trend = ‘‘cte’’ means a spatialregression model with constant mean.

geoR Examples – p. 7/11

Page 28: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Likelihood model fitting

Both ML and REML are implemented through geoRfunction likfit .

scallops.lik.fit <- likfit (scallops.geo,ini.cov.pars=c(1.0,2.0), cov.model =‘‘exponential’’, trend = ‘‘cte’’,fix.nugget = FALSE, nugget = 1.0,nospatial = TRUE, method.lik = ‘‘ML’’)

The option trend = ‘‘cte’’ means a spatialregression model with constant mean.

Output:

likfit: estimated model parameters:beta tausq sigmasq phi

2.3748 0.0947 5.7675 0.2338

geoR Examples – p. 7/11

Page 29: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Bayesian kriging in geoR

Bayesian kriging is carried out by the functionkrige.bayes

geoR Examples – p. 8/11

Page 30: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Bayesian kriging in geoR

Bayesian kriging is carried out by the functionkrige.bayes

This is a handy tool improved upon the likelihoodmethods by providing posterior samples of all the modelparameters, which lead to estimation of their variability.

geoR Examples – p. 8/11

Page 31: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Bayesian kriging in geoR

Bayesian kriging is carried out by the functionkrige.bayes

This is a handy tool improved upon the likelihoodmethods by providing posterior samples of all the modelparameters, which lead to estimation of their variability.

The krige.bayes function is less versatile thanWinBUGS:

geoR Examples – p. 8/11

Page 32: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Bayesian kriging in geoR

Bayesian kriging is carried out by the functionkrige.bayes

This is a handy tool improved upon the likelihoodmethods by providing posterior samples of all the modelparameters, which lead to estimation of their variability.

The krige.bayes function is less versatile thanWinBUGS:

more limited in the types of models it can handle

geoR Examples – p. 8/11

Page 33: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Bayesian kriging in geoR

Bayesian kriging is carried out by the functionkrige.bayes

This is a handy tool improved upon the likelihoodmethods by providing posterior samples of all the modelparameters, which lead to estimation of their variability.

The krige.bayes function is less versatile thanWinBUGS:

more limited in the types of models it can handleupdating is not through MCMC methods

geoR Examples – p. 8/11

Page 34: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Bayesian kriging in geoR

Bayesian kriging is carried out by the functionkrige.bayes

This is a handy tool improved upon the likelihoodmethods by providing posterior samples of all the modelparameters, which lead to estimation of their variability.

The krige.bayes function is less versatile thanWinBUGS:

more limited in the types of models it can handleupdating is not through MCMC methods

BUT: an advantage is it offers a wider range ofcovariance functions

geoR Examples – p. 8/11

Page 35: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Bayesian kriging example

Fitting a constant mean spatial regression model to thescallops data:

geoR Examples – p. 9/11

Page 36: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Bayesian kriging example

Fitting a constant mean spatial regression model to thescallops data:

scallops.bayes1 <-krige.bayes (scallops.geo, locations =‘‘no’’, borders = NULL, model =model.control(trend.d = ‘‘cte’’,cov.model = ‘‘exponential’’), prior =prior.control(beta.prior = ‘‘flat’’,sigmasq.prior = ‘‘reciprocal’’,tausq.rel.prior = ‘‘uniform’’,tausq.rel.discrete=seq(from=0.0, to=1.0,by=0.01)))

geoR Examples – p. 9/11

Page 37: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Bayesian kriging example

Fitting a constant mean spatial regression model to thescallops data:

scallops.bayes1 <-krige.bayes (scallops.geo, locations =‘‘no’’, borders = NULL, model =model.control(trend.d = ‘‘cte’’,cov.model = ‘‘exponential’’), prior =prior.control(beta.prior = ‘‘flat’’,sigmasq.prior = ‘‘reciprocal’’,tausq.rel.prior = ‘‘uniform’’,tausq.rel.discrete=seq(from=0.0, to=1.0,by=0.01)))

For predictions set locations = PredLoc wherePredLoc contains the locations to predict.

geoR Examples – p. 9/11

Page 38: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Bayesian kriging example

Obtaining the posterior quantiles:

geoR Examples – p. 10/11

Page 39: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Bayesian kriging example

Obtaining the posterior quantiles:out <- scallops.krige.bayes$posterior

geoR Examples – p. 10/11

Page 40: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Bayesian kriging example

Obtaining the posterior quantiles:out <- scallops.krige.bayes$posterior

out <- out$sample

geoR Examples – p. 10/11

Page 41: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Bayesian kriging example

Obtaining the posterior quantiles:out <- scallops.krige.bayes$posterior

out <- out$sample

beta.qnt <- quantile(out$beta,c(0.50,0.025,0.975))

geoR Examples – p. 10/11

Page 42: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Bayesian kriging example

Obtaining the posterior quantiles:out <- scallops.krige.bayes$posterior

out <- out$sample

beta.qnt <- quantile(out$beta,c(0.50,0.025,0.975))

phi.qnt <- quantile(out$phi,c(0.50,0.025,0.975))

geoR Examples – p. 10/11

Page 43: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Bayesian kriging example

Obtaining the posterior quantiles:out <- scallops.krige.bayes$posterior

out <- out$sample

beta.qnt <- quantile(out$beta,c(0.50,0.025,0.975))

phi.qnt <- quantile(out$phi,c(0.50,0.025,0.975))

sigmasq.qnt <- quantile(out$sigmasq,c(0.50,0.025,0.975))

geoR Examples – p. 10/11

Page 44: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Bayesian kriging example

Obtaining the posterior quantiles:out <- scallops.krige.bayes$posterior

out <- out$sample

beta.qnt <- quantile(out$beta,c(0.50,0.025,0.975))

phi.qnt <- quantile(out$phi,c(0.50,0.025,0.975))

sigmasq.qnt <- quantile(out$sigmasq,c(0.50,0.025,0.975))

tausq.rel.qnt <-quantile(out$tausq.rel,c(0.50,0.025,0.975))

geoR Examples – p. 10/11

Page 45: Spatial data analysis with geoR - BiostatisticsSpatial data analysis with geoR Ris an increasingly popular freeware alternative to ... Bayesian kriging in geoR Bayesian kriging is

Output

> beta.qnt50% 2.5% 97.5%

1.931822 -6.426464 7.786515> phi.qnt

50% 2.5% 97.5%0.5800106 0.2320042 4.9909913

> sigmasq.qnt50% 2.5% 97.5%

11.225002 4.147358 98.484722> tausq.rel.qnt

50% 2.5% 97.5%0.03 0.00 0.19

Note: tausq.rel refers to the ratio of the nugget varianceto the spatial variance, and is seen to be negligible here.

geoR Examples – p. 11/11