Ticket #308 (closed Defect: fixed)
When a GAM uses the loess function (lo) and is fitted with the R gam package, Predict GAM fails when rasters include NODATA values
| Reported by: | jjr8 | Owned by: | jjr8 |
|---|---|---|---|
| Priority: | Medium | Milestone: | 0.7 |
| Component: | Tools - Statistics | Version: | |
| Keywords: | Cc: |
Description (last modified by jjr8) (diff)
An example formula that uses the loess function is:
presence ~ lo(mlfor) + lo(dfor2) + lo(preyall) + lo(insolindex)
The gam package's predict.gam function includes a parameter called na.action. The documentation for this parameter states "function determining what should be done with missing values in 'newdata'. The default is to predict 'NA'." In other words, if one of the predictors is NA, the predicted response will be NA. This does not seem to be true. Instead you get this error (from the MGET tool):
RPy_RException: Error in gam.lo(data[["lo(mlfor)"]], z, w, span = 0.5, degree = 1, ncols = 1, : NA/NaN/Inf in foreign function call (arg 6)
The full R traceback is:
11: .Fortran("lowese", fit$iv, liv, lv, fit$v, m, xeval, s = double(m),
PACKAGE = "gam")
10: gam.lo(data[["lo(mlfor)"]], z, w, span = 0.5, degree = 1, ncols = 1,
xeval = smooth.frame[["lo(mlfor)"]])
9: eval(expr, envir, enclos)
8: eval(Call)
7: newdata.predict.gam(object, newdata, type, dispersion, se.fit,
na.action, terms, ...)
6: predict.gam(fittedLM, newdata = grids@data, type = "response")
5: predict(fittedLM, newdata = grids@data, type = "response")
4: as.vector(predict(fittedLM, newdata = grids@data, type = "response"))
3: PredictLMForArcGISRasters(model, rastersForPredictors, "D:\\gis\\projects\\usfws_fisher\\analysis\\k2c\\grids\\gam\\models\\GeoEcoTemp\\tmp37u98a\\temp_response.txt",
ignoreOutOfRangeValues = TRUE, chunks = NULL)
2: eval(expr, envir, enclos)
1: function (expr, envir = parent.frame(), enclos = if (is.list(envir) ||
is.pairlist(envir)) parent.frame() else baseenv())
.Internal(eval(expr, envir, enclos))(expression(PredictLMForArcGISRasters(model,
rastersForPredictors, "D:\\gis\\projects\\usfws_fisher\\analysis\\k2c\\grids\\gam\\models\\GeoEcoTemp\\tmp37u98a\\temp_response.txt",
ignoreOutOfRangeValues = TRUE, chunks = NULL)))
None
At the deepest level, the problem is that gam.lo does not remove NA values before calling the "lowese" function, impelmented in Fortran. R has a rule that you cannot pass NA, NaN, or Inf values to a function implemented in Fortran or C. This seems to be a bug in gam.lo.
To work around this, MGET should just not pass NA values to predict.gam.
Thanks to Dave LaPlante for reporting this problem.
