knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
term
is an R package to create, manipulate, query and repair vectors of parameter terms.
Parameter terms are the labels used to reference values in vectors, matrices and arrays.
They represent the names in coefficient tables and the column names in
mcmc
and mcmc.list
objects.
library(term) # term vectors are the labels used to reference values in # vectors, matrices and arrays term <- term( "alpha[1]", "alpha[2]", "beta[1,1]", "beta[2,1]", "beta[1,2]", "beta[2,2]", "sigma" ) # they print like character vectors term # but are S3 class objects that also inherit from vctrs_vctr class(term)
# consider a matrix of term values set.seed(101) estimate <- matrix(rnorm(4), nrow = 2) estimate # the term labels can be created using as_term() term <- as_term(estimate, name = "b0") term # and combined with the term values to create a coef table library(tibble) coef <- tibble(term = term, estimate = as.vector(estimate)) coef
# the term vectors are readily sorted coef[rev(order(coef$term)), ] # and the parameter names or parameter dimensions extracted term <- term( "alpha[1]", "alpha[2]", "beta[1,1]", "beta[2,1]", "beta[1,2]", "beta[2,2]", "sigma" ) pars(term) pdims(term) # this can also be done for each term pars_terms(term) tindex(term)
# term vectors can be tested for whether they have (parseably) valid, # (dimensionally) consistent and complete terms. # valid terms valid_term(term("a", "a[1]", "a [2]", " b [3 ] ", "c[1,10]")) # invalid terms valid_term(new_term(c("a a", "a[]", "a[2", " b[3 3]", "c[1,10]c"))) # consistent terms consistent_term(term("a", "a[2]", "b[1,1]", "b[10,99]")) # inconsistent terms consistent_term(term("a", "a[2,1]", "b[1,1]", "b[10,99,1]")) # complete terms is_incomplete_terms(term("a", "a[2]", "b[1,1]", "b[2,1]")) # incomplete terms is_incomplete_terms(term("a", "a[3]", "b[1,1]", "b[2,2]"))
# term vectors can be checked using functions styled on those in the chk package x <- term("a[1]", "a[3]") vld_term(x, validate = "valid") chk_term(x, validate = "complete")
term <- new_term(c("b[4]", "b [2]", "b", "b[1", "b[2, 2]", "b", "a [ 1 ] ", NA)) term # valid terms can be repaired (invalid terms are converted to missing values) term <- repair_terms(term) term # the `term()` constructor rejects invalid terms term("b[4]", "b [2]", "b", "b[1", "b[2, 2]", "b", "a [ 1 ] ", NA) # missing values can easily removed term <- term[!is.na(term)] term # and only unique values retained term <- unique(term) term # a term vector can be sorted by parameter name and index term <- sort(term) term # an inconsistent term removed term <- term[term != "b[2,2]"] term # and incomplete terms completed term <- sort(complete_terms(term)) term
To install the release version from CRAN.
install.packages("term")
The website for the release version is at https://poissonconsulting.github.io/term/.
To install the development version from r-universe.
install.packages("term", repos = c("https://poissonconsulting.r-universe.dev", "https://cloud.r-project.org"))
or from GitHub
# install.packages("remotes") remotes::install_github("poissonconsulting/term")
Please report any issues.
Pull requests are always welcome.
Please note that the term project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.