# [R] Zero distance error in corSpatial - correlation structure using lme

John Poulsen jpoulsen at zoo.ufl.edu
Mon Mar 9 14:28:25 CET 2009

```Hello,

I am having a problem specifying the correlation structure in lme which
leads to the error: Error in getCovariate.corSpatial(object, data =
data) : Cannot have zero distances in "corSpatial".  I have specified a
grouping variable which is the only fix I could find by searching this
error on R-help.

ISee the below example.  When my samples (tran) - which are transects
sampled several times - have different coordinates (STX1 and STY1),
everything seems to work fine.  But in my actual data set, there are
multiple observations per sample and the coordinates are the same for
the same sample.  This is demonstrated below using the coordinates STX2
and STY2, which are the same for a single transect.  How can I properly
specify this structure in Variogram() and corSpatial()?

library(nlme)

set.seed(1001)
ntrans <- 30
npertrans <- 5
ntot <- ntrans*npertrans
STX1<-runif(ntot, min=16.0921, max=16.4823)
STY1<-runif(ntot, min=1.9812, max=2.3932)
STX2<-rep(runif(ntrans, min=16.0921, max=16.4823), each=npertrans)
STY2<-rep(runif(ntrans, min=1.9812, max=2.3932), each=npertrans)
site<-rep(c(1,2,3), each=ntot/3)
tran<-rep(1:ntrans,each=npertrans)
log<-rep(rbinom(n=ntrans, 1, 0.5), each=npertrans)
yabund<-rnorm(ntot, 5, 0.4)
dat<-data.frame(STX1, STY1, STX2, STY2, site, tran, log, yabund)

test1<-lme(yabund~factor(log)+STX1+STY1, random=~1|tran,data=dat)
test2<-lme(yabund~factor(log)+STX2+STY2, random=~1|tran,data=dat)

Variogram(test1, form=~STX1+STY1|tran)
plot(Variogram(test1, form=~STX1+STY1|tran))

Variogram(test2, form=~STX2+STY2|tran)   # Varigoram has distance of 0!
plot(Variogram(test2, form=~STX2+STY2|tran))

mod1<-lme(log(yabund)~factor(log)+STX1+STY1, random=~1|tran, data=dat,
corr=corSpatial(form=~STY1+STX1|tran, type="exponential",
nugget=T), method="REML")

mod2<-lme(log(yabund)~factor(log)+STX2+STY2, random=~1|tran, data=dat,
corr=corSpatial(form=~STY2+STX2, type="exponential",
nugget=T), method="REML")

# Error in getCovariate.corSpatial(object, data = data) :
# Cannot have zero distances in "corSpatial"

```