From fa3dd7ed5858c5a94eb4aba5816feb01132beb92 Mon Sep 17 00:00:00 2001 From: Marvin Wright Date: Wed, 17 Jan 2024 09:33:08 +0100 Subject: [PATCH] sample() does not work for vector with length 1 --- R/forge.R | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/R/forge.R b/R/forge.R index 0bfae007..cfed28ce 100644 --- a/R/forge.R +++ b/R/forge.R @@ -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