## ---- toy example ----
## We use the sampled, i.e., one dimensional SVCs
str(SVCdata)
# sub-sample data to have feasible run time for example
set.seed(123)
id <- sample(length(SVCdata$locs), 50)
## SVC_mle call with matrix arguments
fit <- with(SVCdata, SVC_mle(
y[id], X[id, ], locs[id],
control = SVC_mle_control(profileLik = TRUE, cov.name = "mat32")))
## SVC_mle call with formula
df <- with(SVCdata, data.frame(y = y[id], X = X[id, -1]))
fit <- SVC_mle(
y ~ X, data = df, locs = SVCdata$locs[id],
control = SVC_mle_control(profileLik = TRUE, cov.name = "mat32")
)
class(fit)
summary(fit)
# \donttest{
## ---- real data example ----
require(sp)
## get data set
data("meuse", package = "sp")
# construct data matrix and response, scale locations
y <- log(meuse$cadmium)
X <- model.matrix(~1+dist+lime+elev, data = meuse)
locs <- as.matrix(meuse[, 1:2])/1000
## starting MLE
# the next call takes a couple of seconds
fit <- SVC_mle(
y = y, X = X, locs = locs,
# has 4 fixed effects, but only 3 random effects (SVC)
# elev is missing in SVC
W = X[, 1:3],
control = SVC_mle_control(
# inital values for 3 SVC
# 7 = (3 * 2 covariance parameters + nugget)
init = c(rep(c(0.4, 0.2), 3), 0.2),
profileLik = TRUE
)
)
## summary and residual output
summary(fit)
plot(fit)
## predict
# new locations
newlocs <- expand.grid(
x = seq(min(locs[, 1]), max(locs[, 1]), length.out = 30),
y = seq(min(locs[, 2]), max(locs[, 2]), length.out = 30))
# predict SVC for new locations
SVC <- predict(fit, newlocs = as.matrix(newlocs))
# visualization
sp.SVC <- SVC
coordinates(sp.SVC) <- ~loc_1+loc_2
spplot(sp.SVC, colorkey = TRUE)
# }
Run the code above in your browser using DataLab