##load data
data(mesa.data)
data(mesa.data.model)
data(mesa.data.res)
##extract estimated parameters
x <- mesa.data.res$par.est$res.best$par
##find regression parameters using GLS
x.reg <- cond.expectation(x, mesa.data.model, only.pars = TRUE)
str(x.reg)
##compute predictions at all locations, including beta-fields
EX <- cond.expectation(x, mesa.data.model, compute.beta=TRUE)
##compute predictions at only observations locations
EX.obs <- cond.expectation(x, mesa.data.model, mesa.data=mesa.data,
only.obs=TRUE, compute.beta=FALSE)
##Let's load precomputed results instead.
EX <- mesa.data.res$EX
EX.obs <- mesa.data.res$EX.obs
##Compare the predictions at all locations and only obs
dim(EX$EX)
dim(EX.obs$EX)
##estimate beta from the observations for reference
##create data matrix
D <- create.data.matrix(mesa.data)
beta <- matrix(NA,dim(D)[2], dim(mesa.data$trend)[2])
##extact the temporal trends
F <- mesa.data$trend
##drop the date column
F$date <- NULL
##estimate the beta-coeficients at each location
for(i in 1:dim(D)[2])
beta[i,] <- summary(lm(D[,i] ~ as.matrix(F)))$coefficients[,1]
##Study the results
##Start by comparing beta fields
par(mfcol=c(1,1), mar=c(4.5,4.5,2,.5), pty="s")
plot(x=beta[,1], y=EX$EX.beta[,1], main="Temporal Intercept",
xlab="Empirical estimate", ylab="Spatio-Temporal Model")
abline(0,1,col="grey")
##plot predictions and observations for 4 locations
par(mfrow=c(4,1),mar=c(2.5,2.5,2,.5))
plotPrediction(EX, 1, mesa.data.model)
plotPrediction(EX, 10, mesa.data.model)
plotPrediction(EX, 17, mesa.data.model)
plotPrediction(EX, 22, mesa.data.model)
##compare the only obs predictions with what we can extract from EX
par(mfcol=c(1,1), mar=c(4.5,4.5,2,.5), pty="s")
plot(EX$EX[EX$I], EX.obs$EX)
##minor (1e-14) numerical differences in the results
print(range(EX$EX[EX$I]-EX.obs$EX))
Run the code above in your browser using DataLab