Академический Документы
Профессиональный Документы
Культура Документы
2012-10-17
Creating a geodata-object
Our first step is to construct a geodata object containing locations, observations
(fall temperatures) and covariates.
> temp.geodata <- as.geodata(temp, coords.col=1:2,
data.col=8, covar.col=1:4)
We then examine the data structure and plot the observations
> summary(temp.geodata)
> plot(temp.geodata)
The initial plot has modeled the mean using a constant; lets try some more
interesting mean-models.
> #1st trend using long and lat
> plot(temp.geodata, trend="1st")
> #add elevation and distance to coast
> plot(temp.geodata, trend=~long+lat+elevation+dCoast)
Computing variograms
Having decided on a mean-model we now examine the residual dependence.
Both for a model with only a constant mean, and for a more advanced model
> ##largest distance that makes sense is in the North-South direction.
> D.max <- 1.5*diff(range(temp$lat))
> ##compute variograms
> estV.const <- variog(temp.geodata, option="bin",
trend="cte", bin.cloud="TRUE",
max.dist=D.max)
> ##use uvec to specify more bins
> estV.const.50 <- variog(temp.geodata, option="bin",
trend="cte", bin.cloud="TRUE",
max.dist=D.max, uvec=50)
> ##with different trend
> estV.trend <- variog(temp.geodata, option="bin",
trend=~long+lat+elevation+dCoast,
bin.cloud="TRUE", max.dist=D.max)
2012-10-14, 13:55:12Z, rev.3326
Cross-validation
geoR provides several features for cross-validation. Both leave-one-out crossvalidation and validation using held-out data can be done using xvalid. To
save time, we have here chosen not to reestimate the covariance parameters.
> cv.ML.sph <- xvalid(temp.geodata, reestimate=FALSE,
model=estV.ML.sph)
The cross-validation produces a large number of diagnostics plots that can be
examined (these contain both residuals and standardised residuals).
> par(mfrow=c(3,4), mar=c(3,3,0,1), mgp=c(2,1,0))
> plot(cv.ML.sph)
Prediction on a grid
A common goal of spatial modelling is to compute predictions at unobserved
target locations. Here this will be illustrated by predicting temperature over
the entire US. First lets load the gridded covariates and plot them as images.
> grid <- read.csv("temp1997_grid.csv")
> ##convert data to images for plotting
> elev.Im <- as.image(grid$elevation, grid[,c("long","lat")],
nrow=115, ncol=50)
> dCoast.Im <- as.image(grid$dCoast, grid[,c("long","lat")],
nrow=115, ncol=50)
> ##and plot, adding a map for reference
> par(mfrow=c(1,2))
> image.plot(elev.Im)
> map("world", add=TRUE, col="magenta", lwd=2)
> image.plot(dCoast.Im)
> map("world", add=TRUE, col="magenta", lwd=2)
We need to collect the prediction grid into a geodata-object, with constant 1
observations.
> grid.geodata <- as.geodata(cbind(1,grid), data.col=1,
coords.col=2:3, covar.col=2:5)
Using the gridded data and estimated parameters we can compute predictions,
> kc <- krige.conv(geodata=temp.geodata,
locations=grid.geodata$coords,
krige=krige.control(type.krige="OK",
trend.d=trend.spatial(estV.trend$trend,
temp.geodata),
trend.l=trend.spatial(estV.trend$trend,
grid.geodata),
obj.model=estV.ML.sph)
)
We can also compute predictions based on the mean value in the Kriging model
> mu <- (trend.spatial(estV.trend$trend, grid.geodata) %*%
estV.ML.sph$beta)
or based on only a regression models
> OLS.temp <- lm(SON ~ long+lat+elevation+dCoast, temp)
> OLS.pred <- predict(OLS.temp, grid)
Plot predicted values, prediction uncertainties, and observation locations.
> par(mfrow=c(2,2))
> ##predictions
> image.plot( as.image(kc$predict, grid[,c("long","lat")],
nrow=115, ncol=50))
> map("world", add=TRUE, col="black", lwd=2)
> points(temp$long, temp$lat, pch=19, col="magenta", cex=.5)
> ##standard deviations
> image.plot( as.image(sqrt(kc$krige.var), grid[,c("long","lat")],
nrow=115, ncol=50))
> map("world", add=TRUE, col="black", lwd=2)
> points(temp$long, temp$lat, pch=19, col="magenta", cex=.5)
mean component
> image.plot( as.image(mu, grid[,c("long","lat")],
nrow=115, ncol=50))
> map("world", add=TRUE, col="black", lwd=2)
> points(temp$long, temp$lat, pch=19, col="magenta", cex=.5)
and OLS predictions
> image.plot( as.image(OLS.pred, grid[,c("long","lat")],
nrow=115, ncol=50))
> map("world", add=TRUE, col="black", lwd=2)
> points(temp$long, temp$lat, pch=19, col="magenta", cex=.5)
The end!
2012-10-14, 13:55:12Z, rev.3326