| Type: | Package |
| Title: | Some Utilities for Developing Data Science Software |
| Version: | 0.7.5 |
| Description: | A collection of general-purpose helper functions that I (and maybe others) find useful when developing data science software. Includes tools for simulation, data transformation, input validation, and more. |
| License: | GPL (≥ 3) |
| Encoding: | UTF-8 |
| Config/testthat/edition: | 3 |
| RoxygenNote: | 7.3.2 |
| Depends: | R (≥ 4.1.0) |
| Imports: | benchmarkme, checkmate, cli, cubature, dplyr, future,future.apply, ggplot2, glue, hexSticker, progressr, R6, Rcpp,SimMultiCorrData, stats, tibble, tools, utils |
| LinkingTo: | mvtnorm, Rcpp, RcppArmadillo, testthat |
| Suggests: | knitr, rmarkdown, testthat (≥ 3.0.0), xml2 |
| URL: | https://github.com/loelschlaeger/oeli,http://loelschlaeger.de/oeli/ |
| BugReports: | https://github.com/loelschlaeger/oeli/issues |
| NeedsCompilation: | yes |
| Packaged: | 2025-08-18 08:37:57 UTC; Lennart Oelschläger |
| Author: | Lennart Oelschläger [aut, cre] |
| Maintainer: | Lennart Oelschläger <oelschlaeger.lennart@gmail.com> |
| Repository: | CRAN |
| Date/Publication: | 2025-08-18 09:20:24 UTC |
oeli: Some Utilities for Developing Data Science Software
Description

A collection of general-purpose helper functions that I (and maybe others) find useful when developing data science software. Includes tools for simulation, data transformation, input validation, and more.
Author(s)
Maintainer: Lennart Oelschlägeroelschlaeger.lennart@gmail.com
See Also
Useful links:
Report bugs athttps://github.com/loelschlaeger/oeli/issues
Dictionary R6 Object
Description
Provides a simple key-value interface based on R6.
Active bindings
keys[
character()]
Available keys.alias[
list()]
Available keys per alias value.
Methods
Public methods
Methodnew()
Initializing a newDictionary object.
Usage
Dictionary$new( key_name, alias_name = NULL, value_names = character(), value_assert = alist(), allow_overwrite = TRUE, keys_reserved = character(), alias_choices = NULL, dictionary_name = NULL)
Arguments
key_name[
character(1)]
The name for the key variable.alias_name[
NULL|character(1)]
Optionally the name for the alias variable.value_names[
character(0)]
The names of the values connected to a key.value_assert[
alist(1)]
For each element invalue_names,values_assertcan have anidentically named element of the formcheckmate::assert*(...), where...can be any argument for the assertion function except for thexargument.allow_overwrite[
logical(1)]
Allow overwriting existing keys with new values?Duplicate keys are never allowed.keys_reserved[
character()]
Names that must not be used as keys.alias_choices[
NULLorcharacter()]
Optionally possible values for the alias. Can also beNULL, then allalias values are allowed.dictionary_name[
NULLorcharacter()]
Optionally the name for the dictionary.
Methodadd()
Adding an element to the dictionary.
Usage
Dictionary$add(...)
Arguments
...Values for
the key variable
key_name(must be a singlecharacter),the alias variable
alias_name(optionally, must then be acharactervector),all the variables specified for
value_names(if any, they mustcomply to thevalue_assertchecks).
Methodget()
Getting elements from the dictionary.
Usage
Dictionary$get(key, value = NULL)
Arguments
key[
character(1)]
A value for the key variablekey_name. Use the$keysmethod foravailable keys.value[
NULL|character(1)]
One of the elements invalue_names, selecting the required value.Can also beNULL(default) for all values connected to thekey, returned as alist.
Methodremove()
Removing elements from the dictionary (and associated alias, if any).
Usage
Dictionary$remove(key)
Arguments
key[
character(1)]
A value for the key variablekey_name. Use the$keysmethod foravailable keys.
Methodprint()
Printing details of the dictionary.
Usage
Dictionary$print()
See Also
Other package helpers:Storage,check_missing(),find_namespace_calls(),identical_structure(),input_check_response(),match_arg(),package_logo(),print_data.frame(),print_matrix(),system_information(),unexpected_error(),user_confirm()
Examples
# Managing variable metadata for a datasetmeta_dict <- Dictionary$new( key_name = "var_name", alias_name = "category", value_names = c("label", "type"), value_assert = alist( label = checkmate::assert_string(), type = checkmate::assert_choice(choices = c("numeric", "factor", "character")) ), allow_overwrite = FALSE, keys_reserved = c("id"), alias_choices = c("demographics", "outcome", "other"), dictionary_name = "Variable Metadata")# Add entries to the dictionarymeta_dict$add( var_name = "age", label = "Age of respondent", type = "numeric", category = "demographics")meta_dict$add( var_name = "gender", label = "Gender identity", type = "factor", category = "demographics")meta_dict$add( var_name = "income", label = "Annual income in USD", type = "numeric", category = c("demographics", "outcome"))# Print dictionarymeta_dict$print()# Retrieve full metadata for a variablemeta_dict$get("income")# Retrieve a specific piece of metadatameta_dict$get("income", value = "label")# Show variables by categorymeta_dict$aliasSimulator R6 Object
Description
Creates a simulation setup, where a functionf is evaluatedruns times,optionally at each combination of input values.
Provides some convenience (see below for more details):
Simulation results can be restored from a backup if the R session crashes.
More simulation runs can be conducted after the initial simulation, failedsimulation cases can be re-run.
Parallel computation and progress updates are supported.
Details
Backup
Simulation results can be saved to disk, allowing you to restore the resultsif the R session is interrupted or crashes before the simulation completes.To enable backup, setbackup = TRUE in the$go() method, which willcreate a backup directory at the location specified bypath.To restore, useSimulator$initialize(use_backup = path).
More runs and re-run
If additional simulation runs are needed, simply call the$go() methodagain. Any cases that were not successfully completed in previous runs willbe attempted again.
Parallel computation
By default, simulations run sequentially. But since they are independent,they can be parallelized to decrease computation time. To enable parallelcomputation, use the{future} framework.For example, run
future::plan(future::multisession, workers = 4)
in advance for computation in 4 parallel R sessions.
Progress updates
Use the{progressr} framework toget progress updates. For example, run the following in advance:
progressr::handlers(global = TRUE)progressr::handlers( progressr::handler_progress(format = ">> :percent, :eta to go :message"))
Active bindings
results[
tibble, read-only]
The simulation results.cases[
tibble, read-only]
The simulation cases.
Methods
Public methods
Methodnew()
Initialize aSimulator object, either a new one or from backup.
Usage
Simulator$new( use_backup = NULL, verbose = getOption("verbose", default = FALSE))Arguments
use_backup[
NULL|character(1)]
Optionally a path to a backup folder previously used in$go().verbose[
logical(1)]
Provide info? Does not include progress updates. For that, see details.
Methodprint()
Print method.
Usage
Simulator$print()
Methoddefine()
Define function and arguments for a newSimulator object.
Usage
Simulator$define(f, ...)
Arguments
f[
function]
Afunctionto evaluate....Arguments for
f. Each value must benamed after an argument of
f, anda
list, where each element is a variant of that argument forf.
Methodgo()
Run simulations.
Usage
Simulator$go( runs = 0, backup = FALSE, path = paste0("backup_", format(Sys.time(), "%Y-%m-%d-%H-%M-%S")))Arguments
runs[
integer(1)]
The number of (additional) simulation runs.If
runs = 0, only pending cases (if any) are solved.backup[
logical(1)]
Create a backup underpath?path[
character(1)]
Only relevant, ifbackup = TRUE.In this case, a path for a new folder, which does not yet exist andallows reading and writing.
See Also
Other simulation helpers:correlated_regressors(),ddirichlet_cpp(),dmixnorm_cpp(),dmvnorm_cpp(),dtnorm_cpp(),dwishart_cpp(),gaussian_tv(),simulate_markov_chain()
Examples
# 1. Initialize a new simulation setup:object <- Simulator$new(verbose = TRUE)# 2. Define function `f` and arguments (if any):f <- function(x, y = 1) { Sys.sleep(runif(1)) # to see progress updates x + y}x_args <- list(1, 2)object$define(f = f, x = x_args)print(object)# 3. Define 'future' and 'progress' (optional):## Not run: future::plan(future::sequential)progressr::handlers(global = TRUE)## End(Not run)# 4. Evaluate `f` `runs` times at each parameter combination (backup is optional):path <- file.path(tempdir(), paste0("backup_", format(Sys.time(), "%Y-%m-%d-%H-%M-%S")))object$go(runs = 2, backup = TRUE, path = path)# 5. Access the results:object$results# 6. Check if cases are pending or if an error occurred:object$cases# 7. Restore simulation results from backup:object_restored <- Simulator$new(use_backup = path)print(object_restored)## Not run: all.equal(object, object_restored)# 8. Run more simulations and pending simulations (if any):object_restored$go(runs = 2)Storage R6 Object
Description
Provides a simple indexing interface for list elements based on R6.Basically, it allows to store items in a list and to regain them based onidentifiers defined by the user.
Value
The output depends on the method:
$new()returns aStorageobject.$add(),$remove(), and$print()invisibly return theStorageobject (to allow for method chaining)$get()returns the requested element(s)$number()returns aninteger$indices()return anintegervector
Setting identifiers
An identifier is acharacter, typically a binary property. Identifierscan be negated by placing an exclamation mark ("!") in front of them.Identifiers that have been assigned to other elements previously do not needto be specified again for new elements; instead, a default value can be used.This default value can be defined either globally for all cases (via the$missing_identifier field) or separately for each specific case (viathe method argument).
User confirmation
If desired, the user can be asked for confirmation when adding, extracting,or removing elements using identifiers. This behavior can be set globallythrough the$confirm field or customized separately for each specificcase via the method argument.
Active bindings
identifier[
character()]
The identifiers used.confirm[
logical(1)]
The default value for confirmations.missing_identifier[
logical(1)]
The default value for not specified identifiers.hide_warnings[
logical(1)]
Hide warnings (for example if unknown identifiers are selected)?
Methods
Public methods
Methodnew()
Initializing aStorage object.
Usage
Storage$new()
Methodadd()
Adding an element.
Usage
Storage$add( x, identifier, confirm = interactive() & self$confirm, missing_identifier = self$missing_identifier)
Arguments
x[
any]
An object to be saved.identifier[
character()]
Pne or more identifiers (the identifier"all"is reserved to selectall elements).confirm[
logical(1)]
Prompted for confirmation?missing_identifier[
logical(1)| NA]
The value for not specified identifiers.
Methodget()
Getting elements.
Usage
Storage$get( identifier = character(), ids = integer(), logical = "and", confirm = interactive() & self$confirm, missing_identifier = self$missing_identifier, id_names = FALSE)
Arguments
identifier[
character()]
Pne or more identifiers (the identifier"all"is reserved to selectall elements).ids[
integer()]
One or more ids.logical[
character(1)]
In the case that multiple identifiers are selected, how should they becombined? Options are:"and"(the default): the identifiers are combined with logical and(all identifiers must beTRUE)"or": the identifiers are combined with logical or (at least oneidentifier must beTRUE)
confirm[
logical(1)]
Prompted for confirmation?missing_identifier[
logical(1)| NA]
The value for not specified identifiers.id_names[
logical(1)]
Name the elements according to their ids?
Methodremove()
removing elements
Usage
Storage$remove( identifier = character(), ids = integer(), logical = "and", confirm = interactive() & self$confirm, missing_identifier = self$missing_identifier, shift_ids = TRUE)
Arguments
identifier[
character()]
Pne or more identifiers (the identifier"all"is reserved to selectall elements).ids[
integer()]
One or more ids.logical[
character(1)]
In the case that multiple identifiers are selected, how should they becombined? Options are:"and"(the default): the identifiers are combined with logical and(all identifiers must beTRUE)"or": the identifiers are combined with logical or (at least oneidentifier must beTRUE)
confirm[
logical(1)]
Prompted for confirmation?missing_identifier[
logical(1)| NA]
The value for not specified identifiers.shift_ids[
logical(1)]
Shift ids when in-between elements are removed?
Methodnumber()
Computing the number of identified elements.
Usage
Storage$number( identifier = "all", missing_identifier = self$missing_identifier, logical = "and", confirm = FALSE)
Arguments
identifier[
character()]
Pne or more identifiers (the identifier"all"is reserved to selectall elements).missing_identifier[
logical(1)| NA]
The value for not specified identifiers.logical[
character(1)]
In the case that multiple identifiers are selected, how should they becombined? Options are:"and"(the default): the identifiers are combined with logical and(all identifiers must beTRUE)"or": the identifiers are combined with logical or (at least oneidentifier must beTRUE)
confirm[
logical(1)]
Prompted for confirmation?
Methodindices()
Returning indices based on defined identifiers.
Usage
Storage$indices( identifier = "all", logical = "and", confirm = interactive() & self$confirm)
Arguments
identifier[
character()]
Pne or more identifiers (the identifier"all"is reserved to selectall elements).logical[
character(1)]
In the case that multiple identifiers are selected, how should they becombined? Options are:"and"(the default): the identifiers are combined with logical and(all identifiers must beTRUE)"or": the identifiers are combined with logical or (at least oneidentifier must beTRUE)
confirm[
logical(1)]
Prompted for confirmation?
Methodprint()
Printing details of the saved elements.
Usage
Storage$print(...)
Arguments
...Currently not used.
See Also
Other package helpers:Dictionary,check_missing(),find_namespace_calls(),identical_structure(),input_check_response(),match_arg(),package_logo(),print_data.frame(),print_matrix(),system_information(),unexpected_error(),user_confirm()
Examples
### 1. Create a `Storage` object:my_storage <- Storage$new()# 2. Add elements along with identifiers:my_storage$ add(42, c("number", "rational"))$ add(pi, c("number", "!rational"))$ add("fear of black cats", c("text", "!rational"))$ add("wearing a seat belt", c("text", "rational"))$ add(mean, "function")# 3. What elements are stored?print(my_storage)# 4. Extract elements based on identifiers:my_storage$get("rational")my_storage$get("!rational")my_storage$get(c("text", "!rational"))my_storage$get("all") # get all elementsmy_storage$get(c("text", "!text"))my_storage$get(c("text", "!text"), logical = "or")# 5. Extract elements based on ids:my_storage$get(ids = 4:5)my_storage$get(ids = 4:5, id_names = TRUE) # add the ids as namesCheck correlation matrix
Description
These functions check whether the input fulfills the properties of acorrelation matrix.
Usage
check_correlation_matrix(x, dim = NULL, tolerance = sqrt(.Machine$double.eps))assert_correlation_matrix( x, dim = NULL, tolerance = sqrt(.Machine$double.eps), .var.name = checkmate::vname(x), add = NULL)test_correlation_matrix(x, dim = NULL, tolerance = sqrt(.Machine$double.eps))Arguments
x | [any] |
dim | [ |
tolerance | [ |
.var.name | [ |
add | [ |
Value
Same as documented incheck_matrix.
See Also
Other matrix helpers:check_covariance_matrix(),check_transition_probability_matrix(),cov_to_chol(),diff_cov(),insert_matrix_column(),matrix_diagonal_indices(),matrix_indices(),sample_correlation_matrix(),sample_covariance_matrix(),sample_transition_probability_matrix(),stationary_distribution()
Examples
M <- matrix(c(1, 0.9, 0.9, 0.9, 1, -0.9, 0.9, -0.9, 1), nrow = 3)check_correlation_matrix(M)test_correlation_matrix(M)## Not run: assert_correlation_matrix(M)## End(Not run)Check covariance matrix
Description
These functions check whether the input fulfills the properties of acovariance matrix.
Usage
check_covariance_matrix(x, dim = NULL, tolerance = sqrt(.Machine$double.eps))assert_covariance_matrix( x, dim = NULL, tolerance = sqrt(.Machine$double.eps), .var.name = checkmate::vname(x), add = NULL)test_covariance_matrix(x, dim = NULL, tolerance = sqrt(.Machine$double.eps))Arguments
x | [any] |
dim | [ |
tolerance | [ |
.var.name | [ |
add | [ |
Value
Same as documented incheck_matrix.
See Also
Other matrix helpers:check_correlation_matrix(),check_transition_probability_matrix(),cov_to_chol(),diff_cov(),insert_matrix_column(),matrix_diagonal_indices(),matrix_indices(),sample_correlation_matrix(),sample_covariance_matrix(),sample_transition_probability_matrix(),stationary_distribution()
Examples
M <- matrix(c(1, 2, 3, 2, 1, 2, 3, 2, 1), nrow = 3)check_covariance_matrix(M)test_covariance_matrix(M)## Not run: assert_covariance_matrix(M)## End(Not run)Check list of lists
Description
These functions check whether the input is a list that contains listelements.
Usage
check_list_of_lists(x, len = NULL)assert_list_of_lists( x, len = NULL, .var.name = checkmate::vname(x), add = NULL)test_list_of_lists(x, len = NULL)Arguments
x | [any] |
len | [ |
.var.name | [ |
add | [ |
Value
Same as documented incheck_list.
See Also
Other list helpers:merge_lists()
Examples
L <- list(list(1), list(2), 3)check_list_of_lists(L)test_list_of_lists(L)## Not run: assert_list_of_lists(L)## End(Not run)Check missing formal argument
Description
These functions check whether a value was specified as an argument to afunction.
Usage
check_missing(x)assert_missing(x)test_missing(x)Arguments
x | [ |
Value
Depending on the function prefix:
If the check is successful,
assert_missing()returnsxinvisibly,whereascheck_missing()andtest_missing()returnTRUE.If the check is not successful,
assert_missing()throws an error message,test_missing()returnsFALSE, andcheck_missing()returns a string withthe error message.
See Also
Other package helpers:Dictionary,Storage,find_namespace_calls(),identical_structure(),input_check_response(),match_arg(),package_logo(),print_data.frame(),print_matrix(),system_information(),unexpected_error(),user_confirm()
Examples
f <- function(x) { check_missing(x)}f()g <- function(x) { test_missing(x)}g()h <- function(x) { assert_missing(x)}## Not run: h()## End(Not run)Check numeric vector
Description
These functions check whether the input is a numeric vector.
Usage
check_numeric_vector( x, lower = -Inf, upper = Inf, finite = FALSE, any.missing = TRUE, all.missing = TRUE, len = NULL, min.len = NULL, max.len = NULL, unique = FALSE, sorted = FALSE, names = NULL, typed.missing = FALSE, null.ok = FALSE)assert_numeric_vector( x, lower = -Inf, upper = Inf, finite = FALSE, any.missing = TRUE, all.missing = TRUE, len = NULL, min.len = NULL, max.len = NULL, unique = FALSE, sorted = FALSE, names = NULL, typed.missing = FALSE, null.ok = FALSE, .var.name = checkmate::vname(x), add = NULL)test_numeric_vector( x, lower = -Inf, upper = Inf, finite = FALSE, any.missing = TRUE, all.missing = TRUE, len = NULL, min.len = NULL, max.len = NULL, unique = FALSE, sorted = FALSE, names = NULL, typed.missing = FALSE, null.ok = FALSE)Arguments
x | [any] |
lower | [ |
upper | [ |
finite | [ |
any.missing | [ |
all.missing | [ |
len | [ |
min.len | [ |
max.len | [ |
unique | [ |
sorted | [ |
names | [ |
typed.missing | [ |
null.ok | [ |
.var.name | [ |
add | [ |
Value
Same as documented incheck_numeric.
See Also
Other vector helpers:check_probability_vector(),chunk_vector(),equidistant_vectors(),insert_vector_entry(),map_indices(),match_numerics(),permutations(),split_vector_at(),subsets(),vector_occurrence()
Examples
x <- c(1, 2, "3")check_numeric_vector(x)test_numeric_vector(x)## Not run: assert_numeric_vector(x)## End(Not run)Check probability vector
Description
These functions check whether the input fulfills the properties of aprobability matrix.
Usage
check_probability_vector(x, len = NULL, tolerance = sqrt(.Machine$double.eps))assert_probability_vector( x, len = NULL, tolerance = sqrt(.Machine$double.eps), .var.name = checkmate::vname(x), add = NULL)test_probability_vector(x, len = NULL, tolerance = sqrt(.Machine$double.eps))Arguments
x | [any] |
len | [ |
tolerance | [ |
.var.name | [ |
add | [ |
Value
Same as documented incheck_numeric.
See Also
Other vector helpers:check_numeric_vector(),chunk_vector(),equidistant_vectors(),insert_vector_entry(),map_indices(),match_numerics(),permutations(),split_vector_at(),subsets(),vector_occurrence()
Examples
p <- c(0.2, 0.3, 0.6)check_probability_vector(p)test_probability_vector(p)## Not run: assert_probability_vector(p)## End(Not run)Check transition probability matrix
Description
These functions check whether the input is a transition probability matrix.
Usage
check_transition_probability_matrix( x, dim = NULL, tolerance = sqrt(.Machine$double.eps))assert_transition_probability_matrix( x, dim = NULL, tolerance = sqrt(.Machine$double.eps), .var.name = checkmate::vname(x), add = NULL)test_transition_probability_matrix( x, dim = NULL, tolerance = sqrt(.Machine$double.eps))Arguments
x | [any] |
dim | [ |
tolerance | [ |
.var.name | [ |
add | [ |
Value
Same as documented incheck_matrix.
See Also
Other matrix helpers:check_correlation_matrix(),check_covariance_matrix(),cov_to_chol(),diff_cov(),insert_matrix_column(),matrix_diagonal_indices(),matrix_indices(),sample_correlation_matrix(),sample_covariance_matrix(),sample_transition_probability_matrix(),stationary_distribution()
Examples
T <- matrix(c(0.8, 0.2, 0.1, 0.1, 0.7, 0.4, 0.1, 0.1, 0.6), nrow = 3)check_transition_probability_matrix(T)test_transition_probability_matrix(T)## Not run: assert_transition_probability_matrix(T)## End(Not run)Split a vector into chunks
Description
This function either
splits a vector into
nchunks of equal size (type = 1),splits a vector into chunks of size
n(type = 2).
Usage
chunk_vector(x, n, type = 1, strict = FALSE)Arguments
x | [atomic()'] |
n | [ |
type | [
|
strict | [ |
Value
Alist.
See Also
Other vector helpers:check_numeric_vector(),check_probability_vector(),equidistant_vectors(),insert_vector_entry(),map_indices(),match_numerics(),permutations(),split_vector_at(),subsets(),vector_occurrence()
Examples
x <- 1:12chunk_vector(x, n = 3, type = 1)chunk_vector(x, n = 3, type = 2)try(chunk_vector(x, n = 5, strict = TRUE))Simulate correlated regressor values
Description
This function simulates regressor values from various marginal distributionswith custom correlations.
Usage
correlated_regressors( labels, n = 100, marginals = list(), correlation = diag(length(labels)), verbose = FALSE)Arguments
labels | [ |
n | [ |
marginals | [ Each list entry must be named according to a regressor label, and thefollowing distributions are currently supported:
|
correlation | [ |
verbose | [ |
Value
Adata.frame withn rows andlength(labels) columns.
References
This function heavily depends on the{SimMultiCorrData} package.
See Also
Other simulation helpers:Simulator,ddirichlet_cpp(),dmixnorm_cpp(),dmvnorm_cpp(),dtnorm_cpp(),dwishart_cpp(),gaussian_tv(),simulate_markov_chain()
Examples
labels <- c("P", "C", "N1", "N2", "U")n <- 100marginals <- list( "P" = list(type = "poisson", lambda = 2), "C" = list(type = "categorical", p = c(0.3, 0.2, 0.5)), "N1" = list(type = "normal", mean = -1, sd = 2), "U" = list(type = "uniform", min = -2, max = -1))correlation <- matrix( c(1, -0.3, -0.1, 0, 0.5, -0.3, 1, 0.3, -0.5, -0.7, -0.1, 0.3, 1, -0.3, -0.3, 0, -0.5, -0.3, 1, 0.1, 0.5, -0.7, -0.3, 0.1, 1), nrow = 5, ncol = 5)data <- correlated_regressors( labels = labels, n = n, marginals = marginals, correlation = correlation)head(data)Cholesky root of covariance matrix
Description
These functions compute the Cholesky root elements of a covariance matrixand, conversely, build a covariance matrix from its Cholesky root elements.
Usage
cov_to_chol(cov, unique = TRUE)chol_to_cov(chol)unique_chol(chol)Arguments
cov | [ It can also be the zero matrix, in which case the Cholesky root is defined asthe zero matrix. |
unique | [ |
chol | [ |
Value
Forcov_to_chol anumericvector of Cholesky rootelements.
Forchol_to_cov a covariancematrix.
See Also
Other matrix helpers:check_correlation_matrix(),check_covariance_matrix(),check_transition_probability_matrix(),diff_cov(),insert_matrix_column(),matrix_diagonal_indices(),matrix_indices(),sample_correlation_matrix(),sample_covariance_matrix(),sample_transition_probability_matrix(),stationary_distribution()
Examples
cov <- sample_covariance_matrix(4)chol <- cov_to_chol(cov)all.equal(cov, chol_to_cov(chol))Dirichlet distribution
Description
The functionddirichlet() computes the density of a Dirichlet distribution.
The functionrdirichlet() samples from a Dirichlet distribution.
The functions with suffix_cpp perform no input checks, hence are faster.
Usage
ddirichlet_cpp(x, concentration, log = FALSE)rdirichlet_cpp(concentration)ddirichlet(x, concentration, log = FALSE)rdirichlet(n = 1, concentration)Arguments
x | [ |
concentration | [ |
log | [ |
n | [ |
Value
Forddirichlet(): The density value.
Forrdirichlet(): Ifn = 1 avector of lengthp, elseamatrix of dimensionn timesp with samples as rows.
See Also
Other simulation helpers:Simulator,correlated_regressors(),dmixnorm_cpp(),dmvnorm_cpp(),dtnorm_cpp(),dwishart_cpp(),gaussian_tv(),simulate_markov_chain()
Examples
x <- c(0.5, 0.3, 0.2)concentration <- 1:3# compute densityddirichlet(x = x, concentration = concentration)ddirichlet(x = x, concentration = concentration, log = TRUE)# samplerdirichlet(concentration = 1:3)rdirichlet(n = 4, concentration = 1:2)Deletingdata.frame columns
Description
This function deletes columns of adata.frame by name.
Usage
delete_columns_data.frame(df, column_names)Arguments
df | [ |
column_names | [ |
Value
The inputdf without the columns defined bycolumn_names.
See Also
Other data.frame helpers:group_data.frame(),occurrence_info(),round_data.frame()
Examples
df <- data.frame("label" = c("A", "B"), "number" = 1:10)delete_columns_data.frame(df = df, column_names = "label")delete_columns_data.frame(df = df, column_names = "number")delete_columns_data.frame(df = df, column_names = c("label", "number"))Difference and un-difference covariance matrix
Description
These functions difference and un-difference random vectors and covariancematrices.
Usage
diff_cov(cov, ref = 1)undiff_cov(cov_diff, ref = 1)delta(ref = 1, dim)M(ranking = seq_len(dim), dim)Arguments
cov,cov_diff | [ |
ref | [ |
dim | [ |
ranking | [ |
Details
Assumex \sim N(0, \Sigma) is a multivariate normally distributedrandom vector of dimensionn. We may want to consider the differencedvector
\tilde x = (x_1 - x_k, x_2 - x_k, \dots, x_n - x_k)',
excludingthekth element (hence,\tilde x is of dimension(n - 1) \times 1). Formally,\tilde x = \Delta_k x, where\Delta_k is a difference operator that depends on the referencerowk. More precise,\Delta_k is the identity matrix of dimensionn without rowk and with-1s in columnk.The difference operator\Delta_k can be computed viadelta(ref = k, dim = n).
Then,\tilde x \sim N(0, \tilde \Sigma), where
\tilde{\Sigma} = \Delta_k \Sigma \Delta_k'
is the differenced covariance matrix with respect to rowk = 1,\dots,n.The differenced covariance matrix\tilde \Sigma can be computed viadiff_delta(Sigma, ref = k).
Since\Delta_k is a non-bijective mapping,\Sigma cannot beuniquely restored from\tilde \Sigma. However, it is possible tocompute a non-unique solution\Sigma_0, such that\Delta_k \Sigma_0 \Delta_k = \tilde \Sigma. For such a non-uniquesolution, we add a column and a row of zerosat column and row numberk to\tilde{\Sigma}, respectively.An "un-differenced" covariance matrix\Sigma_0 can be computed viaundiff_delta(Sigma_diff, ref = k).
As a alternative to\Delta_k, the functionM() returns a matrix fortaking differences such that the resulting vector is negative.
Value
A (differenced or un-differenced) covariancematrix.
See Also
Other matrix helpers:check_correlation_matrix(),check_covariance_matrix(),check_transition_probability_matrix(),cov_to_chol(),insert_matrix_column(),matrix_diagonal_indices(),matrix_indices(),sample_correlation_matrix(),sample_covariance_matrix(),sample_transition_probability_matrix(),stationary_distribution()
Examples
n <- 4Sigma <- sample_covariance_matrix(dim = n)k <- 2x <- c(1, 3, 2, 4)# build difference operatordelta_k <- delta(ref = k, dim = n)# difference vectordelta_k %*% x# difference Sigma(Sigma_diff <- diff_cov(Sigma, ref = k))# un-difference Sigma(Sigma_0 <- undiff_cov(Sigma_diff, ref = k))# difference againSigma_diff_2 <- diff_cov(Sigma_0, ref = k)all.equal(Sigma_diff, Sigma_diff_2)# difference such that the resulting vector is negativeM(ranking = order(x, decreasing = TRUE), dim = n) %*% xMixture of normal distributions
Description
The functiondmixnorm() computes the density of a mixture of multivariatenormal distribution.
The functionpmixnorm() computes the cumulative distribution function of amixture of multivariate normal distribution.
The functionrmixnorm() samples from a mixture of multivariate normaldistribution.
The functions with suffix_cpp perform no input checks, hence are faster.
The univariate normal mixture is available as the special casep = 1.
Usage
dmixnorm_cpp(x, mean, Sigma, proportions)pmixnorm_cpp(x, mean, Sigma, proportions, abseps = 0.001)rmixnorm_cpp(mean, Sigma, proportions)dmixnorm(x, mean, Sigma, proportions)pmixnorm(x, mean, Sigma, proportions, abseps = 0.001)rmixnorm(n = 1, mean, Sigma, proportions)Arguments
x | [ |
mean | [ |
Sigma | [ |
proportions | [ If proportions do not sum to unity, they are rescaled to do so. |
abseps | [ |
n | [ |
Details
pmixnorm() is based onmvtnorm::pmvnorm with the randomizedQuasi-Monte-Carlo procedure by Genz and Bretz. The argumentabseps controlsthe accuracy of the Gaussian integral approximation.
Value
Fordmixnorm(): The density value.
Forpmixnorm(): The value of the distribution function.
Forrmixnorm(): Ifn = 1 avector of lengthp (notethat it is a column vector forrmixnorm_cpp()), elseamatrix of dimensionn timesp with samples as rows.
See Also
Other simulation helpers:Simulator,correlated_regressors(),ddirichlet_cpp(),dmvnorm_cpp(),dtnorm_cpp(),dwishart_cpp(),gaussian_tv(),simulate_markov_chain()
Examples
x <- c(0, 0)mean <- matrix(c(-1, -1, 0, 0), ncol = 2)Sigma <- matrix(c(diag(2), diag(2)), ncol = 2)proportions <- c(0.7, 0.3)# compute densitydmixnorm(x = x, mean = mean, Sigma = Sigma, proportions = proportions)# compute CDFpmixnorm(x = x, mean = mean, Sigma = Sigma, proportions = proportions)# samplermixnorm(n = 3, mean = mean, Sigma = Sigma, proportions = proportions)Multivariate normal distribution
Description
The functiondmvnorm() computes the density of a multivariate normaldistribution.
The functionpmvnorm() computes the cumulative distribution function of amultivariate normal distribution.
The functionrmvnorm() samples from a multivariate normal distribution.
The functions with suffix_cpp perform no input checks, hence are faster.
The univariate normal distribution is available as the special casep = 1.
Usage
dmvnorm_cpp(x, mean, Sigma, log = FALSE)pmvnorm_cpp(x, mean, Sigma, abseps = 0.001)rmvnorm_cpp(mean, Sigma, log = FALSE)dmvnorm(x, mean, Sigma, log = FALSE)pmvnorm(x, mean, Sigma, abseps = 0.001)rmvnorm(n = 1, mean, Sigma, log = FALSE)Arguments
x | [ |
mean | [ For the functions without suffix |
Sigma | [ For For the functions without suffix |
log | [ |
abseps | [ |
n | [ |
Details
pmvnorm() just callsmvtnorm::pmvnorm with the randomizedQuasi-Monte-Carlo procedure by Genz and Bretz. The argumentabseps controlsthe accuracy of the Gaussian integral approximation.
Value
Fordmvnorm(): The density value.
Forpmvnorm(): The value of the distribution function.
Forrmvnorm(): Ifn = 1 avector of lengthp (notethat it is a column vector forrmvnorm_cpp()), elseamatrix of dimensionn timesp with samples as rows.
See Also
Other simulation helpers:Simulator,correlated_regressors(),ddirichlet_cpp(),dmixnorm_cpp(),dtnorm_cpp(),dwishart_cpp(),gaussian_tv(),simulate_markov_chain()
Examples
x <- c(0, 0)mean <- c(0, 0)Sigma <- diag(2)# compute densitydmvnorm(x = x, mean = mean, Sigma = Sigma)dmvnorm(x = x, mean = mean, Sigma = Sigma, log = TRUE)# compute CDFpmvnorm(x = x, mean = mean, Sigma = Sigma)# samplermvnorm(n = 3, mean = mean, Sigma = Sigma)rmvnorm(mean = mean, Sigma = Sigma, log = TRUE)Measure computation time
Description
This function measures the computation time of a call.
Usage
do.call_timed(what, args, units = "secs")Arguments
what,args | Passed to |
units | Passed to |
Details
This function is a wrapper fordo.call.
Value
A list of the two elements"result" (the results of thedo.callcall) and"time" (the computation time).
See Also
Other function helpers:function_arguments(),function_body(),function_defaults(),quiet(),timed(),try_silent(),variable_name()
Examples
## Not run: what <- function(s) { Sys.sleep(s) return(s)}args <- list(s = 1)do.call_timed(what = what, args = args)## End(Not run)Truncated normal distribution
Description
The functiondtnorm() computes the density of a truncated normaldistribution.
The functionrtnorm() samples from a truncated normal distribution.
The functiondttnorm() andrttnorm() compute the density and sample froma two-sided truncated normal distribution, respectively.
The functions with suffix_cpp perform no input checks, hence are faster.
Usage
dtnorm_cpp(x, mean, sd, point, above, log = FALSE)dttnorm_cpp(x, mean, sd, lower, upper, log = FALSE)rtnorm_cpp(mean, sd, point, above, log = FALSE)rttnorm_cpp(mean, sd, lower, upper, log = FALSE)dtnorm(x, mean, sd, point, above, log = FALSE)dttnorm(x, mean, sd, lower, upper, log = FALSE)rtnorm(mean, sd, point, above, log = FALSE)rttnorm(mean, sd, lower, upper, log = FALSE)Arguments
x | [ |
mean | [ |
sd | [ |
point,lower,upper | [ |
above | [ |
log | [ |
Value
Fordtnorm() anddttnorm(): The density value.
Forrtnorm() andrttnorm(): The random draw
See Also
Other simulation helpers:Simulator,correlated_regressors(),ddirichlet_cpp(),dmixnorm_cpp(),dmvnorm_cpp(),dwishart_cpp(),gaussian_tv(),simulate_markov_chain()
Examples
x <- c(0, 0)mean <- c(0, 0)Sigma <- diag(2)# compute densitydmvnorm(x = x, mean = mean, Sigma = Sigma)dmvnorm(x = x, mean = mean, Sigma = Sigma, log = TRUE)# samplermvnorm(n = 3, mean = mean, Sigma = Sigma)rmvnorm(mean = mean, Sigma = Sigma, log = TRUE)Wishart distribution
Description
The functiondwishart() computes the density of a Wishart distribution.
The functionrwishart() samples from a Wishart distribution.
The functions with suffix_cpp perform no input checks, hence are faster.
Usage
dwishart_cpp(x, df, scale, log = FALSE, inv = FALSE)rwishart_cpp(df, scale, inv = FALSE)dwishart(x, df, scale, log = FALSE, inv = FALSE)rwishart(df, scale, inv = FALSE)Arguments
x | [ |
df | [ |
scale | [ |
log | [ |
inv | [ |
Value
Fordwishart(): The density value.
Forrwishart(): Amatrix, the random draw.
See Also
Other simulation helpers:Simulator,correlated_regressors(),ddirichlet_cpp(),dmixnorm_cpp(),dmvnorm_cpp(),dtnorm_cpp(),gaussian_tv(),simulate_markov_chain()
Examples
x <- diag(2)df <- 6scale <- matrix(c(1, -0.3, -0.3, 0.8), ncol = 2)# compute densitydwishart(x = x, df = df, scale = scale)dwishart(x = x, df = df, scale = scale, log = TRUE)dwishart(x = x, df = df, scale = scale, inv = TRUE)# samplerwishart(df = df, scale = scale)rwishart(df = df, scale = scale, inv = TRUE)# expectation of Wishart is df * scalen <- 100replicate(n, rwishart(df = df, scale = scale), simplify = FALSE) |> Reduce(f = "+") / ndf * scale# expectation of inverse Wishart is scale / (df - p - 1)n <- 100replicate(n, rwishart(df = df, scale = scale, TRUE), simplify = FALSE) |> Reduce(f = "+") / nscale / (df - 2 - 1)Generate equidistant vectors in Euclidean space
Description
This function constructs the coordinates of vertices of a regular simplexin\mathbb{R}^{\code{dim}} and returns the firstn of them,
scaled so that the pairwise Euclidean distance between any two verticesequals
dist,and centered so their centroid is at
center.
Usage
equidistant_vectors(dim, n = dim + 1, dist = 1, center = rep(0, dim))Arguments
dim | [ |
n | [ |
dist | [ |
center | [ |
Value
A matrix, where each column is a vertex of the simplex.
See Also
Other vector helpers:check_numeric_vector(),check_probability_vector(),chunk_vector(),insert_vector_entry(),map_indices(),match_numerics(),permutations(),split_vector_at(),subsets(),vector_occurrence()
Examples
dim <- n <- 3(dist <- runif(1))(center <- rnorm(dim))(V <- equidistant_vectors(dim = dim, n = n, dist = dist, center = center))rowMeans(V)dist(t(V))Namespace calls
Description
This function searches for namespace calls in.R files, i.e., code lines ofthe format<package name>::<function name>.
Usage
find_namespace_calls(path = "R", triple_colon = FALSE, as_list = FALSE)Arguments
path | [ |
triple_colon | [ |
as_list | [ |
Value
Adata.frame. Ifas_list = TRUE, alist.
See Also
Other package helpers:Dictionary,Storage,check_missing(),identical_structure(),input_check_response(),match_arg(),package_logo(),print_data.frame(),print_matrix(),system_information(),unexpected_error(),user_confirm()
Examples
## Not run: find_namespace_calls()find_namespace_calls(as_list = TRUE)## End(Not run)Get function arguments
Description
This function returns the names of function arguments.
Usage
function_arguments(f, with_default = TRUE, with_ellipsis = TRUE)Arguments
f | [ |
with_default | [ |
with_ellipsis | [ |
Value
Acharacter vector.
See Also
Other function helpers:do.call_timed(),function_body(),function_defaults(),quiet(),timed(),try_silent(),variable_name()
Examples
f <- function(a, b = 1, c = "", ...) { }function_arguments(f)function_arguments(f, with_default = FALSE)function_arguments(f, with_ellipsis = FALSE)Extract function body
Description
This function extracts the body of a function as a singlecharacter.
Usage
function_body(fun, braces = FALSE, nchar = getOption("width") - 4)Arguments
fun | [ |
braces | [ |
nchar | [ |
Value
Acharacter, the body off.
See Also
Other function helpers:do.call_timed(),function_arguments(),function_defaults(),quiet(),timed(),try_silent(),variable_name()
Examples
fun <- mean.defaultfunction_body(fun)function_body(fun, braces = TRUE)function_body(fun, nchar = 30)Get default function arguments
Description
This function returns the default function arguments (if any).
Usage
function_defaults(f, exclude = NULL)Arguments
f | [ |
exclude | [ Can be |
Value
A namedlist.
See Also
Other function helpers:do.call_timed(),function_arguments(),function_body(),quiet(),timed(),try_silent(),variable_name()
Examples
f <- function(a, b = 1, c = "", ...) { }function_defaults(f)function_defaults(f, exclude = "b")Gaussian total variation
Description
Computes the total variation (TV) between two multivariate Gaussiandistributionsf_1,f_2:
\mathrm{TV}(f_1, f_2) = \tfrac{1}{2}\int_{\mathbb{R}^p} \lvert f_1(x) - f_2(x) \rvert \, dx.
The value ranges from 0 (identical distributions) to 1 (no overlap).
Usage
gaussian_tv( mean1, mean2, Sigma1, Sigma2, method = c("auto", "mc", "cubature"), n = 10000, tol_equal = 1e-06, eps = 1e-06)Arguments
mean1,mean2 | [ |
Sigma1,Sigma2 | [ |
method | [
|
n | [ |
tol_equal | [ |
eps | [ |
Value
The total variation in [0, 1].
See Also
Other simulation helpers:Simulator,correlated_regressors(),ddirichlet_cpp(),dmixnorm_cpp(),dmvnorm_cpp(),dtnorm_cpp(),dwishart_cpp(),simulate_markov_chain()
Examples
### univariate casemean1 <- 0mean2 <- 1Sigma1 <- Sigma2 <- matrix(1)gaussian_tv(mean1, mean2, Sigma1, Sigma2)### bivariate casemean1 <- c(0, 0)mean2 <- c(1, 1)Sigma1 <- matrix(c(1, 0.2, 0.2, 1), ncol = 2)Sigma2 <- matrix(c(1.5, -0.3, -0.3, 1), ncol = 2)gaussian_tv(mean1, mean2, Sigma1, Sigma2, method = "mc", n = 1e3)Grouping of adata.frame
Description
This function groups adata.frame according to values of one column.
Usage
group_data.frame(df, by, keep_by = TRUE)Arguments
df | [ |
by | [ |
keep_by | [ |
Value
Alist ofdata.frames, subsets ofdf.
See Also
Other data.frame helpers:delete_columns_data.frame(),occurrence_info(),round_data.frame()
Examples
df <- data.frame("label" = c("A", "B"), "number" = 1:10)group_data.frame(df = df, by = "label")group_data.frame(df = df, by = "label", keep_by = FALSE)Check if two objects have identical structure
Description
This function determines whether two objects have the same structure,
Usage
identical_structure(x, y)Arguments
x,y | [ |
Value
EitherTRUE ifx andy have the same structure, andFALSE, else.
References
Inspired byhttps://stackoverflow.com/a/45548885/15157768.
See Also
Other package helpers:Dictionary,Storage,check_missing(),find_namespace_calls(),input_check_response(),match_arg(),package_logo(),print_data.frame(),print_matrix(),system_information(),unexpected_error(),user_confirm()
Examples
identical_structure(integer(1), 1L)identical_structure(diag(2), matrix(rnorm(4), 2, 2))identical_structure(diag(2), data.frame(diag(2)))Standardized response to input check
Description
This function provides a standardized response to input checks, ensuringconsistency.
Usage
input_check_response( check, var_name = NULL, error = TRUE, prefix = "Input {.var {var_name}} is bad:")Arguments
check | [ Can also be a |
var_name | [ |
error | [ |
prefix | [ |
Value
TRUE ifcheck isTRUE (or any element incheck isTRUE, ifcheckis alist) . Else, depending onerror:
If
errorisTRUE, throws an error.If
errorisFALSE, returnsFALSE.
See Also
Other package helpers:Dictionary,Storage,check_missing(),find_namespace_calls(),identical_structure(),match_arg(),package_logo(),print_data.frame(),print_matrix(),system_information(),unexpected_error(),user_confirm()
Examples
x <- "1"y <- 1### check is successfulinput_check_response( check = checkmate::check_character(x), var_name = "x", error = TRUE)### alternative checksinput_check_response( check = list( checkmate::check_character(x), checkmate::check_character(y) ), var_name = "x", error = TRUE)### standardized check response## Not run: input_check_response( check = checkmate::check_character(y), var_name = "y", error = TRUE)input_check_response( check = list( checkmate::check_flag(x), checkmate::check_character(y) ), var_name = "y", error = TRUE)## End(Not run)Insert column in matrix
Description
This function inserts a column into a matrix.
Usage
insert_matrix_column(A, x, p)Arguments
A | [ |
x | [ Can also be a single value. |
p | [
|
Value
Amatrix.
See Also
Other matrix helpers:check_correlation_matrix(),check_covariance_matrix(),check_transition_probability_matrix(),cov_to_chol(),diff_cov(),matrix_diagonal_indices(),matrix_indices(),sample_correlation_matrix(),sample_covariance_matrix(),sample_transition_probability_matrix(),stationary_distribution()
Examples
A <- diag(3)x <- 1:3insert_matrix_column(A, x, 0)insert_matrix_column(A, x, 1)insert_matrix_column(A, x, 2)insert_matrix_column(A, x, 3)### also single valuex <- 2insert_matrix_column(A, x, 0)### also multiple positionsinsert_matrix_column(A, x, 0:3)### also trivial caseinsert_matrix_column(matrix(nrow = 0, ncol = 0), integer(), integer())Insert entry in vector
Description
This function inserts a value into a vector.
Usage
insert_vector_entry(v, x, p)Arguments
v | [ |
x | [ |
p | [
|
Value
Avector.
See Also
Other vector helpers:check_numeric_vector(),check_probability_vector(),chunk_vector(),equidistant_vectors(),map_indices(),match_numerics(),permutations(),split_vector_at(),subsets(),vector_occurrence()
Examples
v <- 1:3x <- 0insert_vector_entry(v, x, 0)insert_vector_entry(v, x, 1)insert_vector_entry(v, x, 2)insert_vector_entry(v, x, 3)### also multiple positionsinsert_vector_entry(v, x, 0:3)### also trivial caseinsert_vector_entry(integer(), integer(), integer())Map indices
Description
This function maps indices from an input vector to corresponding sequences ofgrouped indices. Each element from the input specifies a group to be mappedfrom the sequence, determined by the grouping sizen.
Usage
map_indices(indices, n)Arguments
indices | [ |
n | [ |
Details
This function is useful when working with indices arranged in fixed-sizegroups, where each group can be referenced by a single index. For example, ifindices are structured in chunks of 3, calling this function withn = 3will map the corresponding groups of 3 consecutive indices for the giveninput indices, see the examples.
Value
Anintegervector, containing the mapped indices according to thespecified group size.
See Also
Other vector helpers:check_numeric_vector(),check_probability_vector(),chunk_vector(),equidistant_vectors(),insert_vector_entry(),match_numerics(),permutations(),split_vector_at(),subsets(),vector_occurrence()
Examples
# Example: Map indices based on groups of 3map_indices(c(1, 3, 5), 3)Argument matching
Description
This function matches function arguments and is a modified version ofmatch.arg.
Usage
match_arg(arg, choices, several.ok = FALSE, none.ok = FALSE)Arguments
arg | [ |
choices | [ |
several.ok | [ |
none.ok | [ |
Value
The un-abbreviated version of the exact or unique partial match if there isone. Otherwise, an error is signaled ifseveral.ok isFALSEornone.ok isFALSE.Whenseveral.ok isTRUE and (at least) one element ofarg has a match, all un-abbreviated versions of matches are returned.Whennone.ok isTRUE andarg has zero elements,character(0) is returned.
See Also
Other package helpers:Dictionary,Storage,check_missing(),find_namespace_calls(),identical_structure(),input_check_response(),package_logo(),print_data.frame(),print_matrix(),system_information(),unexpected_error(),user_confirm()
Best-possible match of two numeric vectors
Description
This function matches the indices of two numeric vectors as good as possible(that means with the smallest possible sum of deviations).
Usage
match_numerics(x, y)Arguments
x,y | [ |
Value
Aninteger vector of lengthlength(x) with the positions ofy inx.
See Also
Other vector helpers:check_numeric_vector(),check_probability_vector(),chunk_vector(),equidistant_vectors(),insert_vector_entry(),map_indices(),permutations(),split_vector_at(),subsets(),vector_occurrence()
Examples
x <- c(-1, 0, 1)y <- c(0.1, 1.5, -1.2)match_numerics(x, y)Get indices of matrix diagonal
Description
This function returns the indices of the diagonal elements of a quadraticmatrix.
Usage
matrix_diagonal_indices(n, triangular = NULL)Arguments
n | [ |
triangular | [ |
Value
Anintegervector.
See Also
Other matrix helpers:check_correlation_matrix(),check_covariance_matrix(),check_transition_probability_matrix(),cov_to_chol(),diff_cov(),insert_matrix_column(),matrix_indices(),sample_correlation_matrix(),sample_covariance_matrix(),sample_transition_probability_matrix(),stationary_distribution()
Examples
# indices of diagonal elementsn <- 3matrix(1:n^2, n, n)matrix_diagonal_indices(n)# indices of diagonal elements of lower-triangular matrixL <- matrix(0, n, n)L[lower.tri(L, diag=TRUE)] <- 1:((n * (n + 1)) / 2)Lmatrix_diagonal_indices(n, triangular = "lower")# indices of diagonal elements of upper-triangular matrixU <- matrix(0, n, n)U[upper.tri(U, diag=TRUE)] <- 1:((n * (n + 1)) / 2)Umatrix_diagonal_indices(n, triangular = "upper")Getmatrix indices
Description
This function returnsmatrix indices ascharacter.
Usage
matrix_indices(x, prefix = "", exclude_diagonal = FALSE)Arguments
x | [ |
prefix | [ |
exclude_diagonal | [ |
Value
Acharactervector.
See Also
Other matrix helpers:check_correlation_matrix(),check_covariance_matrix(),check_transition_probability_matrix(),cov_to_chol(),diff_cov(),insert_matrix_column(),matrix_diagonal_indices(),sample_correlation_matrix(),sample_covariance_matrix(),sample_transition_probability_matrix(),stationary_distribution()
Examples
M <- diag(3)matrix_indices(M)matrix_indices(M, "M_")matrix_indices(M, "M_", TRUE)Merge named lists
Description
This function mergeslists based on their element names. Elements areonly included in the final outputlist, if no formerlist hascontributed an element with the same name.
Usage
merge_lists(...)Arguments
... | One or more named |
Value
Alist.
See Also
Other list helpers:check_list_of_lists()
Examples
merge_lists(list("a" = 1, "b" = 2), list("b" = 3, "c" = 4, "d" = NULL))Provide information about occurrences
Description
This function provides verbose information about absolute or relativeelement occurrences indata.frame columns.
Usage
occurrence_info(x, relative = FALSE, named = FALSE)Arguments
x | [ |
relative | [ |
named | [ |
Value
Acharacter().
See Also
Other data.frame helpers:delete_columns_data.frame(),group_data.frame(),round_data.frame()
Examples
occurrence_info(datasets::warpbreaks, relative = FALSE, named = TRUE)Creating a basic logo for an R package
Description
This function creates a basic R package logo. The logo has a whitebackground and the package name (with or without curly brackets) in thecenter. The font size for the package name is scaled such that it fits insidethe logo. Type?oeli to see an example.
Usage
package_logo( package_name, brackets = FALSE, background = ggplot2::ggplot() + ggplot2::theme_void(), s_x = 1, s_y = 1, s_width = 1, s_height = 1, white_around_sticker = FALSE)Arguments
package_name | [ |
brackets | [ |
background | A |
s_x,s_y,s_width,s_height,white_around_sticker | Passed on to |
Value
Aggplot object.
References
See Also
Other package helpers:Dictionary,Storage,check_missing(),find_namespace_calls(),identical_structure(),input_check_response(),match_arg(),print_data.frame(),print_matrix(),system_information(),unexpected_error(),user_confirm()
Examples
print(package_logo("my_package", brackets = TRUE))Build permutations
Description
This function creates all permutations of a givenvector.
Usage
permutations(x)Arguments
x | [ |
Value
Alist of all permutations ofx.
References
Modified version ofhttps://stackoverflow.com/a/20199902/15157768.
See Also
Other vector helpers:check_numeric_vector(),check_probability_vector(),chunk_vector(),equidistant_vectors(),insert_vector_entry(),map_indices(),match_numerics(),split_vector_at(),subsets(),vector_occurrence()
Examples
permutations(1:3)permutations(LETTERS[1:3])Print (abbreviated)data.frame
Description
This function prints a (possibly abbreviated)data.frame.
Usage
print_data.frame( x, rows = NULL, cols = NULL, digits = NULL, row.names = TRUE, col.names = TRUE)Arguments
x | [ |
rows,cols | [ Printing is abbreviated in the middle. Can be |
digits | [ Negative values are allowed, resulting in rounding to a power of ten. Can be |
row.names,col.names | [ |
Value
Invisibly returnsx.
See Also
Other package helpers:Dictionary,Storage,check_missing(),find_namespace_calls(),identical_structure(),input_check_response(),match_arg(),package_logo(),print_matrix(),system_information(),unexpected_error(),user_confirm()
Examples
x <- data.frame(1:10, LETTERS[1:10], stats::rnorm(10))print_data.frame(x, rows = 7)print_data.frame(x, rows = 7, cols = 2)print_data.frame(x, rows = 7, cols = 2, digits = 1)print_data.frame(x, rows = 7, cols = 2, digits = 1, row.names = FALSE)print_data.frame(x, rows = 7, cols = 2, digits = 1, col.names = FALSE)Print (abbreviated)matrix
Description
This function prints a (possibly abbreviated)matrix.
Usage
print_matrix( x, rowdots = 4, coldots = 4, digits = 2, label = NULL, simplify = FALSE, details = !simplify)Arguments
x | [ |
rowdots | [ |
coldots | [ |
digits | [ |
label | [ |
simplify | [ |
details | [ |
Value
Invisibly returnsx.
References
This function is a modified version ofpprint() from the{ramify}package.
See Also
Other package helpers:Dictionary,Storage,check_missing(),find_namespace_calls(),identical_structure(),input_check_response(),match_arg(),package_logo(),print_data.frame(),system_information(),unexpected_error(),user_confirm()
Examples
print_matrix(x = 1, label = "single numeric")print_matrix(x = LETTERS[1:26], label = "letters")print_matrix(x = 1:3, coldots = 2)print_matrix(x = matrix(rnorm(99), ncol = 1), label = "single column matrix")print_matrix(x = matrix(1:100, nrow = 1), label = "single row matrix")print_matrix(x = matrix(LETTERS[1:24], ncol = 6), label = "big matrix")print_matrix(x = diag(5), coldots = 2, rowdots = 2, simplify = TRUE)Silence R code
Description
This function silences warnings, messages and anycat() orprint()output from R expressions or functions.
Usage
quiet(x, print_cat = TRUE, message = TRUE, warning = TRUE)Arguments
x | [ |
print_cat | [ |
message | [ |
warning | [ |
Value
Invisibly the expressionx.
References
This function is a modified version ofquiet.
See Also
Other function helpers:do.call_timed(),function_arguments(),function_body(),function_defaults(),timed(),try_silent(),variable_name()
Examples
f <- function() { warning("warning") message("message") cat("cat") print("print")}quiet(f())Roundnumeric columns of adata.frame
Description
This function rounds (only) thenumeric columns of adata.frame.
Usage
round_data.frame(df, digits = 0)Arguments
df | [ |
digits | [ Negative values are allowed, resulting in rounding to a power of ten. Can be |
Value
Adata.frame.
See Also
Other data.frame helpers:delete_columns_data.frame(),group_data.frame(),occurrence_info()
Examples
df <- data.frame("label" = c("A", "B"), "number" = rnorm(10))round_data.frame(df, digits = 1)Sample correlation matrix
Description
This function samples a correlation matrix by sampling a covariance matrixfrom an inverse Wishart distribution and transforming it to a correlationmatrix.
Usage
sample_correlation_matrix(dim, df = dim, scale = diag(dim))Arguments
dim | [ |
df | [ |
scale | [ |
Value
A correlationmatrix.
See Also
Other matrix helpers:check_correlation_matrix(),check_covariance_matrix(),check_transition_probability_matrix(),cov_to_chol(),diff_cov(),insert_matrix_column(),matrix_diagonal_indices(),matrix_indices(),sample_covariance_matrix(),sample_transition_probability_matrix(),stationary_distribution()
Examples
sample_correlation_matrix(dim = 3)Sample covariance matrix
Description
This function samples a covariance matrix from an inverse Wishartdistribution.
Usage
sample_covariance_matrix(dim, df = dim, scale = diag(dim), diag = FALSE)Arguments
dim | [ |
df | [ |
scale | [ |
diag | [ |
Value
A covariancematrix.
See Also
Other matrix helpers:check_correlation_matrix(),check_covariance_matrix(),check_transition_probability_matrix(),cov_to_chol(),diff_cov(),insert_matrix_column(),matrix_diagonal_indices(),matrix_indices(),sample_correlation_matrix(),sample_transition_probability_matrix(),stationary_distribution()
Examples
sample_covariance_matrix(dim = 3)Sample transition probability matrices
Description
This function returns a random, squared matrix of dimensiondimthat fulfills the properties of a transition probability matrix.
Usage
sample_transition_probability_matrix(dim, state_persistent = TRUE)Arguments
dim | [ |
state_persistent | [ |
Value
A transition probabilitymatrix.
See Also
Other matrix helpers:check_correlation_matrix(),check_covariance_matrix(),check_transition_probability_matrix(),cov_to_chol(),diff_cov(),insert_matrix_column(),matrix_diagonal_indices(),matrix_indices(),sample_correlation_matrix(),sample_covariance_matrix(),stationary_distribution()
Examples
sample_transition_probability_matrix(dim = 3)Simulate Markov chain
Description
This function simulates a Markov chain.
Usage
simulate_markov_chain(Gamma, T, delta = oeli::stationary_distribution(Gamma))Arguments
Gamma | [ |
T | [ |
delta | [ The stationary distribution is used by default. |
Value
Anumeric vector of lengthT with states.
See Also
Other simulation helpers:Simulator,correlated_regressors(),ddirichlet_cpp(),dmixnorm_cpp(),dmvnorm_cpp(),dtnorm_cpp(),dwishart_cpp(),gaussian_tv()
Examples
Gamma <- matrix(c(0.8, 0.2, 0.3, 0.7), byrow = TRUE, nrow = 2)delta <- c(0.6, 0.4)simulate_markov_chain(Gamma = Gamma, T = 20, delta = delta)Split a vector at positions
Description
This function splits a vector at specific positions.
Usage
split_vector_at(x, at)Arguments
x | [atomic()'] |
at | [ For example, |
Value
Alist.
References
Based on https://stackoverflow.com/a/19274414.
See Also
Other vector helpers:check_numeric_vector(),check_probability_vector(),chunk_vector(),equidistant_vectors(),insert_vector_entry(),map_indices(),match_numerics(),permutations(),subsets(),vector_occurrence()
Examples
x <- 1:10split_vector_at(x, c(2, 3, 5, 7))Stationary distribution
Description
This function computes the stationary distribution corresponding to atransition probability matrix.
Usage
stationary_distribution(tpm, soft_fail = FALSE)Arguments
tpm | [ |
soft_fail | [ |
Value
Anumeric vector.
See Also
Other matrix helpers:check_correlation_matrix(),check_covariance_matrix(),check_transition_probability_matrix(),cov_to_chol(),diff_cov(),insert_matrix_column(),matrix_diagonal_indices(),matrix_indices(),sample_correlation_matrix(),sample_covariance_matrix(),sample_transition_probability_matrix()
Examples
tpm <- matrix(0.05, nrow = 3, ncol = 3)diag(tpm) <- 0.9stationary_distribution(tpm)Generate vector subsets
Description
This function generates subsets of a vector.
Usage
subsets(v, n = seq_along(v))Arguments
v | [atomic()'] |
n | [integer(1)'] |
Value
Alist, each element is a subset ofv.
See Also
Other vector helpers:check_numeric_vector(),check_probability_vector(),chunk_vector(),equidistant_vectors(),insert_vector_entry(),map_indices(),match_numerics(),permutations(),split_vector_at(),vector_occurrence()
Examples
v <- 1:3subsets(v)subsets(v, c(1, 3)) # only subsets of length 1 or 3subsets(integer()) # trivial case worksGeneral system level information
Description
This function returns alist of general system level information.
Usage
system_information()Value
Alist with elements:
maschine, the model name of the devicecores, the number of coresram, the size of the RAMos, the operating systemrversion, the R version used
See Also
Other package helpers:Dictionary,Storage,check_missing(),find_namespace_calls(),identical_structure(),input_check_response(),match_arg(),package_logo(),print_data.frame(),print_matrix(),unexpected_error(),user_confirm()
Examples
system_information()Interrupt long evaluations
Description
This function interrupts an evaluation after a certain number of seconds.Note the limitations documented insetTimeLimit.
Usage
timed(expression, seconds = Inf, on_time_out = "silent")Arguments
expression | [ |
seconds | [ |
on_time_out | [
|
Value
The value ofexpression or, if the evaluation time exceeded, whateveris specified foron_time_out.
See Also
Other function helpers:do.call_timed(),function_arguments(),function_body(),function_defaults(),quiet(),try_silent(),variable_name()
Examples
foo <- function(x) { for (i in 1:10) Sys.sleep(x / 10) return(x)}timed(foo(0.5), 1)timed(foo(1.5), 1)Try an expression silently
Description
This function tries to executeexpr and returns a string with theerror message if the execution failed.
Usage
try_silent(expr)Arguments
expr | [ |
Details
This function is a wrapper fortry.
Value
Either the value ofexpr or in case of a failure an object of classfail, which contains the error message.
See Also
Other function helpers:do.call_timed(),function_arguments(),function_body(),function_defaults(),quiet(),timed(),variable_name()
Examples
## Not run: try_silent(1 + 1)try_silent(1 + "1")## End(Not run)Handling of an unexpected error
Description
This function reacts to an unexpected error by throwing an error and linkingto an issue site with the request to submit an issue.
Usage
unexpected_error( msg = "Ups, an unexpected error occured.", issue_link = "https://github.com/loelschlaeger/oeli/issues")Arguments
msg | [ |
issue_link | [ |
Value
No return value, but it throws an error.
See Also
Other package helpers:Dictionary,Storage,check_missing(),find_namespace_calls(),identical_structure(),input_check_response(),match_arg(),package_logo(),print_data.frame(),print_matrix(),system_information(),user_confirm()
User confirmation
Description
This function asks in an interactive question a binary question.
Usage
user_confirm(question = "Question?", default = FALSE)Arguments
question | [ |
default | [ |
Value
EitherTRUE orFALSE.
See Also
Other package helpers:Dictionary,Storage,check_missing(),find_namespace_calls(),identical_structure(),input_check_response(),match_arg(),package_logo(),print_data.frame(),print_matrix(),system_information(),unexpected_error()
Determine variable name
Description
This function tries to determine the name of a variable passed to afunction.
Usage
variable_name(variable, fallback = "unnamed")Arguments
variable | [ |
fallback | [ |
Value
Acharacter, the variable name.
See Also
Other function helpers:do.call_timed(),function_arguments(),function_body(),function_defaults(),quiet(),timed(),try_silent()
Examples
variable_name(a)f <- function(x) variable_name(x)f(x = a)Find the positions of first or last occurrence of unique vector elements
Description
This function finds the positions of first or last occurrence of uniquevector elements.
Usage
vector_occurrence(x, type = "first")Arguments
x | [ |
type | [ |
Value
Anintegervector, the positions of the unique vector elements.The ordering corresponds tounique(x), i.e., thei-th element inthe output is the (first or last) occurrence of thei-th element fromunique(x).
See Also
Other vector helpers:check_numeric_vector(),check_probability_vector(),chunk_vector(),equidistant_vectors(),insert_vector_entry(),map_indices(),match_numerics(),permutations(),split_vector_at(),subsets()
Examples
x <- c(1, 1, 1, 2, 2, 2, 3, 3, 3)unique(x)vector_occurrence(x, "first")vector_occurrence(x, "last")