PLN-team / PLNmodels

A collection of Poisson lognormal models for multivariate count data analysis
https://pln-team.github.io/PLNmodels
GNU General Public License v3.0
54 stars 18 forks source link

Problem with formulas in ZIPLN? #126

Closed papayoun closed 1 week ago

papayoun commented 5 months ago

I work with version 1.2.0 of the package, downloaded on github, and I have issue to use ZIPLN within (mc)lapply framework to parallelize treatment over different model formulas.

Basically, what I would like to do with ZIPLN is this, which works with the classical PLN function:

library(PLNmodels) # Version 1.2.0
data("barents")

# With PLN ----------------------------------------------------------------

pln_formulas <- c("Abundance ~ 1")
lapply(pln_formulas, function(form_){
  PLN(pln_formulas, data = barents)
}) # Works fine

I then define a new formula and I want to use ZIPLN, but this won't work within lapply. It works if I do it within a loop:

# With ZIPLN --------------------------------------------------------------

ZIPLN("Abundance ~ 1 | 1", data = barents) # Works fine, just to check

# Creating vector of formulas
zi_pln_formulas <- c("Abundance ~ 1 | 1")

# Works fine within a loop using as.formula
for(x in zi_pln_formulas){
  ZIPLN(as.formula(x), data = barents)
} # Works fine

However, the following codes won't work (the error is shown in comment each time)

# Using the raw object
ZIPLN(zi_pln_formulas, data = barents) 
# Error in x$formula : object of type 'symbol' is not subsettable

# Within a lapply, first as a string
lapply(zi_pln_formulas, function(form_){
  ZIPLN(form_, data = barents)
})
 # Error in x$formula : object of type 'symbol' is not subsettable

# Within a lapply, with as.formula
lapply(zi_pln_formulas, function(form_){
  ZIPLN(as.formula(form_), data = barents)
}) 
# Error in as.formula(form_) : object 'form_' not found

My R session info is the following

platform       x86_64-pc-linux-gnu                        
arch           x86_64                                     
os             linux-gnu                                  
system         x86_64, linux-gnu                          
status         Patched                                    
major          4                                          
minor          2.2                                        
year           2022                                       
month          11                                         
day            10                                         
svn rev        83330                                      
language       R                                          
version.string R version 4.2.2 Patched (2022-11-10 r83330)
mahendra-mariadassou commented 5 months ago

Hi @papayoun, thanks for finding the bug. I'll have a look into it (it's probably due to the "non standard" formulas used in ZIPLN).

Can you just check something for me: I get an error when I try to use as.formula() within ZIPLN within a loop but your reprex suggests it works for you (I have the same error as the one you get with lapply() or the raw object). Can you confirm this ?

library(PLNmodels)
#> This is package 'PLNmodels' version 1.2.0
#> Use future::plan(multicore/multisession) to speed up PLNPCA/PLNmixture/stability_selection.

# Creating vector of formulas
zi_pln_formulas <- c("Abundance ~ 1 | 1")

# Works fine within a loop using as.formula
for(x in zi_pln_formulas){
  ZIPLN(x, data = barents)
} # Works fine for @papayoun 
# but not for me
#> Error in x$formula: objet de type 'symbol' non indiçable 

Created on 2024-04-10 with reprex v2.1.0

jchiquet commented 3 weeks ago

Found a fix (need to eval the symbol in the correct environment to get the right formula). A PR is en route.

Seems to work without any "as.formula" inside/outside the loop.

jchiquet commented 3 weeks ago

@papayoun could you please confirm that the current master version of PLNmodels fixes your problem ?

thank you,

jchiquet commented 1 week ago

Ok, closing since no reaction of @papayoun