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

Prevent division by zero #31

Merged
merged 2 commits into from
May 3, 2023

Conversation

billdenney
Copy link

Fix #29

R/vpcstats.R Outdated
o$obs[, ypc := (mpred/pred)*y]
o$sim[, ypc := (mpred/pred)*y]
o$obs[, ypc := ifelse(pred == 0, 0, (mpred/pred)*y)]
o$sim[, ypc := ifelse(pred == 0, 0, (mpred/pred)*y)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @billdenney for the PR! Yes, we will certainly want to prevent division by zero here, however, I receive the following error in this line of code.

Error in `[.data.table`(o$sim, , `:=`(ypc, ifelse(pred == 0, 0, (mpred/pred) *  : 
  Supplied 600 items to be assigned to 18000 items of column 'ypc'. If you wish to 'recycle' the RHS please use rep() to make this intent clear to readers of your code.

It looks like we may want to update this line to:

o$sim[, ypc := ifelse(rep(pred, times = nrow(o$sim)/nrow(o$obs)) == 0, 0, (mpred/pred)*y)]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for finding that. I think that I was using too small a toy example in my test.

@certara-jcraig certara-jcraig merged commit 1c1be0d into certara:master May 3, 2023
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

Successfully merging this pull request may close these issues.

Division by zero when pred == 0
2 participants