% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/bru.gof.R
\name{devel.cvmeasure}
\alias{devel.cvmeasure}
\title{Variance and correlations measures for prediction components}
\usage{
devel.cvmeasure(joint, prediction1, prediction2, samplers = NULL,
  mesh = NULL)
}
\arguments{
\item{joint}{A joint \code{prediction} of two latent model components.}

\item{prediction1}{A \code{prediction} of first component.}

\item{prediction2}{A \code{prediction} of the first component.}

\item{samplers}{A SpatialPolygon object describing the area for which to compute the cummulative variance measure.}

\item{mesh}{The \code{inla.mesh} for which the prediction was performed (required for cummulative Vmeasure).}
}
\value{
Variance and correlations measures.
}
\description{
Calculates local and integrated variance and correlation measures as introduced by Yuan et al. (2017).
}
\examples{
\donttest{
if (require("INLA", quietly = TRUE)) {
  
# Load Gorilla data

data("gorillas", package = "inlabru")

# Use RColorBrewer 

library(RColorBrewer)

# Fit a model with two components:
# 1) A spatial smoothe SPDE 
# 2) A spatial covariate effect (vegetation)

pcmatern <- inla.spde2.pcmatern(gorillas$mesh, 
                                prior.sigma = c(0.1, 0.01), 
                                prior.range = c(5, 0.01))

cmp <- coordinates ~ vegetation(map = gorillas$gcov$vegetation, model = "factor") +
  spde(map = coordinates, model = pcmatern, mesh = gorillas$mesh) -
  Intercept

fit <- lgcp(cmp, gorillas$nests, samplers = gorillas$boundary)

# Predict SPDE and vegetation at the mesh vertex locations

vrt = vertices(gorillas$mesh)
joint <- predict(fit, vrt,  ~ spde + vegetation)
field <- predict(fit, vrt, ~ spde)
veg <- predict(fit, vrt, ~ vegetation)

# Plot component mean 

multiplot(ggplot() + gg(gorillas$mesh, color = joint$mean) + 
            coord_equal() + theme(legend.position= "bottom"),
          ggplot() + gg(gorillas$mesh, color = field$mean) + 
            coord_equal() + theme(legend.position= "bottom"),
          ggplot() + gg(gorillas$mesh, color = veg$mean) + 
            coord_equal() + theme(legend.position = "bottom"),
          cols = 3)

# Plot component variance 

multiplot(ggplot() + gg(gorillas$mesh, color = joint$var) + 
            coord_equal() + theme(legend.position = "bottom"),
          ggplot() + gg(gorillas$mesh, color = field$var) + 
            coord_equal() + theme(legend.position = "bottom"),
          ggplot() + gg(gorillas$mesh, color = veg$var) + 
            coord_equal() + theme(legend.position = "bottom"),
          cols = 3)

# Calculate variance and correlation measure

vm <- devel.cvmeasure(joint, field, veg)
lprange <- range(vm$var.joint, vm$var1,vm$var2)

# Variance contribution of the components

csc <- scale_fill_gradientn(colours = brewer.pal(9,"YlOrRd"), limits = lprange)
boundary <- gorillas$boundary

plot.1 <- ggplot() + gg(gorillas$mesh, color = vm$var.joint, mask = boundary) + 
  csc + coord_equal() + ggtitle("joint") + theme(legend.position = "bottom")
plot.2 <- ggplot() + gg(gorillas$mesh, color = vm$var1, mask = boundary) + 
  csc + coord_equal() + ggtitle("SPDE") + theme(legend.position = "bottom")
plot.3 <- ggplot() + gg(gorillas$mesh, color = vm$var2, mask = boundary) + 
  csc + coord_equal() + ggtitle("vegetation") + theme(legend.position = "bottom")

multiplot(plot.1, plot.2, plot.3, cols = 3)

# Covariance of SPDE field and vegetation

ggplot() + gg(gorillas$mesh, color = vm$cov) 

# Correlation between field and vegetation

ggplot() + gg(gorillas$mesh, color = vm$cor) 

# Variance and correlation integrated over space

vm.int <- devel.cvmeasure(joint, field, veg, 
                          samplers = ipoints(gorillas$boundary, gorillas$mesh),
                          mesh = gorillas$mesh)
vm.int

}
}
}
\references{
Y. Yuan, F. E. Bachl, F. Lindgren, D. L. Brochers, J. B. Illian, S. T. Buckland, H. Rue, T. Gerrodette. 2017. 
Point process models for spatio-temporal distance sampling data from a large-scale survey of blue whales.
\url{https://arxiv.org/abs/1604.06013}
}
