Page 1 of 1

Optim Convergence 1, unrecognized capthist

PostPosted: Sun Oct 06, 2019 1:38 pm
by CHWarbington
Hello -
EDITED to add a secondary challenge -

I am running multiple candidate models to compare fit with AICc. Whenever I run a model allowing sigma to vary (e.g. sigma ~ Session, sigma ~ h2), R prints an error message: probable maximization error: optim returned convergence 1. See ?optim
I only get the message when specifying sigma in the model. I am using starting values, from a null model fit previously (without the error message). Here is sample code:

NL.Fullh2 <- secr.fit(s.data, mask = msk, method = 'Nelder-Mead', trace = FALSE, model = list(g0 ~ h2 + Session, sigma ~ h2 + Session, D ~ Session), start = NL.nullKM, details = list(knownmarks = FALSE))


My question is, how reliable is the output from a model with this error? Does anyone have suggestions for eliminating the error?
######################

The second issue involves secr.fit not recognizing the capthist object.
I run the following code:

fullcapt <- read.csv("FullID_sex.csv", header=TRUE)

traps <- read.traps("LinearTraps.csv", detector = "count", sep = ",",
binary.usage = FALSE)
# Trap Usage files for each year
t15 <- read.csv("TrapUsage2015.csv", header=TRUE)
t16 <- read.csv("TrapUsage2016.csv", header=TRUE)
t17 <- read.csv("TrapUsage2017.csv", header=TRUE)

s.data <- make.capthist(fullcapt, traps, covnames = list("Sex"),
fmt="trapID")

## Assign the trap usage lists
uselist <- list(t15, t16, t17)
# insert each usage matrix in relevant traps object
for (i in 1:3) usage(traps(s.data)[[i]]) <- uselist[[i]]
summary(traps(s.data))

But when I try to run a fit, such as:
L.null <- secr.fit(s.data, model = g0 ~ 1, mask = rivermask,
method = 'Nelder-Mead', trace = FALSE,
details = list(knownmarks = FALSE,
userdist = networkdistance(traps, rivermask)))


I get the error message:
Error in secr.fit(s.data, model = g0 ~ 1, mask = rivermask, method = "Nelder-Mead", :
requires 'capthist' object

I do not get the error when I omit the trap usage files.
What am I missing?
Thanks for your help.

Re: Optim Convergence 1, unrecognized capthist

PostPosted: Sun Oct 27, 2019 12:47 pm
by CHWarbington
Hello again -
I am still trying different starting values, yet I still get the optim 1 error.
My Main concern is whether the error precludes the usefulness of the results. I do not get this error if I omit the knownmarks = False command.

Should I use the version without the optim 1 error (and without known marks) or can some inference be gained from the models with the optim 1 error ?

Thanks

Re: Optim Convergence 1, unrecognized capthist

PostPosted: Wed Oct 30, 2019 1:28 pm
by CHWarbington
Hello once more -
I want to sum up the issues I am having, so if anyone can provide some guidance that the errors will be more clear.

I am working on a density estimate using secrlinear, meaning that the animals move along a network (river system in this case) and all activity centres are also on the river system.
I intend to do a model selection, varying g0, sigma, and density by Session, h2, and /or sex in different models and comparing AIC.

The animals are identifiable based on coat markings, hence knownmarks = FALSE is appropriate under the details part of the secr.fit command.

I am combining detections from camera traps and human observers, which vary in lengths of deployment. I also varied the location and active periods of the camera traps during different sessions, thus using a trap usage file / varying effort specification is also appropriate.

I originally fit the data omitting the trap usage files and also omitting knownmarks = FALSE. I fit a set of 10 candidate models, and all but one concluded without error. The model that failed was D ~ Session, sigma ~ h2, and the error was "at least one variance calculation failed."
thus I started fitting models with the varying effort and knownmarks = FALSE commands.

I ran into the unrecognized capthist object issue when trying to incorporate the trap usage files. That sequence of code and the subsequent error is related in the first post in this thread, copied here:

fullcapt <- read.csv("FullID_sex.csv", header=TRUE)

traps <- read.traps("LinearTraps.csv", detector = "count", sep = ",",
binary.usage = FALSE)
# Trap Usage files for each year
t15 <- read.csv("TrapUsage2015.csv", header=TRUE)
t16 <- read.csv("TrapUsage2016.csv", header=TRUE)
t17 <- read.csv("TrapUsage2017.csv", header=TRUE)

s.data <- make.capthist(fullcapt, traps, covnames = list("Sex"),
fmt="trapID")

## Assign the trap usage lists
uselist <- list(t15, t16, t17)
# insert each usage matrix in relevant traps object
for (i in 1:3) usage(traps(s.data)[[i]]) <- uselist[[i]]
summary(traps(s.data))

But when I try to run a fit, such as:
L.null <- secr.fit(s.data, model = g0 ~ 1, mask = rivermask,
method = 'Nelder-Mead', trace = FALSE,
details = list(knownmarks = FALSE,
userdist = networkdistance(traps, rivermask)))


I get the error message:
Error in secr.fit(s.data, model = g0 ~ 1, mask = rivermask, method = "Nelder-Mead", :
requires 'capthist' object

I do not get the error when I omit the trap usage files. (meaning skipping the uselist command and the for loop above)

This brings me to the second issue:
Using knownmarks - FALSE in secr.fit leads to optim returns convergence 1 errors, in models where I allow sigma to vary by a hybrid mixture model. I get this error even when specifying the starting values from models that were fit successfully.

I am only comparing the results within a "set", meaning all the models with knownmarks = FALSE are compared to each other, and only those omitting knownmarks are compared.

So, my questions are:
It appears that I am unable to use the trap usage files to signify the varying effort between sessions. Am I coding it incorrectly?
Due to the use of different types of detectors, can I omit the trap usage files and still have a useful result?
Is the optim convergence 1 a fatal error that precludes the usefulness of the results?
If it is, can I use the results from models that omit the knownmarks - FALSE command, or does this introduce more problems by ignoring the types of marks?

Also, I know that the first post contains code omitting the network distance. I fit the data using the network distance and "standard" secr, to compare results and density estimates only. I get the optim convergence 1 error in "standard" secr and secrlinear, so I did not change the code.
Another sample code that returns the optim 1 error is:
L.Fullh2_2 <- secr.fit(s.data, model = list(g0 ~ h2 + Session, sigma ~ h2 + Session,
+ D ~ Session),
+ mask = rivermask, method = 'Nelder-Mead', trace = FALSE,
+ details = list(knownmarks = FALSE,
+ userdist = networkdistance(traps, rivermask)),
+ start = L.Sex)

Where L.Sex was fit without the optim convergence 1 error and was coded thus:
L.Sex <- secr.fit(s.data, model = list(g0 ~ h2, sigma ~ h2), hcov = "Sex",
mask = rivermask, method = 'Nelder-Mead', trace = FALSE,
details = list(knownmarks = FALSE,
userdist = networkdistance(traps, rivermask)),
start = L.null)

Thank you so much for any response!

Re: Optim Convergence 1, unrecognized capthist

PostPosted: Thu Oct 31, 2019 3:40 pm
by murray.efford
So s.data is not a capthist object. It has been corrupted along the way. Replacing an attribute of an attribute in a list often trips me up. Try instead assigning the usage data with
Code: Select all
for (i in 1:3) usage(traps(s.data[[i]])) <- uselist[[i]]
although I'm not promising this will work (I use trial and error myself). Thoroughly check the result (verify, summary, plot). The form 'list("sex")' catches my eye - should that be 'c("Sex")'?

All-sighting models are vulnerable to some boundary constraints (e.g. Eqn 11 in Efford & Hunter 2018) and I wonder if that is the cause of the problem in your relatively complex model. Mixture models can also be tricky on their own.

Optim convergence 1 "indicates that the iteration limit maxit had been reached". Probably it means maximization is futile, but you can increase maxit in the secr.fit call if you like (i.e. the dots argument of secr.fit() allows you to set arguments for optim). And I assume you tried the default 'method'.

In general it would be good to keep separate topics in separate threads.
Murray