EcologyR / mappestRisk

Other
1 stars 0 forks source link

Problem with [car::Boot()] in `predict_curves()` #50

Open dario-ssm opened 4 months ago

dario-ssm commented 4 months ago

I've just carefully checked what happened to the function. This allowed to fix some problems in the previous functions due to missing dependencies, so the example runs way better (more than 8 models were succesfully fitted rather than previously three).

However, there is a problem with predict_curves() function. It must be some kind of issue with car::Boot(). By loading the package, no bootstrapps are succesfully accomplished. In contrast, if I manually load the car package in my current session with library(car), then it works fine.

I don't really know how to solve this, since this Boot function is difficult to deal with (e.g., not saving in your function environment but requiring parent environment data).

dario-ssm commented 4 months ago

I have partially solved it by setting a require(car, silent = TRUE) within the function predict_curves() .

Until we get a better approach, this may solve the workflow problems.

Pakillo commented 3 months ago

Ok, but I think it should be requireNamespace instead. Anyway, let's try to revise and refactor this function

dario-ssm commented 3 months ago

I have been testing this function and it does not work with requireNamespace() but does with require(). I still don't know what's happening with the car package.

About refactoring this function, it has four clear steps:

  1. Build predictions based on the model.
  2. Perform the bootstrap
  3. Extract the estimates of the bootstrap
  4. Calculate one predicted value for each temperature and bootstrapped estimate.

The bottleneck is the step 4, which is used to predict the curves that work as confidence intervals. Anyway, we should find a way to refactor the function, I'm changing the label to enhance

dario-ssm commented 2 months ago

Automated testing failed consistently with this function. I have solved it with the following workaround:

1) Assign objects within the function to the global environment: as the car package authors suggest for some functions to work within other functions (see this documentation of the car package. Changing parent.frame() to .GlobEnv in the assign function solved the problem, as I misunderstood that parent.frame() would work similarly in testing environment.

2) load and attach car package within the predict_curves() function: As warned incar::Boot() function documentation, using this function with residual resampling, i.e., car::Boot(x, n_samples, "residual") requires the user to previously run library(car) . Note that this requirement does not apply for the more common case resampling (typical boostrap); i.e., car::Boot(x, n_samples, "case"). However, and following indications of Daniel Padfield in this rTPC vignette, the experimental designs originating typical data for TPC fitting, as in our case, allow to perform bootstrap with residual resampling, leading to narrower variation when there is only one repetition for each independent variable value (i.e., temperature treatment). Consequently, I have carefully read the chapter 10 of R Packages book, and I tried requireNamespace() and loadNamespace("car") but none of them work. It seems that the car package must be attached, so the only option (following the recommendation to avoid require("car") is to manually run library(car) within the function. This solves the complete problem.

Refactoring the function is still a task to do.

Pakillo commented 2 months ago

Thanks Dario. I'll try to have a look