JenniNiku / gllvm

Generalized Linear Latent Variable Models
https://jenniniku.github.io/gllvm/
48 stars 20 forks source link

Error using MuMIn dredge function: no 'nobs' method is available #28

Closed taoofcoffee closed 2 years ago

taoofcoffee commented 3 years ago

I was following the vignette (but using my own data) and encountered an error when passing a gllvm model to the dredge function in MuMIn.

I sucessfully fit a gllvm:

full_env <- gllvm(mex_sp, mex_env, family = "negative.binomial", num.lv = 5, formula = ~ elev + panat + structures + shannon + simpson + fhd + dist_rd)

But when I included the gllvm object to MuMin:

fit_table <- dredge(full_env, rank="AICc")

I receive the following error:

Error in nobs.default(global.model) : no 'nobs' method is available

I tried running through the provided vignette1.Rmd and encountered the same error:

fit_table <- dredge(fiti,varying=list(num.lv=1:5), rank="AIC")

Error in nobs.default(global.model) : no 'nobs' method is available

BertvanderVeen commented 3 years ago

@taoofcoffee Thanks for letting us know! I can reproduce this, though it worked for me before, so that seems strange.

I must have made a mistake when I added this. I've pushed a fix (@JenniNiku in the quadratic branch), but this still doesn't seem to work properly. I suspect this is because nobs.gllvm is not exported as a S3 method in the namespace, @JenniNiku do you have any suggestions on how to fix this?

@taoofcoffee, until this comes available you could continue by adding the following functions manually to your R environment:

nobs.gllvm <- function(object){ n <- dim(object$y)[1] return(n) } nobs <- function(object, ...) { UseMethod(generic = "nobs") }

taoofcoffee commented 3 years ago

Great, thanks! I appreciate the speedy feedback!

TobyDT commented 3 years ago

Hi there, I have come across the same problem and didn't have any luck with the functions you recommended @BertvanderVeen but this may be because I am misunderstanding how to implement them. Are these a single line of code with nobs nested in nobs.gllvm? or are they two separate functions? I could get nobs to work, but not nobs.gllvm and not both together as written.

nobs.gllvm <- function(object){ n <- dim(object$y)[1] return(n) } nobs <- function(object, ...) { UseMethod(generic = "nobs") }

TobyDT commented 3 years ago

Sorry @BertvanderVeen the Error I receive when trying to run those functions is:

Error: unexpected symbol in "nobs.gllvm <- function(object){ n <- dim(object$y)[1] return"

BertvanderVeen commented 3 years ago

Darn it, this thing keeps haunting me! Either how, here is some additional useful information. I promise we will get it right at some point :).

I think this issue might have already been fixed in the development branch, though there is another bug there I've addressed in a pull request #40 (don't download the development branch before that is integrated).

Either how, the two lines you quoted should be run separately, and with separate lines inside the function (and with the correct number of observations now);

nobs.gllvm <- function(object){ 
n <- prod(dim(object$y)) 
return(n)
}

and nobs <- function(object, ...) { UseMethod(generic = "nobs") }

Let me know if that helps? You can select the number of latent variables, starting values, or the quadratic model, like this (unlike what is currently says in the vignette, which I should still change): dredge(model, varying=list(num.lv=list(1,2,3), starting.val=list("zero","res"), quadratic=list(FALSE,TRUE,"LV")))

which I think is pretty cool, and helpful!

TobyDT commented 3 years ago

Awesome thanks @BertvanderVeen super helpful!

nwdaudt commented 2 years ago

Hi there, Just letting you know that this issue is still happening. Find relevant 'Session Info' below.

Following Vignette1:

library(mvabund)
library(gllvm)
library(MuMIn)

data("antTraits")
y <- as.matrix(antTraits$abund)
X <- scale(as.matrix(antTraits$env))

# I did not run the loop here, though
fiti <- gllvm(y, X, family = "negative.binomial", num.lv = 2, sd.errors = FALSE,
                formula = ~ Bare.ground + Canopy.cover + Volume.lying.CWD, 
                seed = 1234)

fit_table <- dredge(fiti,  varying = list(num.lv = 1:5), rank = "AIC")

subset(fit_table, delta < 2)

# Error in nobs.default(global.model) : no 'nobs' method is available

Although I should point out that using @BertvanderVeen's manual FUN worked like a charm :)

# relevant bits of sessionInfo()

R version 4.2.0 (2022-04-22)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.4 LTS

other attached packages:
[1] gllvm_1.3.1   TMB_1.9.0     mvabund_4.2.1 MuMIn_1.46.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.9         pillar_1.7.0       compiler_4.2.0     class_7.3-20       tools_4.2.0        statmod_1.4.36     lifecycle_1.0.1   
 [8] tibble_3.1.7       nlme_3.1-157       lattice_0.20-45    mgcv_1.8-40        pkgconfig_2.0.3    rlang_1.0.4        Matrix_1.4-1      
[15] DBI_1.1.3          cli_3.3.0          rstudioapi_0.13    parallel_4.2.0     e1071_1.7-11       dplyr_1.0.9        generics_0.1.3    
[22] vctrs_0.4.1        classInt_0.4-7     stats4_4.2.0       grid_4.2.0         tidyselect_1.1.2   glue_1.6.2         tweedie_2.3.3     
[29] sf_1.0-8           R6_2.5.1           fansi_1.0.3        purrr_0.3.4        tidyr_1.2.0        magrittr_2.0.3     splines_4.2.0     
[36] ellipsis_0.3.2     units_0.8-0        MASS_7.3-57        assertthat_0.2.1   utf8_1.2.2         KernSmooth_2.23-20 proxy_0.4-27      
[43] crayon_1.5.1 
BertvanderVeen commented 2 years ago

Sorry about this, I had really hoped this was resolved but it seems the R-gods are not in my favor. I will look into it. Thanks for letting us know!

BertvanderVeen commented 2 years ago

OK, I -think- this should now finally be addressed with this commit in my development branch.

nwdaudt commented 2 years ago

All good, thanks for your fast reply/action! :)