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

Predict failure when using hpc implementation #10

Open
dromera2 opened this issue Feb 27, 2024 · 3 comments
Open

Predict failure when using hpc implementation #10

dromera2 opened this issue Feb 27, 2024 · 3 comments

Comments

@dromera2
Copy link

Hi!
Thank you for your great work with this package. I am trying to make a spatial prediction with a model calibrated using the hpc implementation (engine = "HPC"), but I am getting an error which is not present when using the traditional approach "Error in if (alphapw[alpha[h], 1] > 0) { : absent value where TRUE/FALSE is necessary". Just wanted to know if you have tested the predict function in this situation or it is not still allowed within this approach.

Best regards,

Daniel Romera

@jarioksa
Copy link
Collaborator

At which state do you get this error? When running in R or after taking the object to the HPC platform?

@gtikhonov
Copy link
Member

gtikhonov commented Feb 27, 2024

Hi,

Thanks for the positive feedback!

I am aware of this issue that you have run into. It comes from duality of certain matrix vs list of vector representations in JSON, which is used to transfer posterior from TF to R. E.g. [[1,2],[3,4]] can be both 2x2 matrix and two-element list with vectors of length 2. So far such misbehaviour has happened only for alpha parameter in some contexts, but theoretically it can manifest for other parameters as well.

This bug will be properly fixed in the next update to the Hmsc-HPC package, which is coming soon. Meanwhile, I would suggest to apply a post-hoc correction to the imported posterior samples just before you call m = importPosteriorFromHPC(m, postList, nSamples, thin, transient) (may require some minor modifications)

for(cInd in 1:nChains){
   if(m$nr==1){
      for(i in 1:nSamples){
         postList[[cInd]][[i]]$Alpha = list(postList[[cInd]][[i]]$Alpha[1,])
      }
   }
   if(is.matrix(postList[[cInd]][[1]]$Alpha)){
      for(i in 1:nSamples){
         x = postList[[cInd]][[i]]$Alpha
         postList[[cInd]][[i]]$Alpha = lapply(seq_len(nrow(x)), function(i) x[i,])
      }
   }
}

@dromera2
Copy link
Author

dromera2 commented Feb 29, 2024

Hi!
Thank you so much for your answer, it has been very usefull. I could run de predictions without any problem. Just for the record, I use it after import the Posteriors just as follows:

fitSepTF = importPosteriorFromHPC(m, chainList, samples, thin, transient)
  
  for(cInd in 1:nChains){
    if(m$nr==1){
      for(i in 1:samples){
        fitSepTF$postList[[cInd]][[i]]$Alpha = list(fitSepTF$postList[[cInd]][[i]]$Alpha[1,])
      }
    }
    if(is.matrix(fitSepTF$postList[[cInd]][[1]]$Alpha)){
      for(i in 1:samples){
        x = fitSepTF$postList[[cInd]][[i]]$Alpha
        fitSepTF$postList[[cInd]][[i]]$Alpha = lapply(seq_len(nrow(x)), function(i) x[i,])
      }
    }
  }
  fitSepTF

Best regards

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

3 participants