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 in predict.mmer Error in $<-.data.frame(*tmp*, start, value = c(1, 2, 3, 4, 5, 13, : replacement has 16 rows, data has 8 #54

Open
barbosawf opened this issue Sep 18, 2023 · 4 comments

Comments

@barbosawf
Copy link

I have been trying to predict a variable from a model using random regression with the mmer function. However, I am getting the following error:

"Error in `$<-.data.frame`(`*tmp*`, start, value = c(1, 2, 3, 4, 5, 13, : replacement has 16 rows, data has 8"

I tried to find out the error by carefully running the function code and realized that the start and end vectors are longer than the number of lines in the Dtable.

That is, when the start and end vectors are created here:

   start = end = numeric()
   add <- 1
   for (i in 1:length(effectsN)) {
     start[i] = add
     end[i] = start[i] + effectsN[i] - 1
     add = end[i] + 1
   }

They cannot be added here:

     Dtable$start <- start
     Dtable$end <- end

Is there a way to fix this?

Here is some information about my model:
image

Thank you very much in advance!

@kronluk
Copy link

kronluk commented Dec 6, 2023

Hi all
First of all, thanks for an awesome package!

I have encountered a similar issue to barbosawf. For me, the problem is that there are less rows in the replacement compared to data when I add a 2D-spline to account for spatial effects in my model.
I can work around this by creating a custom Dtable (i.e. remove an "unused" row from model$Dtable) and specifying the Dtable argugment in predict.mmer.

Although this is a bit ugly, it seems to work in my case.
Yet, there seems to be a problem in the "if (is.null(Dtable) & is.character(D)) {...." part of predict.mmer and it would be good to have a proper solution.

Please find a reproducible example below.

Many thanks!

#==============================================
# Example

## sommer version: 4.3.2 (2023-09-01)
## R version 4.2.1 (2022-06-23 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 22621)
#==============================================

library(sommer)

# get data
data(DT_cpdata)
DT <- DT_cpdata
GT <- GT_cpdata
MP <- MP_cpdata

A <- A.mat(GT)

# fit basic model
m <- mmer(Yield~1,
          random = ~vsr(id, Gu=A) +
                    vsr(Rowf) +
                    vsr(Colf),
          rcov = ~vsr(units),
          data=DT,
          verbose = FALSE, dateWarning = FALSE)

# fit model using 2D p-spline for spatial correction
m_spl2Da <- mmer(Yield~1,
                 random = ~vsr(id, Gu=A) +
                           vsr(Rowf) +
                           vsr(Colf) +
                           spl2Da(Row,Col), 
                 rcov=~vsr(units),
                 data=DT, 
                 nIters = 3, verbose = FALSE, dateWarning = FALSE)

# predict
pr_m        <- predict(m,D="id") # works
pr_m_spl2Da <- predict(m_spl2Da,D="id")  # Error in `$<-.data.frame`(`*tmp*`, "start", value = c(1, 2, 365, 378,  : 
                                         # replacement has 5 rows, data has 6
m$Dtable
pr_m$Dtable

m_spl2Da$Dtable

# remove 'unused' row from Dtable and specify terms to be included/averaged
Dt <- m_spl2Da$Dtable
Dt[Dt$term == "1","include"] = TRUE
Dt[Dt$term == "1","average"] = TRUE
Dt[Dt$term == "id","include"] = TRUE
Dt <- Dt[-6,]

# predict by specifying 'custom' Dtable
pr_m_spl2Da<- predict(m_spl2Da,D="id",Dtable = Dt)
pr_m_spl2Da$Dtable

# inspect
summary(m)
summary(m_spl2Da)
plot(pr_m$pvals$predicted.value,pr_m_spl2Da$pvals$predicted.value) # prediction seems OK


@covaruber
Copy link
Owner

I apologize for the slow response. The predict function for mmer() is indeed a headache, I am trying to rewrite all formulae of mmer but is taking me more than I thought. You can try mmec() and the predict function from that one. That second function uses the ideal formulae and is easy to keep the predict function easy to build.

@nikitautiu
Copy link

On a similar note, I've recently encountered an exception at this line in predict.mmer when trying to predict BLUEs for a model without any random effects. The bug is not major since it just erroneously tries to iterate over the random effect vector.

However, I was wondering what would be the general recommendation moving forward. Should we just use mmec or migrate towards lme4breeding perhaps for future applications?

@covaruber
Copy link
Owner

Sorry for not getting to this bug yet but I have limited time to improve each of the packages I develop. I would insist to move to use the mmec() function which is the one that has a solid predict function:

`library(sommer)

get data

data(DT_cpdata)
DT <- DT_cpdata
GT <- GT_cpdata
MP <- MP_cpdata

A <- A.mat(GT)
Ai <- as(solve(A + diag(1e-4,ncol(A),ncol(A))), Class="dgCMatrix")

fit basic model

m <- mmec(Yield~1,
random = ~vsc(isc(id), Gu=Ai) +
Rowf + Colf,
rcov = ~ units,
data=DT,verbose = FALSE,
dateWarning = FALSE)

fit model using 2D p-spline for spatial correction

m_spl2Da <- mmec(Yield~1,
random = ~vsc(isc(id), Gu=Ai) +
Rowf + Colf + spl2Dc(Row,Col),
rcov = ~ units,
data=DT,verbose = FALSE,
dateWarning = FALSE)

predict

pr_m <- predict(m,D="id") # works
pr_m_spl2Da <- predict(m_spl2Da,D="id") # Error in $<-.data.frame(*tmp*, "start", value = c(1, 2, 365, 378, :

plot(pr_m$pvals$predicted.value,pr_m_spl2Da$pvals$predicted.value) # prediction seems OK
`

At some point I will fix the predict function in mmer() just not yet since I am actively developing lme4breeding and evola. None of the packages will be discontinued since they attack different problems through different algorithms.

Cheers,
Eduardo

@covaruber covaruber reopened this Aug 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants