##First create some random locations
x <- rnorm(3)
y <- rnorm(3)
##compute distance matrix
D <- as.matrix(dist( cbind(x,y) ))
##create a trend
trend <- cbind(rep(1,5),sin(1:5))
##an index of locations
idx <- c(rep(1:3,3),1:2,2:3)
##expand the F matrix to match the locations/times in idx/T.
F <- trend[c(rep(1:3,each=3),4,4,5,5),]
##compute F*sigmaB*F'
FsigmaF <- make.sigma.B.full(c(2,1), c(.3,1), loc.ind1=idx,
F1=F, dists=D)
##create the full F matrix
Falt <- matrix(0,dim(F)[1],dim(F)[2]*max(idx))
for(i in 1:dim(F)[1]){
Falt[i,c(idx[i],idx[i]+max(idx))] <- F[i,]
}
##and compute F*sigmaB*F' using full matrices
sigma.B <- make.sigma.B(c(2,1), c(.3,1), D)
FsigmaF.alt <- Falt %*% sigma.B %*% t(Falt)
##compare the results
range(FsigmaF - FsigmaF.alt)
if( max(abs(FsigmaF - FsigmaF.alt)) > 1e-10 ){
stop("make.sigma.B.full: Results not equal.")
}
##use the function to compute a cross covariance
FsigmaF.cross <- make.sigma.B.full(c(2,1), c(.3,1), loc.ind1=idx[1:6],
loc.ind2=idx[7:13], F1=F[1:6,], F2=F[7:13,], dists=D)
##compare to the relevant part of the full matrix
range(FsigmaF.cross-FsigmaF[1:6,7:13])
if( max(abs(FsigmaF.cross-FsigmaF[1:6,7:13])) > 1e-10 ){
stop("make.sigma.B.full: Results not equal - cross-covariance")
}
Run the code above in your browser using DataLab