Lapack exactly singular issue / question

117 views
Skip to first unread message

Ben Padilla

unread,
Apr 15, 2025, 3:59:45 PM4/15/25
to oSCR
Hi oSCR crew 

Question for you all about a large dataset my colleagues and I are working on. The basics of the data are that we have multiple regions (management units) that were sampled at varying intervals over the course of 10 years. When a unit was surveyed there were 5-7 grids surveyed using scat dogs. We have done single unit-year analyses and are now trying to combine it all into a comprehensive analysis that includes all units over all years.

I've been working on a subset of the data from a single unit that was sampled over 4 consecutive years. Using each grid-year combination as a session we end up with 28 sessions over 4 years and the model fits fine, albeit slowly. I can even fit this with spatial covariates and sex specific p0 and sigma. 

In all of our data the number of grid-year-wmu sessions would be well more than 100. So, I tried to build a model where all the grids in a given unit-year is the session. I can build the model objects with data2oscr and state space objects. When fitting the NULL ~1 ~1 ~1 model with these objects I run into a singularity error (Lapack routine dgesv: system is exactly singular: u[1,1] = 0). This is the same dataset used to fit the model described above just with the sessions described differently. Is there any reason why multiple grids surveyed over a ~2 week period couldnt be considered a single session? 

If you have any other suggestions on how to efficiently analyze this dataset I would love to hear it. Thanks! 
Ben 

Daniel Linden

unread,
Apr 16, 2025, 8:55:51 AM4/16/25
to oscr_p...@googlegroups.com
Hey Ben, conceptually you should be able to define a session however you want.  Your strategy here makes sense.  I assume you don't have individuals that were observed in multiple grids?  If the individual grid-year sessions worked, then something about the combination is causing this error.  I might double check the naming conventions, particularly with traps and individuals.  It could be that something is getting sorted in a strange way.

Can you post a figure of the combined state space and traps?  Just curious about the layout.

--
You received this message because you are subscribed to the Google Groups "oSCR" group.
To unsubscribe from this group and stop receiving emails from it, send an email to oscr_package...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/oscr_package/bbd829b2-ea65-41c7-9273-e229949346c8n%40googlegroups.com.

Ben Padilla

unread,
Apr 16, 2025, 2:34:13 PM4/16/25
to oscr_p...@googlegroups.com
Hi Dan, 
We arent entirely sure about individuals across grids. Across all of our data out of more than 15,000 individuals there are 66 that were detected at >1 grid, but the distances between those grids and the timings of surveys has me questioning whether there was a lab error there. To make double sure all the naming conventions and everything were getting sorted properly I added unit-year-grid to each trap and individual ID. Correcting for individuals got me past the Lapack routine error, but instead I ran into "Error in nlm" due to non-finite value supplied to nlm. This is a model fit using starting values derived from using the getStarts=TRUE argument. Below is a plot of one year's surveys in one management unit, where each of those squares is approx a3x3 km2 surveyed by scat dog teams. Then below it shows the spatial scale of our sampling frame. This is really an analysis that needs random effects for the management units, but I was having trouble figuring how to code this model in nimble given the complexity of the state space. oSCR has been working well and I think/hope we can make it work. 
Thanks for the help
Ben

image.png
image.png


Benjamin Padilla Ph.D
Wildlife Research Supervisor
Oregon Department of Fish and Wildlife
Twitter: @bpdilla ~ Web: www.benpadilla.weebly.com



You received this message because you are subscribed to a topic in the Google Groups "oSCR" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/oscr_package/etRrlDR_W8Y/unsubscribe.
To unsubscribe from this group and all its topics, send an email to oscr_package...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/oscr_package/CAFWOE77edQsbOPt_Tqd%2B8P%2BobyPdg6BGTsh4XiTFKBeSsyGftA%40mail.gmail.com.

Daniel Linden

unread,
Apr 17, 2025, 3:24:59 PM4/17/25
to oscr_p...@googlegroups.com
Have you scaled your distance units?  I think starting values become more important when you have a lot of data like you do here, and with unscaled distance units even more so.  Definitely a tricky problem to troubleshoot though.  I could always give it a go if you send along your data/code.

Ben Padilla

unread,
Apr 17, 2025, 5:21:24 PM4/17/25
to oscr_p...@googlegroups.com
We havent scaled the spatial data, that is something I always forget to do! What is your recommendation for best approach? In the past I've generally just reduced the utm coordinates from meters to km or something by moving the decimal just so that the numbers are closer to zero and easier to handle computationally. Or, would you recommend a scale and center transformation? 

Seems like we are making some progress with getting the full dataset together and a null model is spinning without propagating errors yet. Hopefully it doesnt run for 10 hours then run into a singularity! 

Thanks for the help. 


Benjamin Padilla Ph.D
Wildlife Research Supervisor
Oregon Department of Fish and Wildlife


Daniel Linden

unread,
Apr 17, 2025, 10:31:09 PM4/17/25
to oscr_p...@googlegroups.com, oscr_p...@googlegroups.com
It mostly depends on what sigma might be, but usually having distance units in km instead of m is enough. When the log-scale model coefficients are small single digits, the optimization tends to work better.

So yeah, making sure your coordinates for the state space and trap locations are in km is all you probably need to do.

Glad you’ve got something working, or at least not breaking quickly!



On Apr 17, 2025, at 5:21 PM, Ben Padilla <benjamin...@gmail.com> wrote:


We havent scaled the spatial data, that is something I always forget to do! What is your recommendation for best approach? In the past I've generally just reduced the utm coordinates from meters to km or something by moving the decimal just so that the numbers are closer to zero and easier to handle computationally. Or, would you recommend a scale and center transformation? 

Seems like we are making some progress with getting the full dataset together and a null model is spinning without propagating errors yet. Hopefully it doesnt run for 10 hours then run into a singularity! 

Thanks for the help. 


Benjamin Padilla Ph.D
Wildlife Research Supervisor
Oregon Department of Fish and Wildlife
Web: www.benpadilla.weebly.com



On Thu, Apr 17, 2025 at 12:25 PM Daniel Linden <danl...@gmail.com> wrote:
Have you scaled your distance units?  I think starting values become more important when you have a lot of data like you do here, and with unscaled distance units even more so.  Definitely a tricky problem to troubleshoot though.  I could always give it a go if you send along your data/code.

On Wed, Apr 16, 2025 at 2:34 PM Ben Padilla <benjamin...@gmail.com> wrote:
Hi Dan, 
We arent entirely sure about individuals across grids. Across all of our data out of more than 15,000 individuals there are 66 that were detected at >1 grid, but the distances between those grids and the timings of surveys has me questioning whether there was a lab error there. To make double sure all the naming conventions and everything were getting sorted properly I added unit-year-grid to each trap and individual ID. Correcting for individuals got me past the Lapack routine error, but instead I ran into "Error in nlm" due to non-finite value supplied to nlm. This is a model fit using starting values derived from using the getStarts=TRUE argument. Below is a plot of one year's surveys in one management unit, where each of those squares is approx a3x3 km2 surveyed by scat dog teams. Then below it shows the spatial scale of our sampling frame. This is really an analysis that needs random effects for the management units, but I was having trouble figuring how to code this model in nimble given the complexity of the state space. oSCR has been working well and I think/hope we can make it work. 
Thanks for the help
Ben

<image.png>

Chris Sutherland

unread,
Apr 18, 2025, 2:58:57 AM4/18/25
to oscr_p...@googlegroups.com

Sorry to be late to the party here.

With such a large data set, there are a few things I would do:

1. Fit a bunch of single session models to get a sense of the session-specific variability in parameters and identify any quirks.

2. Do this with a relatively coarse resolution state space and collapsed occasions (with Poisson encounters) at first for computational efficiency.

3. Then, once satisfied all is good, start assembling the MS model using informed start values from steps 1 & 2 which will speed up model fitting.

Hope that's of some use. By the way, it's an extremely decent looking data set. Once you have the model up and running, I can see loadsa potential for cool questions! Good luck with it all.


Ben Padilla

unread,
Apr 18, 2025, 2:57:20 PM4/18/25
to oscr_p...@googlegroups.com
Hi Chris, 
No need to apologize, welcome to the party! 

These are all great ideas. We have fit all the single year-unit models with and without covariates, great idea to use those as starting values for the full model, I hadnt thought of that. We will give these suggestions a try. And yes, this is a great dataset with a lot of potential. It took a lot of work to get everything sorted out and in working order but it will be well worth the efforts in the end! 
Ben


Benjamin Padilla Ph.D
Wildlife Research Supervisor
Oregon Department of Fish and Wildlife


Ben Padilla

unread,
May 5, 2025, 6:14:10 PM5/5/25
to oSCR
Hi Chris and Dan, 
A follow up question here about running in parallel. We have some code to fit our models in parallel using foreach and doParallel. I'm pretty sure it is essentially what is going on under the hood in the oscr.parfit function. But, based on this thread this effectively runs each model in the list on a different core, but doesnt speed up runs of a given model by spread it out across multiple cores. 
We have a long list of models we'd like to fit, so spreading them out across multiple cores is great, but each individual model will still take a week or more at best. We are using scaled state space, covariates, and starting values (from getStarts). Do you know if there is a way to spread a single model across multiple cores with the way the oSCR.fit function works? 
Thanks for the help! 
Ben

Daniel Linden

unread,
May 6, 2025, 8:52:10 AM5/6/25
to oscr_p...@googlegroups.com
Hi Ben, we use "nlm" for the likelihood minimization process and I do not know of any parallel versions.  Technically, we could use "optim" instead and there is a parallelized version of that ("optimParallel").  That would require getting into the guts of the oSCR.fit function, which is not impossible.  The code is explicit and designed to allow folks to poke into once you understand the steps.  But this would not be an insignificant venture...

The other option is to make sure that your state space resolution is as coarse as possible, to reduce the computation time.  This kind of tuning can be tricky and you'd probably have to settle for an approximation to make it worthwhile (i.e., having a state space that is a little too coarse).

Víctor Masías Hinojosa

unread,
May 6, 2025, 11:46:57 PM5/6/25
to oscr_p...@googlegroups.com
Hi,

Its not tested but its close to replace "nlm" by optimParallel.

Best.


oSCR.fit <-
function (model = list(D ~ 1, p0 ~ 1, sig ~ 1, asu ~1), scrFrame, ssDF,
          encmod = c("B", "P", "CLOG","M")[1], multicatch = FALSE, theta = 2,
          trimS = NULL, DorN = c("D", "N")[1], sexmod = c("constant", "session")[1],
          costDF = NULL, distmet = c("euc", "user", "ecol")[1], directions = 8,
          PROJ = NULL, rsfDF = NULL, RSF = FALSE, telemetry.type = c("none","ind","dep")[1],
          se = TRUE, predict = FALSE, start.vals = NULL, getStarts = FALSE, pxArea = 1,
          plotit = F, mycex = 1, nlmgradtol = 1e-06, nlmstepmax = 10, nlmiterlim = 200,
          smallslow = FALSE, print.level = 0){
 
  # Load optimParallel package
  if (!require(optimParallel, quietly = TRUE))
    stop("need to install package 'optimParallel'")
 
  # Rest of the original code remains the same until the optimization part
 
  # ... [previous code library(parallel)
library(optimParallel)

oSCR.fit <- function(model = list(D ~ 1, p0 ~ 1, sig ~ 1, asu ~ 1), scrFrame, ssDF,
          encmod = c("B", "P", "CLOG","M")[1], multicatch = FALSE, theta = 2,
          trimS = NULL, DorN = c("D", "N")[1], sexmod = c("constant", "session")[1],
          costDF = NULL, distmet = c("euc", "user", "ecol")[1], directions = 8,
          PROJ = NULL, rsfDF = NULL, RSF = FALSE, telemetry.type = c("none","ind","dep")[1],
          se = TRUE, predict = FALSE, start.vals = NULL, getStarts = FALSE, pxArea = 1,
          plotit = FALSE, mycex = 1, nlmgradtol = 1e-06, nlmstepmax = 10, nlmiterlim = 200,
          smallslow = FALSE, print.level = 0) {

    if (!requireNamespace("optimParallel", quietly = TRUE))
        stop("Package 'optimParallel' is required. Install it using install.packages('optimParallel')")

    # If optimization is required
    if (!getStarts && !predict) {
        message("Fitting model: D", paste(model)[1],
                            ", p0", paste(model)[2],
                         ", sigma", paste(model)[3],
                           ", asu", paste(model)[4], sep = " ")
        if (!anySex) {
            if (telem) {
                message("Telemetry: ", telemetry.type)
            }
            message("Using likelihood function 'msLL.nosex' \nHold on tight!")
            message(Sys.time())
            message(paste(pn, " ", sep = " | "))
            message(" ")

            # OptimParallel Implementation
            cl <- makeCluster(detectCores())
            setDefaultCluster(cl = cl)
            myfit <- suppressWarnings(
                optimParallel(par = pv, fn = msLL.nosex, pn = pn, D = D, nG = nG, nK = nK,
                    hiK = hiK, dm.den = dm.den, dm.trap = dm.trap,
                    hessian = hessian, control = list(maxit = nlmiterlim, trace = TRUE),
                    method = "BFGS"))
            stopCluster(cl)
        } else {
            if (telem) {
                message("Telemetry: ", telemetry.type)
            }
            message("Using likelihood function 'msLL.sex' \nHold on tight!")
            message(Sys.time())
            message(paste(pn, " ", sep = " | "))
            message(" ")

            # OptimParallel Implementation
            cl <- makeCluster(detectCores())
            setDefaultCluster(cl = cl)
            myfit <- suppressWarnings(
                optimParallel(par = pv, fn = msLL.sex, scrFrame = scrFrame, pn = pn, D = D, nG = nG, nK = nK,
                    hiK = hiK, dm.den = dm.den, dm.trap = dm.trap,
                    hessian = hessian, control = list(maxit = nlmiterlim, trace = TRUE),
                    method = "BFGS"))
            stopCluster(cl)
        }

        # Adjust output to match nlm structure
        myfit$estimate <- myfit$par
        myfit$minimum <- myfit$value
    }
}



--

Víctor Masías Hinojosa
(Cargo) - (Unidad)
vma...@fen.uchile.cl 
Teléfono: +56 2 297x xxxx | Móvil +56 9xxxx xxxx

Universidad de Chile, Facultad de Economía y Negocios
Diagonal Paraguay 257 - Torre 26


fen.uchile.cl
Por un Campus Sustentable, no imprima este correo si no es realmente necesario.
En FEN vivimos la sustentabilidad.

Ben Padilla

unread,
May 7, 2025, 8:49:55 PM5/7/25
to oscr_p...@googlegroups.com
Hi Victor, 
Thanks for sharing. I'll have to have a look at this and test it out. Have you tested it out at all? Did it decrease run times? 
Ben


Benjamin Padilla Ph.D
Wildlife Research Supervisor
Oregon Department of Fish and Wildlife

Ben Padilla

unread,
Jun 9, 2025, 2:42:15 PM6/9/25
to oSCR
Hi all, 

Quick follow up question regarding run time and when we should think about checking on things. We have 10 models that have been running simultaneously on 10 cores using parallel and lapply for 4 weeks now. As noted above, it is a large dataset, but we've scaled coordinates, added starting values, etc. to try and speed things up. We've also reduced the model complexity as much as we can. Our most complex model for density includes session (17 sessions), and an interaction between canopy cover and land ownership (3 categories). No model has more than 3 covariates for density (screenshot of our model list attached). Is >4 weeks a reasonable run time for this set of models? Presumably each one is running at the same time on a unique core so we are just limited by the slowest of the bunch. 

If we stop the process will all progress be lost? I understand that this is more a question of how the parallelization is working rather than oSCR itself, but curious if you have any experience? 

Thanks. 
Ben
image (6).png

Daniel Linden

unread,
Jun 9, 2025, 3:25:13 PM6/9/25
to oscr_p...@googlegroups.com
Hey Ben, it can be really hard to tell how long the models will take to fit.  It depends on many things, from the spatial resolution to how well your starting values are able to reduce the iterations needed for optimization.

My best advice is to try a version with a very coarse state space, much coarser than is ideal.  Or fitting a model for half the survey area, etc.  Such exploratory attempts can give you some idea of how computational time increases with finer resolutions.  A model with <500 pixels in the state space is usually pretty fast but you would need to test.  I would keep reducing the resolution until you know what you're working with.

Unfortunately, stopping the model fitting will lose everything as far as I know.  Similar to MCMC, you would need some data writing mechanism embedded in your function to get "current values" saved somewhere.  Otherwise, the progress is lost.

Ben Padilla

unread,
Jun 10, 2025, 3:15:33 PM6/10/25
to oscr_p...@googlegroups.com
Hi Dan, 
Thanks for the idea, we'll try that. We had already coarsened the resolution, but clearly not as much as we should have. We are testing out some coarse resolution models. 
Ben


Benjamin Padilla Ph.D
Wildlife Research Supervisor
Oregon Department of Fish and Wildlife

Twitter: @bpdilla ~ Web: www.benpadilla.weebly.com

Reply all
Reply to author
Forward
0 new messages