bcgov / ssdtools

An R package to fit and plot Species Sensitivity Distributions (SSDs)
https://bcgov.github.io/ssdtools/
Apache License 2.0
30 stars 16 forks source link

Implement `ssd_fit_dists()` with right censored data #207

Open joethorley opened 3 years ago

joethorley commented 3 years ago
  library(ssdtools)
  data <- ssdtools::boron_data

  data$right <- data$Conc
  data$right[1] <- Inf

  ssd_fit_dists(data, dists = "lnorm", right = "right")
#> Error: Distributions cannot currently be fitted to right censored data.

Created on 2021-08-02 by the reprex package (v2.0.0)

joethorley commented 3 years ago

Note if switch off check then get the following

  library(ssdtools)
  data <- ssdtools::boron_data

  data$right <- data$Conc
  data$right[1] <- Inf

  ssd_fit_dists(data, dists = "lnorm", right = "right")
#> Warning: Distribution 'lnorm' failed to fit (try rescaling data): Error in optim(model$par, model$fn, model$gr, method = "L-BFGS-B", lower = lower,  : 
#>   non-finite value supplied by optim
#> .
#> Error: All distributions failed to fit.

Created on 2021-08-02 by the reprex package (v2.0.0)

joethorley commented 3 years ago

The issue is handling the infinite values in the TMB C++ code.

Really just need to replace existing code

pright = pnorm(log(right(i)), meanlog, sdlog);

with

pright = 1;
if(isfinite(right(i))){ pright=pnorm(log(right(i)), meanlog, sdlog);};
beckyfisher commented 1 year ago

Hi Joe, I'm playing around with a data set with right censored values, is this still on the list of things to implement?

joethorley commented 1 year ago

Yes it is. I'll try and find some time to scope out how much time it would take and see whether the maintenance budget would cover it. What are your time scales?