courtiol / IsoriX

This is the website dedicated to the development of the R package IsoriX
12 stars 5 forks source link

remove warnings in spaMM #115

Closed courtiol closed 11 months ago

courtiol commented 3 years ago

Perhaps some of the spaMM warnings should be removed as they confuse users, otherwise we need to indicate what they could do to avoid the situation creating the warnings.

Here is a first example:

> GNIPDataDEagg <- prepsources(data = GNIPDataDE)
> GermanFit <- isofit(data = GNIPDataDEagg,
+                     mean_model_fix = list(elev = TRUE, lat_abs = TRUE))
[1] Fitting the following residual dispersion model using spaMM  :
[1] "var_source_value ~ 1 + (1|source_ID) + Matern(1|long + lat)"
[1] (it may take a while...)
[1] Fitting the following mean model using spaMM  :
[1] "mean_source_value ~ 1 + elev + lat_abs + (1|source_ID) + Matern(1|long + lat)"
[1] (it may take a while...)
[1] Done!
[1] "Models were fitted in 13 sec."
> GermanScape <- isoscape(raster = ElevRasterDE,
+                         isofit = GermanFit)
[1] Building the isoscapes... 
[1] (this may take a while)
  |===========================================================================================| 100%
[1] predictions for all 12240 locations have been computed in 3 sec.
Warning message:
In .calc_logdisp_cov(object, dvdloglamMat = dvdloglamMat, dvdlogphiMat = dvdlogphiMat,  :
  Numerical precision issue in computation of the information matrix for dispersion parameters:
   the prediction variance may be inaccurate.
courtiol commented 3 years ago

Here is another example leading to the same message:

> GNIPDataDEagg <- prepsources(data = GNIPDataDE)
> GermanFit <- isofit(data = GNIPDataDEagg,
+                     mean_model_fix = list(elev = TRUE, lat_abs = TRUE))
> CalibAlien <- calibfit(data = CalibDataAlien, isofit = GermanFit)
[1] "predicting the isoscape value in each calibration site..."
Warning in .calc_logdisp_cov(object, dvdloglamMat = dvdloglamMat, dvdlogphiMat = dvdlogphiMat,  :
  Numerical precision issue in computation of the information matrix for dispersion parameters:
   the prediction variance may be inaccurate.
f-rousset commented 3 years ago

I prefer having confused users asking questions rather than having hidden persistent problems in the code.

To indicate what to do when the warning occurs, one needs to find the cause as it can have different causes calling for different actions. This is not straightforward in general; the next version of spaMM will (likely) include some improvements. In the present case the warning only results from a very low variance for one random effect (ca. 1e-10) meaning that there is no need to consider it, so one may ask whether we should keep this random effect in the prediction variance computations.

courtiol commented 1 year ago

Perhaps I should intercept expected warnings and only cancel those. I could use a wrapper for that, such as:


wrapper <-  function(x) {
  withCallingHandlers(x, warning = function(w) {
    if(!w[[1]] == "bla") warning(w[[1]], call. = FALSE)
    invokeRestart("muffleWarning")
  })
}

foo <- function(x, warn) {
  warning(warn)
  x
}

wrapper(foo(1, "bla"))
wrapper(foo(2, "bli"))

I would need to check what happens when multiple warnings occur...