# NOT RUN {
rmvn <- function(n, mu=0, V = matrix(1)){
p <- length(mu)
if(any(is.na(match(dim(V),p))))
stop("Dimension problem!")
D <- chol(V)
t(matrix(rnorm(n*p), ncol=p)%*%D + rep(mu,rep(n,p)))
}
##Make some data
set.seed(1)
n <- 2000
coords <- cbind(runif(n,0,1), runif(n,0,1))
x <- cbind(1, rnorm(n))
B <- as.matrix(c(1,5))
sigma.sq <- 5
tau.sq <- 1
phi <- 3/0.5
D <- as.matrix(dist(coords))
R <- exp(-phi*D)
w <- rmvn(1, rep(0,n), sigma.sq*R)
y <- rnorm(n, x%*%B + w, sqrt(tau.sq))
ho <- sample(1:n, 1000)
y.ho <- y[ho]
x.ho <- x[ho,,drop=FALSE]
w.ho <- w[ho]
coords.ho <- coords[ho,]
y <- y[-ho]
x <- x[-ho,,drop=FALSE]
w <- w[-ho,,drop=FALSE]
coords <- coords[-ho,]
##Fit a Conjugate NNGP model and predict for the holdout
sigma.sq.IG <- c(2, sigma.sq)
cov.model <- "exponential"
g <- 10
theta.alpha <- cbind(seq(phi,30,length.out=g), seq(tau.sq/sigma.sq,5,length.out=g))
colnames(theta.alpha) <- c("phi", "alpha")
##one thread
m.c <- spConjNNGP(y~x-1, coords=coords, n.neighbors = 10,
X.0 = x.ho, coords.0 = coords.ho,
k.fold = 5, score.rule = "crps",
n.omp.threads = 1,
theta.alpha = theta.alpha, sigma.sq.IG = sigma.sq.IG, cov.model = cov.model)
m.c$beta.hat
m.c$theta.alpha.sigmaSq
m.c$k.fold.scores
##two threads
m.c <- spConjNNGP(y~x-1, coords=coords, n.neighbors = 10,
X.0 = x.ho, coords.0 = coords.ho,
k.fold = 5, score.rule = "crps",
n.omp.threads = 2,
theta.alpha = theta.alpha, sigma.sq.IG = sigma.sq.IG, cov.model = cov.model)
m.c$beta.hat
m.c$sigmaSq.hat
m.c$k.fold.scores
# }
Run the code above in your browser using DataLab