Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error for specific dataset when using rand_formula "contrasts can be applied only to factors with 2 or more levels" #271

Open
tybonic opened this issue Jun 24, 2024 · 1 comment
Labels
help wanted Extra attention is needed

Comments

@tybonic
Copy link

tybonic commented Jun 24, 2024

Hi Frederick,

I am running ancombc2 on multiple subsets of my data containing different samples. I generate these subsets by filtering a phyloseq object containing all my samples. ancombc2 works fine with 3 of the 4 subsets, but for the 4th subset, I get an error message. This is my call;

ancombc2_out <-ancombc2(
    data = dat_phy_plant_2021_landrace,
    tax_level = NULL,
    fix_formula = "treatment",
    rand_formula = "(1 | run_nr_exp_013) + (1 | block)",
    n_cl = 28,
    verbose = TRUE
)

And this is the error I get:

Obtaining initial estimates ...

Error: Encountering the error for `lmerTest` package.
Please try to select one of your taxa and use its raw counts to fix the same linear mixed-effects model using `lmerTest` without the `ANCOMBC` package.
Load all necessary packages EXCEPT `ANCOMBC`, and see if the error arises due to package incompatibility or other issues.
The error message from `lmerTest` is as follows:
contrasts can be applied only to factors with 2 or more levels
Traceback:

1. ancombc2(data = dat_phy_plant_2021_landrace, tax_level = NULL, 
 .     fix_formula = "treatment", rand_formula = "(1 | run_nr_exp_013) + (1 | block)", 
 .     n_cl = 28, verbose = TRUE)
2. .iter_remle(x = x, y = y1, meta_data = meta_data, fix_formula = fix_formula, 
 .     rand_formula = rand_formula, lme_control = lme_control, theta = NULL, 
 .     tol = iter_control$tol, max_iter = iter_control$max_iter, 
 .     verbose = iter_control$verbose)
3. tryCatch({
 .     result <- lmerTest::lmer(formula = tformula, data = data.frame(y_crt = y[1, 
 .         ], meta_data), control = lme_control)
 . }, error = function(e) {
 .     message <- sprintf(paste("Encountering the error for `lmerTest` package.", 
 .         "Please try to select one of your taxa and use its raw counts to fix the same linear mixed-effects model using `lmerTest` without the `ANCOMBC` package.", 
 .         "Load all necessary packages EXCEPT `ANCOMBC`, and see if the error arises due to package incompatibility or other issues.", 
 .         "The error message from `lmerTest` is as follows:", e$message, 
 .         sep = "\n"))
 .     stop(message, call. = FALSE)
 . })
4. tryCatchList(expr, classes, parentenv, handlers)
5. tryCatchOne(expr, names, parentenv, handlers[[1L]])
6. value[[3L]](cond)
7. stop(message, call. = FALSE)

I tried fitting the model for one if my taxa with lmerTest:lmer() as recommended and that worked fine. Also, my phyloseq object has more than two levels for each random factor:

table(sample_data(dat_phy_plant_2021_landrace)$run_nr_exp_013)
table(sample_data(dat_phy_plant_2021_landrace)$block)
 exp_013_run1 exp_013_run2b 
           21            15 
1_2021_ru 2_2021_ru 3_2021_ru 
       12        11        13 

The factor variables have the data type "factor" and there are no NAs. I am a bit clueless on how to continue troubleshooting. Perhaps you have encountered this error before and have a hint on how to fix it?

@elmonten
Copy link

Hello!

I have been encountering the same issue. What transformation did you use?
I was using centered log ratio, but then I switched to microbiome::transform(pseq_genus, "rclr") and didn't show the error. Thus, I assumed it had something to do with it.

@Maggie8888 Maggie8888 added the help wanted Extra attention is needed label Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants