Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Environmental problems while predicting from gaulss-gams with a custom variance function inside a package

$
0
0

A variance function available within an R package is not found by the predict function while doing predictions from a gam-object constructed previously (mgcv 1.8-31).

The R package shall (amongst other things) predict from gam-objects. All of the previously constructed models use the gaulss-family and have their own variance function. Some variance functions are just a linear effect of a variable, others use more sophisticated custom functions. The models and variance functions were stored in a 'sysdata.rda' file to include them in the package. The package was documented with devtools and roxygen2.

Consider the following minimal example of two GAM's:

library(mgcv)

set.seed(123)

var.fun <- function(x){x^2}

x <- runif(100)
y <- x + rnorm(100, 0, var.fun(x))

mod.gam.1 <- gam(formula = list(y ~ x,
                                ~ var.fun(x)),
                 family = gaulss(link = list("log", "logb")))

mod.gam.2 <- gam(formula = list(y ~ x,
                                ~ I(x^2)),
                 family = gaulss(link = list("log", "logb")))

The first model uses a custom variance function. The second model has the variance formula hardcoded in the model call.

The models and the variance function are then stored in a 'sysdata.rda' file, which is included in a package named "gamvarfun" (I know, naming things... ), as follows:

save(mod.gam.1, var.fun, mod.gam.2,
     file = "~/gamvarfun/R/sysdata.rda")

Now two functions are added to the package to retrieve predictions from the corresponding models:

pred.fun.1 <- function(x){
  predict(mod.gam.1,
          newdata = data.frame("x" = x))
}

pred.fun.2 <- function(x){
  predict(mod.gam.2,
          newdata = data.frame("x" = x))
}

To illustrate the problem I've uploaded a very rudimentary demo-package to github, so the following code should work:

library(devtools)
install_github("jan-schick/gam_issue")
library(gamvarfun)

pred.fun.2(1)
# 0.3135275 0.5443409

pred.fun.1(1)
# Error in var.fun(x) : could not find function "var.fun"

# Writing var.fun to global environment
var.fun <- gamvarfun:::var.fun
pred.fun.1(1)
# 0.3135275 0.5443409

When using pred.fun.1 (containing a custom variance function), an error is displayed. However, pred.fun.2 (hardcoded variance function) works perfectly well. This error does not occur, when devtools::load_all() is used instead of a 'proper' install of the package. I suspected that the problem is due to different environments when using predict.gam. I tested this assumption by writing the custom variance function to the global environment before calling pred.fun.1 (see above), which worked. However, this is obviously no solution for a package.

In earlier attempts I tried to declare the function inside the package, e.g. by placing the code written above directly inside the prediction functions. Which also didn't work when the package was installed.

pred.fun.1 <- function(x){
  var.fun <- function(x){x^2}
  predict(mod.gam.1,
          newdata = data.frame("x" = x))
}

I've also tried with and attach at the same place (inside the prediction functions) without any success.

The only working solution I found, was to export the variance functions and make it part of the package namespace/API. This is no feasible solution in this case as it would lead to numerous visible variance functions, which have no practical benefit for the user.

Then there is the obvious workaround: replacing the variance functions by the original formulas in the model call, i.e. using mod.gam.2 instead of mod.gam.1. However, this is not a proper solution either.

Various search engines and colleagues have been consulted to no avail.

Thus, I would be grateful for any hints how to tackle this problem

R version 3.6.2 (2019-12-12)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.3 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale:
 [1] LC_CTYPE=de_DE.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=de_DE.UTF-8        LC_COLLATE=de_DE.UTF-8
 [5] LC_MONETARY=de_DE.UTF-8    LC_MESSAGES=de_DE.UTF-8
 [7] LC_PAPER=de_DE.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=de_DE.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] gamvarfun_0.1.0 usethis_1.5.0   devtools_2.0.2

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.2        rstudioapi_0.10   magrittr_1.5      splines_3.6.2
 [5] pkgload_1.0.2     lattice_0.20-38   R6_2.4.0          rlang_0.4.0
 [9] tools_3.6.2       grid_3.6.2        pkgbuild_1.0.3    nlme_3.1-143
[13] mgcv_1.8-31       sessioninfo_1.1.1 cli_1.1.0         withr_2.1.2
[17] remotes_2.0.4     assertthat_0.2.1  digest_0.6.20     rprojroot_1.3-2
[21] crayon_1.3.4      Matrix_1.2-18     processx_3.3.1    callr_3.2.0
[25] fs_1.3.1          ps_1.3.0          curl_4.0          testthat_2.1.1
[29] memoise_1.1.0     glue_1.3.1        compiler_3.6.2    desc_1.2.0
[33] backports_1.1.4   prettyunits_1.0.2

Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>