Skip to content

Commit 4151cb2

Browse files
authored
Merge pull request #134 from kgoldfeld/def-data-enhancements
Def data enhancements
2 parents e7efa4c + 814703c commit 4151cb2

File tree

7 files changed

+336
-31
lines changed

7 files changed

+336
-31
lines changed

NAMESPACE

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export(defMiss)
1717
export(defRead)
1818
export(defReadAdd)
1919
export(defReadCond)
20+
export(defRepeat)
21+
export(defRepeatAdd)
2022
export(defSurv)
2123
export(delColumns)
2224
export(gammaGetShapeRate)

R/define_data.R

+145-20
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,147 @@ defDataAdd <- function(dtDefs = NULL,
216216
return(defNew[])
217217
}
218218

219+
#' Add multiple (similar) rows to definitions table
220+
#'
221+
#' @param dtDefs Definition data.table to be modified
222+
#' @param nVars Number of new variables to define
223+
#' @param prefix Prefix (character) for new variables
224+
#' @param formula An R expression for mean (string)
225+
#' @param variance Number or formula
226+
#' @param dist Distribution. For possibilities, see details
227+
#' @param link The link function for the mean, see details
228+
#' @param id A string indicating the field name for the unique record identifier
229+
#' @return A data.table named dtName that is an updated data definitions table
230+
#' @seealso [distributions]
231+
#' @details The possible data distributions are: `r paste0(.getDists(),collapse = ", ")`.
232+
#'
233+
#' @examples
234+
#' def <- defRepeat(
235+
#' nVars = 4, prefix = "g", formula = "1/3;1/3;1/3",
236+
#' variance = 0, dist = "categorical"
237+
#' )
238+
#' def <- defData(def, varname = "a", formula = "1;1", dist = "trtAssign")
239+
#' def <- defRepeat(def, 8, "b", formula = "5 + a", variance = 3, dist = "normal")
240+
#' def <- defData(def, "y", formula = "0.10", dist = "binary")
241+
#'
242+
#' def
243+
#' @export
244+
#' @concept define_data
245+
defRepeat <- function(dtDefs = NULL,
246+
nVars,
247+
prefix,
248+
formula,
249+
variance = 0,
250+
dist = "normal",
251+
link = "identity",
252+
id = "id") {
253+
assertNotMissing(
254+
nVars = missing(nVars),
255+
prefix = missing(prefix),
256+
formula = missing(formula)
257+
)
258+
259+
varnames <- paste0(prefix, 1:nVars)
260+
261+
if (is.null(dtDefs)) {
262+
defNew <- defData(
263+
varname = varnames[1], formula = formula,
264+
variance = variance, dist = dist, link = link, id = id
265+
)
266+
267+
for (i in (2:nVars)) {
268+
defNew <- defData(defNew,
269+
varname = varnames[i],
270+
formula = formula, variance = variance,
271+
dist = dist, link = link, id = id
272+
)
273+
}
274+
} else {
275+
defNew <- data.table::copy(dtDefs)
276+
277+
for (i in 1:nVars) {
278+
defNew <- defData(defNew,
279+
varname = varnames[i],
280+
formula = formula, variance = variance,
281+
dist = dist, link = link, id = id
282+
)
283+
}
284+
}
285+
286+
return(defNew[])
287+
}
288+
289+
#' Add multiple (similar) rows to definitions table that will be used to add data to an
290+
#' existing data.table
291+
#'
292+
#' @param dtDefs Definition data.table to be modified
293+
#' @param nVars Number of new variables to define
294+
#' @param prefix Prefix (character) for new variables
295+
#' @param formula An R expression for mean (string)
296+
#' @param variance Number or formula
297+
#' @param dist Distribution. For possibilities, see details
298+
#' @param link The link function for the mean, see details
299+
#' @param id A string indicating the field name for the unique record identifier
300+
#' @return A data.table named dtName that is an updated data definitions table
301+
#' @seealso [distributions]
302+
#' @details The possible data distributions are: `r paste0(.getDists(),collapse = ", ")`.
303+
#'
304+
#' @examples
305+
#' def <- defRepeatAdd(
306+
#' nVars = 4, prefix = "g", formula = "1/3;1/3;1/3",
307+
#' variance = 0, dist = "categorical"
308+
#' )
309+
#' def <- defDataAdd(def, varname = "a", formula = "1;1", dist = "trtAssign")
310+
#' def <- defRepeatAdd(def, 8, "b", formula = "5 + a", variance = 3, dist = "normal")
311+
#' def <- defDataAdd(def, "y", formula = "0.10", dist = "binary")
312+
#'
313+
#' def
314+
#' @export
315+
#' @concept define_data
316+
defRepeatAdd <- function(dtDefs = NULL,
317+
nVars,
318+
prefix,
319+
formula,
320+
variance = 0,
321+
dist = "normal",
322+
link = "identity",
323+
id = "id") {
324+
assertNotMissing(
325+
nVars = missing(nVars),
326+
prefix = missing(prefix),
327+
formula = missing(formula)
328+
)
329+
330+
varnames <- paste0(prefix, 1:nVars)
331+
332+
if (is.null(dtDefs)) {
333+
defNew <- defDataAdd(
334+
varname = varnames[1], formula = formula,
335+
variance = variance, dist = dist, link = link
336+
)
337+
338+
for (i in (2:nVars)) {
339+
defNew <- defDataAdd(defNew,
340+
varname = varnames[i],
341+
formula = formula, variance = variance,
342+
dist = dist, link = link
343+
)
344+
}
345+
} else {
346+
defNew <- data.table::copy(dtDefs)
347+
348+
for (i in 1:nVars) {
349+
defNew <- defDataAdd(defNew,
350+
varname = varnames[i],
351+
formula = formula, variance = variance,
352+
dist = dist, link = link
353+
)
354+
}
355+
}
356+
357+
return(defNew[])
358+
}
359+
219360
#' Read external csv data set definitions
220361
#'
221362
#' @param filen String file name, including full path. Must be a csv file.
@@ -450,11 +591,10 @@ defSurv <- function(dtDefs = NULL,
450591
formula = 0,
451592
scale,
452593
shape = 1) {
453-
454594
if (is.null(dtDefs)) {
455595
dtDefs <- data.table::data.table()
456596
}
457-
597+
458598
dt.new <- data.table::data.table(
459599
varname,
460600
formula,
@@ -520,57 +660,42 @@ defSurv <- function(dtDefs = NULL,
520660
newvar <- ensureValidName(newvar, call = sys.call(-1))
521661
assertNotInDataTable(vars = newvar, dt = defVars)
522662

523-
switch(
524-
newdist,
525-
663+
switch(newdist,
526664
binary = {
527665
.isValidArithmeticFormula(newform, defVars)
528666
.isIdLogit(link)
529667
},
530-
531668
beta = ,
532669
binomial = {
533670
.isValidArithmeticFormula(newform, defVars)
534671
.isValidArithmeticFormula(variance, defVars)
535672
.isIdLogit(link)
536673
},
537-
538674
noZeroPoisson = ,
539-
540675
poisson = ,
541-
542676
exponential = {
543677
.isValidArithmeticFormula(newform, defVars)
544678
.isIdLog(link)
545679
},
546-
547680
gamma = ,
548-
549681
negBinomial = {
550682
.isValidArithmeticFormula(newform, defVars)
551683
.isValidArithmeticFormula(variance, defVars)
552684
.isIdLog(link)
553685
},
554-
555686
nonrandom = .isValidArithmeticFormula(newform, defVars),
556-
557687
normal = {
558688
.isValidArithmeticFormula(newform, defVars)
559689
.isValidArithmeticFormula(variance, defVars)
560690
},
561-
562691
categorical = .checkCategorical(newform),
563-
564692
mixture = {
565693
.isValidArithmeticFormula(newform, defVars)
566694
.checkMixture(newform)
567695
},
568-
569696
uniform = ,
570-
571697
uniformInt = .checkUniform(newform),
572-
trtAssign = .checkCategorical(newform),
573-
698+
trtAssign = .checkCategorical(newform),
574699
stop("Unknown distribution.")
575700
)
576701

@@ -651,7 +776,7 @@ defSurv <- function(dtDefs = NULL,
651776

652777
#' Check uniform formula
653778
#'
654-
#' @description Unifom formulas must be of the form "min;max"
779+
#' @description Uniform formulas must be of the form "min;max"
655780
#' @param formula Formula as string.
656781
#' @return Invisible, error if formula not valid.
657782
#' @seealso distributions

man/defRepeat.Rd

+58
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/defRepeatAdd.Rd

+60
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-add_data.R

+24-1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,27 @@ test_that("addColumns works.", {
3131
def2 <- defDataAdd(varname = "y", formula = "2.3 * (1/x)", dist = "normal")
3232

3333
expect_silent(addColumns(def2, dt))
34-
})
34+
})
35+
36+
test_that("defRepeatAdd works", {
37+
expect_silent(
38+
defRepeatAdd(nVars = 4, prefix = "g", formula = "1/3;1/3;1/3", variance = 0, dist = "categorical")
39+
)
40+
41+
def <- defDataAdd(varname = "a", formula = "1;1", dist = "trtAssign")
42+
expect_silent(
43+
defRepeatAdd(def, 8, "b", formula = "5 + a", variance = 3, dist = "normal")
44+
)
45+
46+
expect_silent(defRepeatAdd(nVars = 4, prefix = "b", formula = "5 + a", variance = 3, dist = "normal"))
47+
48+
})
49+
50+
test_that("defRepeatAdd throws errors correctly.", {
51+
expect_error(defRepeatAdd(prefix = "b", formula = 5, variance = 3, dist = "normal"),
52+
class = "simstudy::missingArgument")
53+
expect_error(defRepeatAdd(nVars = 8, formula = 5, variance = 3, dist = "normal"),
54+
class = "simstudy::missingArgument")
55+
expect_error(defRepeatAdd(nVars = 8, prefix = "b", variance = 3, dist = "normal"),
56+
class = "simstudy::missingArgument")
57+
})

0 commit comments

Comments
 (0)