⚠️There's a newer version (0.3.5) of this package. Take me there.

term

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.

Installation

To install the latest release from CRAN

install.packages("term")

To install the developmental version from GitHub

# install.packages("remotes")
remotes::install_github("poissonconsulting/term")

To install the latest developmental release from the Poisson drat repository

# install.packages("drat")
drat::addRepo("poissonconsulting")
install.packages("term")

Demonstration

Creating Term Vectors

library(term)

# character vectors can be converted into term vectors
term <- as.term(c(
  "alpha[1]", "alpha[2]", "beta[1,1]", "beta[2,1]",
  "beta[1,2]", "beta[2,2]", "sigma"
))

# term vectors print like character vectors
term
#> [1] "alpha[1]"  "alpha[2]"  "beta[1,1]" "beta[2,1]" "beta[1,2]" "beta[2,2]"
#> [7] "sigma"

# they are S3 class objects that also inherit from character
str(term)
#>  'term' chr [1:7] "alpha[1]" "alpha[2]" "beta[1,1]" "beta[2,1]" "beta[1,2]" ...

# term vectors can also be created from numeric atomic objects
as.term(matrix(1:4, 2), "theta")
#> [1] "theta[1,1]" "theta[2,1]" "theta[1,2]" "theta[2,2]"

Querying Term Vectors

# get the parameter names
pars(term)
#> [1] "alpha" "beta"  "sigma"
# and parameter dimensions
pdims(term)
#> $alpha
#> [1] 2
#> 
#> $beta
#> [1] 2 2
#> 
#> $sigma
#> [1] 1

# get the parameter names by term
pars(term, terms = TRUE)
#> [1] "alpha" "alpha" "beta"  "beta"  "beta"  "beta"  "sigma"
# and the term indices
tindex(term)
#> $`alpha[1]`
#> [1] 1
#> 
#> $`alpha[2]`
#> [1] 2
#> 
#> $`beta[1,1]`
#> [1] 1 1
#> 
#> $`beta[2,1]`
#> [1] 2 1
#> 
#> $`beta[1,2]`
#> [1] 1 2
#> 
#> $`beta[2,2]`
#> [1] 2 2
#> 
#> $sigma
#> [1] 1

Validating Term Vectors

# term vectors can be tested for whether they have (parseably) valid,
# (dimensionally) consistent and complete terms.

# valid terms
valid_term(as.term(c("a", "a[1]", "a [2]", " b [3  ] ", "c[1,10]")))
#> [1] TRUE TRUE TRUE TRUE TRUE

# invalid terms
valid_term(as.term(c("a a", "a[]", "a[2", " b[3 3]", "c[1,10]c")))
#> [1] FALSE FALSE FALSE FALSE FALSE

# consistent terms
consistent_term(as.term(c("a", "a[2]", "b[1,1]", "b[10,99]")))
#> [1] TRUE TRUE TRUE TRUE

# inconsistent terms
consistent_term(as.term(c("a", "a[2,1]", "b[1,1]", "b[10,99,1]")))
#> [1] FALSE FALSE FALSE FALSE

# complete terms
is.incomplete_terms(as.term(c("a", "a[2]", "b[1,1]", "b[2,1]")))
#> [1] FALSE

# incomplete terms
is.incomplete_terms(as.term(c("a", "a[3]", "b[1,1]", "b[2,2]")))
#> [1] TRUE

Checking Term Vectors

x <- as.term(c("a[1]", "a[3]"))
chk_term(x, validate = "valid")
chk_term(x, validate = "complete")
#> Error: All elements of term vector `x` must be complete.

Repairing Term Vectors

term <- as.term(c("b[4]", "b   [2]", "b", "b[1", "b[2, 2]", "b", "a [ 1 ] ", NA))
term
#> [1] "b[4]"     "b   [2]"  "b"        "b[1"      "b[2, 2]"  "b"        "a [ 1 ] "
#> [8] NA

# valid terms can be repaired (invalid terms are converted to missing values)
term <- repair_terms(term)
term
#> [1] "b[4]"   "b[2]"   "b[1]"   NA       "b[2,2]" "b[1]"   "a"      NA

# missing values can easily removed
term <- term[!is.na(term)]
term
#> [1] "b[4]"   "b[2]"   "b[1]"   "b[2,2]" "b[1]"   "a"

# and only unique values retained
term <- unique(term)
term
#> [1] "b[4]"   "b[2]"   "b[1]"   "b[2,2]" "a"

# a term vector can be sorted by parameter name and index
term <- sort(term)
term
#> [1] "a"      "b[1]"   "b[2]"   "b[4]"   "b[2,2]"

# an inconsistent term removed
term <- term[term != "b[2,2]"]
term
#> [1] "a"    "b[1]" "b[2]" "b[4]"

# and incomplete terms completed
term <- sort(complete_terms(term))
term
#> [1] "a"    "b[1]" "b[2]" "b[3]" "b[4]"

Contribution

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.

Copy Link

Version

Down Chevron

Install

install.packages('term')

Monthly Downloads

358

Version

0.1.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Last Published

January 15th, 2020

Functions in term (0.1.0)