The spamtree
package is built to run multivariate spatial regressions based on spatial multivariate trees and using a non-separable cross-covariance function on latent dimensions. In this vignette we simulate two spatially referenced outcomes.
library(abind)
library(magrittr)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(ggplot2)
library(spamtree)
set.seed(2021)
<- 30 # coordinate values for jth dimension
SS <- SS^2 # total n. locations, including missing ones
n
<- seq(0.0, 1, length.out=SS)
xlocs <- expand.grid(xlocs, xlocs) %>%
coords as.data.frame() %>%
arrange(Var1, Var2)
<- coords %>% mutate(mv_id=1)
c1 <- coords %>% mutate(mv_id=2)
c2
<- bind_rows(c1, c2)
coords
<- coords %>% dplyr::select(-mv_id)
coords_q <- coords_q %>% as.matrix()
cx <- 1:nrow(cx) - 1
ix <- coords$mv_id
mv_id
<- 2
q <- 1
sigma.sq <- c(.03, .1)
tau.sq <- rep(0, nrow(cx))
tausq_long == 1] <- tau.sq[1]
tausq_long[mv_id == 2] <- tau.sq[2]
tausq_long[mv_id
# some true values for the non-separable multivariate cross-covariance implemented here
<- c(1, 1.5)
ai1 <- c(.1, .51)
ai2 <- c(1, 2)
phi_i <- 5
thetamv
<- matrix(0, q, q)
Dmat 2,1] <- 1
Dmat[upper.tri(Dmat)] <- Dmat[lower.tri(Dmat)]
Dmat[
<- cbind(rnorm(nrow(coords)), rnorm(nrow(coords)))
X <- c(-.9, .05)
B
# generate covariance matrix for full GP
system.time({
<- CrossCovarianceAG10(cx, mv_id, cx, mv_id, ai1, ai2, phi_i, thetamv, Dmat)
CC
})#> user system elapsed
#> 0.065 0.015 0.081
<- t(chol(CC))
LC
# sample the outcomes at all locations
<- X %*% B + LC %*% rnorm(nrow(cx)) + sqrt(tausq_long) * rnorm(nrow(cx))
y_full rm(list=c("CC", "LC"))
# make some na: 0=na
# (this also creates some misalignment)
<- rep(1, nrow(coords))
lna $Var1 > .4) & (coords_q$Var1 < .9) &
lna[((coords_q$Var2 < .7) & (coords_q$Var2 > .4)) & (mv_id == 1)] <- NA
(coords_q$Var1 > .2) & (coords_q$Var1 < .7) &
lna[((coords_q$Var2 < .7) & (coords_q$Var2 > .4)) & (mv_id == 2)] <- NA
(coords_q<- y_full * lna
y
<- coords %>% cbind(y) %>% as.data.frame() simdata
We now run spamtree
. In practice the data size would be much larger, and we would run many more MCMC iterations.
# prepare for spamtrees
<- 200
mcmc_keep <- 200
mcmc_burn <- 2
mcmc_thin
<- spamtree(y, X,
spamtree_done
cx, mv_id, mcmc = list(keep=mcmc_keep, burn=mcmc_burn, thin=mcmc_thin),
num_threads = 10,
verbose=TRUE)
#> Building reference set.
#> Branching the tree 1 ( 1 ) 2 ( 2 ) 3 ( 3 ) 4 ( 4 ).
#> Finalizing with leaves.
#> Building graph.
#> Running MCMC for 600 iterations.
#> 10.0% 226ms (total: 257ms) ~ MCMC acceptance 9.50% (total: 31.15%)
#> 20.0% 199ms (total: 456ms) ~ MCMC acceptance 12.50% (total: 20.66%)
#> 30.0% 193ms (total: 649ms) ~ MCMC acceptance 14.00% (total: 15.47%)
#> 40.0% 200ms (total: 850ms) ~ MCMC acceptance 7.00% (total: 11.62%)
#> 50.0% 196ms (total: 1047ms) ~ MCMC acceptance 5.00% (total: 11.30%)
#> 60.0% 201ms (total: 1249ms) ~ MCMC acceptance 6.50% (total: 11.08%)
#> 70.0% 203ms (total: 1452ms) ~ MCMC acceptance 8.50% (total: 10.69%)
#> 80.0% 209ms (total: 1661ms) ~ MCMC acceptance 11.00% (total: 10.81%)
#> 90.0% 216ms (total: 1878ms) ~ MCMC acceptance 15.00% (total: 12.57%)
#> MCMC done [2074ms]
And finally we do some postprocessing and plot the predictions for both outcomes, and the latent process.
# predictions
<- spamtree_done$yhat_mcmc %>%
y_out abind(along=3) %>% `[`(,1,) %>%
apply(1, mean)
<- spamtree_done$w_mcmc %>%
w_out abind(along=3) %>% `[`(,1,) %>%
apply(1, mean)
<- spamtree_done$coordsinfo %>%
outdf rename(mv_id = sort_mv_id) %>%
cbind(data.frame(w_spamtree = w_out,
y_spamtree = y_out))
# plot predictions
%>%
outdf ggplot(aes(Var1, Var2, fill=y_spamtree)) +
geom_raster() +
facet_grid(~mv_id) +
scale_fill_viridis_c() +
theme_minimal() + theme(legend.position="none")
# plot latent process
%>%
outdf ggplot(aes(Var1, Var2, fill=w_spamtree)) +
geom_raster() +
facet_grid(~mv_id) +
scale_fill_viridis_c() +
theme_minimal() + theme(legend.position="none")