Skip to content

Commit

Permalink
sample() does not work for vector with length 1
Browse files Browse the repository at this point in the history
  • Loading branch information
mnwright committed Jan 17, 2024
1 parent c2d7f27 commit fa3dd7e
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions R/forge.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,14 @@ forge <- function(

if (nrow(omega) == 0) {
stop("All leaves have zero likelihood.")
} else if (nrow(omega) == 1) {
draws <- omega[, .(f_idx)]
} else {
# Draw random leaves with probability proportional to weight
draws <- data.table(
'f_idx' = omega[, sample(f_idx, size = n_synth, replace = TRUE, prob = wt)]
)
}

# Draw random leaves with probability proportional to weight
draws <- data.table(
'f_idx' = omega[, sample(f_idx, size = n_synth, replace = TRUE, prob = wt)]
)
omega <- merge(draws, omega, sort = FALSE)[, idx := .I]

# Simulate continuous data
Expand Down

0 comments on commit fa3dd7e

Please sign in to comment.