# These examples demonstrate correct use of the corrMatrix argument
# by using it to fit a Matern spatial model and comparing the results
# to the result using the alternative basic Matern(1|.) syntax
# The first series of examples uses a simplified syntax that works
# only because each geographical location is represented only once
# in the data. This condition is relaxed afterwards.
data("blackcap")
# Fit to be reproduced by the different syntaxes:
fitme(migStatus ~ means+ Matern(1|longitude+latitude),data=blackcap,
fixed=list(nu=0.6285603,rho=0.0544659))
# Here we manually reconstruct the correlation matrix of this fit:
MLcorMat <- MaternCorr(proxy::dist(blackcap[,c("longitude","latitude")]),
nu=0.6285603,rho=0.0544659)
# (Crucially, rownames of 'blackcap' define the dimnames of 'MLcorMat')
# We create a factor ordered as geographical locations are ordered
# in the data, hence matching (in this simplified example)
# their order in the correlation matrix:
blackcap$name <- as.factor(rownames(blackcap))
fitme(migStatus ~ means+ corrMatrix(1|name),data=blackcap,
corrMatrix=MLcorMat)
# Correct results after permutation of the matrix:
# (here the 'dist' object has to be converted to 'matrix' to allow permutation).
perm <- sample(14)
pmat <- proxy::as.matrix(MLcorMat, diag=1)[perm,perm]
# Crucially, info about the permutation is provided
# by the dimnames of 'pmat' which allow a correct match
# to levels of the 'name' variable.
fitme(migStatus ~ means+ corrMatrix(1|name),data=blackcap,
corrMatrix=pmat)
# It is possible, but more risky, not to use a factor whose levels
# are specifically defined to match the dimnames of the matrix.
# Note the message if we don't use such a factor:
(nofactor <- fitme(migStatus ~ means+ corrMatrix(1|longitude+latitude),
data=blackcap, corrMatrix=MLcorMat))
#### Case with several samples in each location: ####
if (spaMM.getOption("example_maxtime")>0.7 &&
requireNamespace("IsoriX", quietly = TRUE)) {
# [the fits are quite fast, but requireNamespace("IsoriX"...)
# is too slow on CRAN servers].
data("GNIPDataDE", package = "IsoriX")
## Fit to be reproduced by the different syntaxes:
fitme(source_value ~ 1 + Matern(1|long+lat), data=GNIPDataDE,
fixed=list(nu=0.75, rho=0.2))
## Reconstruct the distance matrix of this fit:
dat <- GNIPDataDE
longlat <- paste0(dat$long,":",dat$lat)
# Use unique() to get unique geographical coordinates *and* unique names:
distmat <- as.matrix(dist(unique(dat[,c("long","lat")])))
dimnames(distmat) <- list(unique(longlat), unique(longlat))
# Define matching factor:
dat$ID <- factor(longlat)
## Various possible syntaxes for the fit (last one more general):
# corrMatrix(1|.) term + general 'covStruct' argument (see help("covStruct"))
fitme(source_value ~ 1 + corrMatrix(1|ID), data=dat,
covStruct=list(corrMatrix=MaternCorr(distmat,rho = 0.2, nu=0.75)))
# corrMatrix(1|.) term + more ad hoc 'corrMatrix' argument
fitme(source_value ~ 1 + corrMatrix(1|ID), data=dat,
corrMatrix=MaternCorr(distmat,rho = 0.2, nu=0.75))
# Matern(1|.) term + even more ad hoc 'distMatrix' argument
fitme(source_value ~ 1 + Matern(1|ID), data=dat,
distMatrix=distmat, fixed=list(rho = 0.2, nu=0.75))
# 'distMatrix' in covStruct=list(distMatrix=distmat) would not work,
# but a general syntax allowing multiple distance matrices is:
fitme(source_value ~ 1 + Matern(1|ID), data=dat,
distMatrix=list("1"=distmat), # more than one element might be specified
# covStruct=list(...), can be used if required for other random effects
fixed=list(corrPars=list("1"=list(rho = 0.2, nu=0.75)))
)
}
Run the code above in your browser using DataLab