Skip to content

Commit 50ea8da

Browse files
kgoldfeldmutlusunKeith Goldfeld
authored
Pr240 (#242)
* feat(add_correlated_data): warn if addCorGen `idname` is not unique * Minor edit before merging --------- Co-authored-by: mutlusun <mutlusun@users.noreply.github.com> Co-authored-by: Keith Goldfeld <KSG@180MADPHMLT162.local>
1 parent e691e63 commit 50ea8da

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

R/add_correlated_data.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,18 @@
5959
#' @export
6060
addCorData <- function(dtOld, idname, mu, sigma, corMatrix = NULL,
6161
rho, corstr = "ind", cnames = NULL) {
62+
# addCorData will produce odd results if `idname` appears multiple times in
63+
# `dtOld`. Thus, we check and omit a warning.
64+
65+
if (length(unique(dtOld[, get(idname)])) != nrow(dtOld)) {
66+
valueWarning(
67+
names = c("dtOld"),
68+
msg = paste(idname, "appears multiple times in data table. Please check results.")
69+
)
70+
}
71+
6272
# dtName must contain id for now
73+
6374
dtTemp <- copy(dtOld)
6475
data.table::setkeyv(dtTemp, idname)
6576

tests/testthat/test-generate_correlated_data.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,22 @@ test_that("genCorMat generates errors correctly.", {
398398

399399
# addCorData ----
400400

401+
test_that("addCorData emits warning if `idname` appears multiple times", {
402+
tdef <- defData(varname = "grp", dist = "binary", formula = 0.5, id = "id")
403+
404+
dt <- genData(500, tdef)
405+
dt <- rbind(dt, dt)
406+
407+
mu <- rnorm(3, mean = c(3, 8, 15))
408+
sigma <- rgamma(3, 1.5, 1)
409+
corMat <- genCorMat(3)
410+
411+
expect_warning(
412+
addCorData(dt, "id", mu = mu, sigma = sigma, corMat = corMat),
413+
"id appears multiple times in data table. Please check results."
414+
)
415+
})
416+
401417
test_that("addCorData adds correlated data with compound symmetry structure", {
402418

403419
skip_on_cran()

0 commit comments

Comments
 (0)