| Title: | Block Randomisation for Clinical Studies |
|---|---|
| Description: | Create block randomised treatment lists for clinical studies. |
| Authors: | Hauke Sonnenberg |
| Maintainer: | Hauke Sonnenberg <[email protected]> |
| License: | GPL (>= 2) |
| Version: | 0.1.0 |
| Built: | 2026-05-20 06:24:08 UTC |
| Source: | https://github.com/hsonne/blockrand2 |
Get a suitable blocksize for a given number of subjects
blocksize(x, N)blocksize(x, N)
x |
number of patients in stratum |
N |
number of treatments |
integer vector of length one representing the blcksize
# Blocksize for 10 subjects and 2 treatments blocksize(10, 2) # Blocksize for 10 subjects and 3 treatments blocksize(10, 3)# Blocksize for 10 subjects and 2 treatments blocksize(10, 2) # Blocksize for 10 subjects and 3 treatments blocksize(10, 3)
Create all possible combinations of values given in a list of value vectors
createCombinations(x)createCombinations(x)
x |
List of vectors containing the values to be combined. The names of the list elements will appear as column names in the output data frame. |
Data frame with each row representing a possible combination of values.
createCombinations(list( sex = c("male", "female"), smokes = c("yes", "sometimes", "no"), cancer = c("yes", "no") ))createCombinations(list( sex = c("male", "female"), smokes = c("yes", "sometimes", "no"), cancer = c("yes", "no") ))
Create block randomisation list documents in three possible formats: HTML, PDF (requires LaTeX) and Microsoft Word
createRandomisationDoc(patients, strataVars, treatments, header = list(title = "Randomisierungsliste zur Studie: title", author = "author", date = Sys.Date()), format = c("html", "word", "pdf"), newpage = FALSE, file.rmd = system.file("extdata", "randomList.Rmd", package = "blockrand2"), outdir = file.path(tempdir(), "blockrand2"))createRandomisationDoc(patients, strataVars, treatments, header = list(title = "Randomisierungsliste zur Studie: title", author = "author", date = Sys.Date()), format = c("html", "word", "pdf"), newpage = FALSE, file.rmd = system.file("extdata", "randomList.Rmd", package = "blockrand2"), outdir = file.path(tempdir(), "blockrand2"))
patients |
data frame containing the patient's data as returned by
|
strataVars |
List of strata variables, each of which is defined in the form of a vector of possible (character) values. |
treatments |
named vector of character defining the possible treatments. The names are used as acronyms, the values as the full treatment names |
header |
list with elements |
format |
vector of elements from |
newpage |
if |
file.rmd |
full path to the RMarkdown file that is rendered to create
the randomisation list documents. By default the file |
outdir |
full path to the output directory to which the created document
files are written. By default the files are written to a subfolder
|
list containing the full paths to the created files as values and the
file extensions (html, docx, pdf) as element names
https://github.com/hsonne/blockrand2 (provides a short tutorial in the README file)
## Not run: # Define title of the study, the author and the date header <- list( title = "Randomisation lists for the study: *Jokes against craziness*", author = "Hauke Sonnenberg", date = "2016-08-27" ) # Define stratum variables strataVars <- list( sex = c("male", "female"), crazyness = c("weak", "medium", "strong") ) # Define the treatments treatments <- c( joke = "Tell funny jokes", nojoke = "Keep serious!" ) # Create some patient's testdata using the stratum levels patients <- createTestdata(strataVars, 40) # Create the randomisation list documents. We store the paths to the created # files that are returned invisibly by createRandomisationDoc() in a variable files <- createRandomisationDoc( patients = patients, strataVars = strataVars, treatments = treatments, header = header, newpage = TRUE ) # Show the paths of the created files files # Open the html file in the default browser browseURL(files$html) # Open the pdf file in the default PDF viewer system(paste(getOption("pdfviewer"), files$pdf)) ## End(Not run)## Not run: # Define title of the study, the author and the date header <- list( title = "Randomisation lists for the study: *Jokes against craziness*", author = "Hauke Sonnenberg", date = "2016-08-27" ) # Define stratum variables strataVars <- list( sex = c("male", "female"), crazyness = c("weak", "medium", "strong") ) # Define the treatments treatments <- c( joke = "Tell funny jokes", nojoke = "Keep serious!" ) # Create some patient's testdata using the stratum levels patients <- createTestdata(strataVars, 40) # Create the randomisation list documents. We store the paths to the created # files that are returned invisibly by createRandomisationDoc() in a variable files <- createRandomisationDoc( patients = patients, strataVars = strataVars, treatments = treatments, header = header, newpage = TRUE ) # Show the paths of the created files files # Open the html file in the default browser browseURL(files$html) # Open the pdf file in the default PDF viewer system(paste(getOption("pdfviewer"), files$pdf)) ## End(Not run)
Create random sequences of blockwise equally distributed values. The blocksizes are chosen according to the number of values to be created.
createRandomSequences(x, counts, names.strata = NULL)createRandomSequences(x, counts, names.strata = NULL)
x |
vector of values to be chosen from |
counts |
vector of integers defining the lengths of vectors of values to be created |
names.strata |
vector of character to be used as element names in the output list |
list of vectors of values out of x with lengths according to
the values given in counts
# Define stratum levels strata <- list( sex = c("male", "female"), medication = c("yes", "no"), lesion = c("low", "high") ) # Create some testdata using the stratum levels and group and count by stratum byStratum <- stratify(createTestdata(strata, 50)) # Create random sequences of treatments (A, B) sequences <- createRandomSequences( x = c("A", "B"), counts = byStratum$n, names.strata = byStratum$stratum ) # Append a column "treatments" showing the sequences of treatments as comma # separated lists byStratum$treatments <- sapply(sequences, paste, collapse = ",") # Show the result byStratum# Define stratum levels strata <- list( sex = c("male", "female"), medication = c("yes", "no"), lesion = c("low", "high") ) # Create some testdata using the stratum levels and group and count by stratum byStratum <- stratify(createTestdata(strata, 50)) # Create random sequences of treatments (A, B) sequences <- createRandomSequences( x = c("A", "B"), counts = byStratum$n, names.strata = byStratum$stratum ) # Append a column "treatments" showing the sequences of treatments as comma # separated lists byStratum$treatments <- sapply(sequences, paste, collapse = ",") # Show the result byStratum
Create testdata for clinical studies
createTestdata(strataVars, n = if (is.null(n.per.stratum)) 30 else NULL, n.per.stratum = NULL, format.patient = "P%03d", offset.patient = 0)createTestdata(strataVars, n = if (is.null(n.per.stratum)) 30 else NULL, n.per.stratum = NULL, format.patient = "P%03d", offset.patient = 0)
strataVars |
List of strata variables, each of which is defined in the form of a vector of possible (character) values. |
n |
Number of records to be created |
n.per.stratum |
optional. Vector of integer numbers defining the number
of records for each possible stratum. The length of this vector must be
equal to the number of possible strata (resulting from the product of
lengths of vectors in |
format.patient |
Passed as argument |
offset.patient |
integer number to be added to the generated patient
numbers |
Data frame with a column patient containing the patient's ID
and one column for each stratum, named according to the element names in
strataVars
strataVars <- list( sex = c("female", "male"), medication = c("yes", "no") ) createTestdata(strataVars, n = 20) # Define the number of patients in each stratum (there are four possible # strata: length(strataVars$sex) * length(strataVars$medication)) createTestdata(strataVars, n.per.stratum = c(5, 6, 3, 4))strataVars <- list( sex = c("female", "male"), medication = c("yes", "no") ) createTestdata(strataVars, n = 20) # Define the number of patients in each stratum (there are four possible # strata: length(strataVars$sex) * length(strataVars$medication)) createTestdata(strataVars, n.per.stratum = c(5, 6, 3, 4))
Create a random sequence of equally distributed values
equalDistribution(x, length.out = length(x), do.stop = TRUE)equalDistribution(x, length.out = length(x), do.stop = TRUE)
x |
Vector of values to be chosen from |
length.out |
Length of the output vector to be created |
do.stop |
If |
Vector of length.out values all of which are elements of
x. If length.out is a multiple of the lenght of x the
values are equally distributed. Otherwise (and if do.stop is
FALSE so that no error is thrown) the frequencies of the values
differ by one at most.
y1 <- equalDistribution(x = LETTERS[1:2], 10) # Check the distribution with table table(y1) # Do all values occur with the same frequency? all(diff(table(y1)) == 0) y2 <- equalDistribution(x = LETTERS[1:3], length.out = 11, do.stop = FALSE) # Do the frequencies differ by one at most? all(diff(table(y2)) <= 1)y1 <- equalDistribution(x = LETTERS[1:2], 10) # Check the distribution with table table(y1) # Do all values occur with the same frequency? all(diff(table(y1)) == 0) y2 <- equalDistribution(x = LETTERS[1:3], length.out = 11, do.stop = FALSE) # Do the frequencies differ by one at most? all(diff(table(y2)) <= 1)
Group data by combinations of stratum values and count the records falling into each combination.
stratify(x, strataVars = toStrataVars(x, exclude = names(x)[1]), column.n = "n", column.stratum = "stratum", format.stratum = "S%02d")stratify(x, strataVars = toStrataVars(x, exclude = names(x)[1]), column.n = "n", column.stratum = "stratum", format.stratum = "S%02d")
x |
Data frame with stratum values in columns |
strataVars |
List of strata variables. By default it is created from all
but the first columns of |
column.n |
Column name to be used in the output for the column containing the number of records falling into the combination of stratum values. |
column.stratum |
Column name to be used in the output for the column containing the stratum identifier |
format.stratum |
format string to be used in |
Data frame with each row representing a combination of stratum values
# Define stratifying variables strataVars <- list(sex = c("female", "male"), medication = c("yes", "no")) # Create some random testdata x <- createTestdata(strataVars) # Count records in each stratum stratify(x, strataVars) stratify(x, strataVars, column.n = "n.patients") # Create testdata with defined numbers of records in each stratum n.per.stratum <- 1:4 y <- createTestdata(strataVars, n.per.stratum = n.per.stratum) # Count records in each stratum (counts <- stratify(y, strataVars)) # Check if the numbers of records in each stratum are as expected all(counts$n == n.per.stratum)# Define stratifying variables strataVars <- list(sex = c("female", "male"), medication = c("yes", "no")) # Create some random testdata x <- createTestdata(strataVars) # Count records in each stratum stratify(x, strataVars) stratify(x, strataVars, column.n = "n.patients") # Create testdata with defined numbers of records in each stratum n.per.stratum <- 1:4 y <- createTestdata(strataVars, n.per.stratum = n.per.stratum) # Count records in each stratum (counts <- stratify(y, strataVars)) # Check if the numbers of records in each stratum are as expected all(counts$n == n.per.stratum)
Create a string describing the stratum from a one-row data frame
toStratumString(stratumInfo)toStratumString(stratumInfo)
stratumInfo |
data frame with one row and stratum information in all but
the very first ( |
character string of the form attribute1 = value1, attribute2 = value2, etc.
stratumInfo <- data.frame( stratum = "S01", sex = "male", pretreatment = "no", n = 5 ) toStratumString(stratumInfo)stratumInfo <- data.frame( stratum = "S01", sex = "male", pretreatment = "no", n = 5 ) toStratumString(stratumInfo)