-
Notifications
You must be signed in to change notification settings - Fork 1
Add multinomial distribution functions #145
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d33ad4a
Add multinomial distribution functions (#62)
nehill197 2339606
Fix log_lik_multinom() ave() bug and guard against singleton groups
nehill197 209243f
Document group's min-rows requirement; add res_multinom() simulate test
nehill197 9f33a21
Test that res_multinom(simulate = TRUE) propagates the singleton-grou…
nehill197 7917cc3
Guard against uneven group sizes and NA group in multinomial functions
nehill197 3bd467d
Condense multinomial docs/comments; validate group in dev_multinom an…
nehill197 7e1663d
Reword singleton-group error to hint at vectorized evaluation
nehill197 b5ad25d
Fix length-mismatch and partial-NA validation gaps in multinomial fun…
nehill197 2569a14
Clarify internal test intent and pin down modal-size tie-break behavior
nehill197 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,7 @@ Suggests: | |
| covr, | ||
| extraDistr, | ||
| ggplot2, | ||
| glmnet, | ||
| hms, | ||
| knitr, | ||
| memoise, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,84 @@ | ||
| dev_res <- function(x, mu, dev) { | ||
| sign(x - mu) * sqrt(dev) | ||
| } | ||
|
|
||
| # Row indices for each `group`, shared across the multinom_* helpers within | ||
| # a single call (chk_multinom_group(), multinom_row_na(), the sampling loop | ||
| # in ran_multinom()) so `group` isn't re-split by every one of them. | ||
| multinom_split <- function(group) { | ||
| split(seq_along(group), group) | ||
| } | ||
|
|
||
| # Checks every group shares one `size` and `prob` values summing to 1 | ||
| # (required by rmultinom() and the deviance/log-lik identities), has >= 2 | ||
| # rows (a trial needs >= 2 categories -- singletons usually mean `group` | ||
| # was evaluated row-by-row instead of over the whole vector), and matches | ||
| # the modal row count across groups (a short group usually means a row was | ||
| # lost). Only non-NA values are compared, so lone NAs don't error here -- | ||
| # see multinom_row_na(). Callers must chk_not_any_na(group) first; `group` | ||
| # itself can't be NA-tolerant since it's what identifies the trial. | ||
| chk_multinom_group <- function(size, prob, group, groups = multinom_split(group)) { | ||
| for (idx in groups) { | ||
| if (length(idx) < 2L) { | ||
| stop( | ||
| "Each `group` must contain at least 2 rows (a multinomial trial needs at least 2 categories); found a group with only 1 row. This usually means `group`/`size`/`prob` were passed one row at a time instead of as vectors.", | ||
| call. = FALSE | ||
| ) | ||
| } | ||
| known_size <- size[idx][!is.na(size[idx])] | ||
| if (length(unique(known_size)) > 1L) { | ||
| stop( | ||
| "`size` must be the same for every row belonging to the same `group` (multinomial trial).", | ||
| call. = FALSE | ||
| ) | ||
| } | ||
| known_prob <- prob[idx][!is.na(prob[idx])] | ||
| known_prob_sum <- sum(known_prob) | ||
| # a group with a missing prob can only be validated one-sided: the known | ||
| # values must not already exceed 1, since a full sum-to-1 check would be | ||
| # (wrongly) skipped whenever any prob in the group is NA | ||
| prob_bad <- if (length(known_prob) < length(idx)) { | ||
| known_prob_sum > 1 + 1e-6 | ||
| } else { | ||
| abs(known_prob_sum - 1) > 1e-6 | ||
| } | ||
| if (prob_bad) { | ||
| stop( | ||
| "`prob` must sum to 1 for every `group` (multinomial trial).", | ||
| call. = FALSE | ||
| ) | ||
| } | ||
| } | ||
| if (length(groups) > 1L) { | ||
| group_sizes <- lengths(groups) | ||
| size_counts <- table(group_sizes) | ||
| # ties are broken in favour of the smallest row count (table()'s names | ||
| # are sorted ascending, and which.max() takes the first maximum) | ||
| mode_size <- as.integer(names(size_counts)[which.max(size_counts)]) | ||
| bad <- group_sizes != mode_size | ||
| if (any(bad)) { | ||
| stop( | ||
| sprintf( | ||
| "Every `group` should have the same number of rows (%d, the most common number of categories in this data); found a group (\"%s\") with %d row(s) instead. This usually means `group` lost a row that should have been part of that trial.", | ||
| mode_size, | ||
| names(groups)[bad][1], | ||
| group_sizes[bad][1] | ||
| ), | ||
| call. = FALSE | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| # Flags every row whose trial has an NA `size`/`prob` anywhere in the group, | ||
| # since a trial's categories are scored/drawn jointly, not independently. | ||
| multinom_row_na <- function(size, prob, group, groups = multinom_split(group)) { | ||
| bad <- is.na(size) | is.na(prob) | ||
| result <- rep(FALSE, length(group)) | ||
| for (idx in groups) { | ||
| if (any(bad[idx])) { | ||
| result[idx] <- TRUE | ||
| } | ||
| } | ||
| result | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ Numericise | |
| Numericize | ||
| ORCID | ||
| POSIXct | ||
| Poissons | ||
| Psychol | ||
| Schaub | ||
| Shachar | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.