set.seed(1)
n <- 100 ##number of coords
m <- 25 ##number of knots
q <- 5 ##number of response variables
coords <- cbind(runif(n),runif(n))
knots <- cbind(runif(m), runif(m))
theta <- c(rep(6,q),rep(0.5,q)) #phi and nu
A <- matrix(0,q,q)
A[lower.tri(A,TRUE)] <- rnorm(q*(q-1)/2+q)
V <- A%*%t(A)
P <- matrix(0,q,q)
P[lower.tri(A,TRUE)] <- rnorm(q*(q-1)/2+q)
Psi <- P%*%t(P)
tol <- 1.0e-8
##
##non bias-adjusted predictive process
##
c1 <- mvCovInvLogDet(coords=coords, knots=knots, cov.model="exponential",
V=V, Psi=Psi, theta=theta,
modified.pp=FALSE, SWM=FALSE)
if(max(abs(chol2inv(chol(c1$C)) - c1$C.inv)) > tol) stop("test-1 failed")
if(max(abs(2*sum(log(diag(chol(c1$C)))) - c1$log.det)) > tol)
stop("test-1 failed")
c2 <- mvCovInvLogDet(coords=coords, knots=knots, cov.model="matern",
V=V, Psi=Psi, theta=theta,
modified.pp=FALSE, SWM=FALSE)
if(max(abs(chol2inv(chol(c2$C)) - c2$C.inv)) > tol) stop("test-2 failed")
if(max(abs(2*sum(log(diag(chol(c2$C)))) - c2$log.det)) > tol)
stop("test-2 failed")
##
##bias-adjusted predictive process
##
c3 <- mvCovInvLogDet(coords=coords, knots=knots, cov.model="exponential",
V=V, Psi=Psi, theta=theta,
modified.pp=TRUE, SWM=FALSE)
if(max(abs(chol2inv(chol(c3$C)) - c3$C.inv)) > tol) stop("test-3 failed")
if(max(abs(2*sum(log(diag(chol(c3$C)))) - c3$log.det)) > tol)
stop("test-3 failed")
c4 <- mvCovInvLogDet(coords=coords, knots=knots, cov.model="matern",
V=V, Psi=Psi, theta=theta,
modified.pp=TRUE, SWM=FALSE)
if(max(abs(chol2inv(chol(c4$C)) - c4$C.inv)) > tol) stop("test-4 failed")
if(max(abs(2*sum(log(diag(chol(c4$C)))) - c4$log.det)) > tol)
stop("test-4 failed")
##
##non bias-adjusted predictive process using SWM
##
c5 <- mvCovInvLogDet(coords=coords, knots=knots, cov.model="exponential",
V=V, Psi=Psi, theta=theta,
modified.pp=FALSE, SWM=TRUE)
if(max(abs(chol2inv(chol(c5$C)) - c5$C.inv)) > tol) stop("test-5 failed")
if(max(abs(2*sum(log(diag(chol(c5$C)))) - c5$log.det)) > tol)
stop("test-5 failed")
c6 <- mvCovInvLogDet(coords=coords, knots=knots, cov.model="matern",
V=V, Psi=Psi, theta=theta,
modified.pp=FALSE, SWM=TRUE)
if(max(abs(chol2inv(chol(c6$C)) - c6$C.inv)) > tol) stop("test-6 failed")
if(max(abs(2*sum(log(diag(chol(c6$C)))) - c6$log.det)) > tol)
stop("test-6 failed")
##
##bias-adjusted predictive process using SWM
##
c7 <- mvCovInvLogDet(coords=coords, knots=knots, cov.model="exponential",
V=V, Psi=Psi, theta=theta,
modified.pp=TRUE, SWM=TRUE)
if(max(abs(chol2inv(chol(c7$C)) - c7$C.inv)) > tol) stop("test-7 failed")
if(max(abs(2*sum(log(diag(chol(c7$C)))) - c7$log.det)) > tol)
stop("test-7 failed")
c8 <- mvCovInvLogDet(coords=coords, knots=knots, cov.model="matern",
V=V, Psi=Psi, theta=theta,
modified.pp=TRUE, SWM=TRUE)
if(max(abs(chol2inv(chol(c8$C)) - c8$C.inv)) > tol) stop("test-8 failed")
if(max(abs(2*sum(log(diag(chol(c8$C)))) - c8$log.det)) > tol)
stop("test-8 failed")
Run the code above in your browser using DataLab