covaruber / sommer

36 stars 23 forks source link

mmec error when data is provided as tibble #60

Closed nielverb closed 1 month ago

nielverb commented 2 months ago

The mmec function returns an error ('Error in .subscript.2ary(x, i, j, drop = drop) : subscript out of bounds') when the data is provided as a tibble, a 'data frame with class [tbl_df]'. This can be solved by converting the tibble to a data.frame with the as.data.frame function, but as it's not obvious what is causing the error in the first place, it may be useful to ensure package compatibility with the tibble format.

A minimal code example is shown below:

library(MASS)
library(tidyverse)
library(sommer)

ID <- paste0("ID-", str_pad(1:100, 3, pad="0")) # 100 genotype IDs
Env <- as.character(1:4) # 4 environments

ID_test <- sample(ID, 15) # sample 15 genotype IDs for making a test set

# creating a data frame with all combinations of IDs and environments
df_example <- expand_grid(ID = ID, Env = Env) %>% 
  mutate(y = rnorm(n()), # random response variable
         yNA = if_else(ID %in% ID_test, NA, y)) # masking test IDs with NA

# genomic matrix dummy
M_dummy <- matrix(round(runif(100*1000)*2, 0)-1, nrow = 100)
colnames(M_dummy) <- paste0("SNP-", str_pad(1:dim(M_dummy)[2], 4, pad = "0"))
rownames(M_dummy) <- paste0("ID-", str_pad(1:dim(M_dummy)[1], 3, pad = "0"))

# calculate GRM
X <- scale(M_dummy,scale = T,center = T)
GRM_dummy <- tcrossprod(X)/ncol(X)
GRM_dummy <- as(GRM_dummy, Class="dgCMatrix")

# run mmec model, this should return the error
GBLUP_model <- mmec(formula("yNA ~ 1"),
              random = ~ vsc(dsc(Env), dsc(ID), Gu = GRM_dummy),
              rcov = ~ units,
              data = df_example)

# error can be solved by using as.data.frame around df_example
GBLUP_model <- mmec(formula("yNA ~ 1"),
                    random = ~ vsc(dsc(Env), dsc(ID), Gu = GRM_dummy),
                    rcov = ~ vsc(dsc(ID), isc(units)),
                    data = as.data.frame(df_example))
covaruber commented 1 month ago

Thanks for reporting this and I apologize for the slow response. I will add this line of code and push it together with other changes to CRAN and GitHub next week :) thanks

covaruber commented 1 month ago

This has been corrected and pushed to Github. On CRAN in the next days.