Page 1 of 1

Linear trend for detection in RPresence

PostPosted: Mon Jul 29, 2024 10:25 pm
by nathan_whitmore
Hi there,

I've been trying to replicate my results from PRESENCE into RPresence, however I can't quite work out how to incorporate a linear trend via the design matrix for detection. When I look at the matrix from my model in which method.linear.trend is a design matrix incorporating a linear trend (in which the detection rate changes consistently with time):

meth.linear.trend <-occMod(model = list(psi~ 1, gamma ~ 1, epsilon ~ 1, p ~ -1 + method.linear.trend),
data = mydata, type = "do.1")

The tail of the model matrix shows an issue (simplified here as a dummy example):

P(1-1) "1" "0"
P(1-2) "1" "1"
P(1-3) "1" "p.method.linear.trendd3"
P(1-4) "1" "p.method.linear.trendd3"

I'm guessing the design matrix is now being handled as factors rather than as continuous variables (where the numbers 2 and 3 understandably become problematic). In such a case what is the correct way to handle the coding for the linear trend?

Re: Linear trend for detection in RPresence

PostPosted: Mon Jul 29, 2024 11:04 pm
by jhines
Hi Nathan,

RPresence automatically builds a covariate named 'SURVEY', which is categorical. You can create a continuous covariate with:

srvy <- matrix(rep(1:mydata$nsurveys,each=mydata$nunits),nrow=mydata$nsurveys)

This creates an NxK matrix (N=nsites, K=nsurveys) with the survey number in each column. You can then turn it into a data.frame and use it in the occMod function:

meth.linear.trend <-occMod(model = list(psi~ 1, gamma ~ 1, epsilon ~ 1, p ~ -1 + trend),
data = mydata, cov.list=list(p.cov=data.frame(trend=as.vector(srvy))), type = "do.1")

Alternatively, you can add the covariate to any survey covariates you already have:

mydata$survcov$trend = as.vector(srvy)

Then, you wouldn't need the cov.list argument in the occMod function call.

Jim

Re: Linear trend for detection in RPresence

PostPosted: Mon Jul 29, 2024 11:09 pm
by jhines
One more thing... the design matrix will have covariate names like the ones you see in your example, but any covariate which equal zero is replaced with "0" in the design matrix. Similarly for covariates which are always 1 will be replaced with "1" in the design matrix.

Re: Linear trend for detection in RPresence

PostPosted: Tue Jul 30, 2024 1:54 am
by nathan_whitmore
Hi Jim,

Thanks very much for help on this. Thanks to your explanation I've successfully replicated the model now.

However, I noticed that in your code your inadvertently suggested nrow =mydata$nsurveys. Just incase anyone else strikes the problem it should of course be nrow = mydata$nunits given it is an NxK matrix (N=nsites, K=nsurveys). So the magic piece of code is:

srvy <- matrix(rep(1:mydata$nsurveys, each=mydata$nunits), nrow=mydata$nunits)

Cheers,
Nathan