Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://poissonconsulting.github.io/term/

An R package to manipulate the names of parameters terms
https://poissonconsulting.github.io/term/

coefficients cran mcmc r rstats term

Last synced: 15 days ago
JSON representation

An R package to manipulate the names of parameters terms

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# term

[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable)
[![R-CMD-check](https://github.com/poissonconsulting/term/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/poissonconsulting/term/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/poissonconsulting/term/graph/badge.svg)](https://app.codecov.io/gh/poissonconsulting/term)
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)
[![CRAN status](https://www.r-pkg.org/badges/version/term)](https://cran.r-project.org/package=term)
![CRAN downloads](http://cranlogs.r-pkg.org/badges/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](https://cran.r-project.org)
```{r, eval=FALSE}
install.packages("term")
```

To install the developmental version from [GitHub](https://github.com/poissonconsulting/term)
```{r, eval=FALSE}
# install.packages("remotes")
remotes::install_github("poissonconsulting/term")
```

## Demonstration

### Term Vectors
```{r}
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)
```

```{r}
# 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
```

### Querying Term Vectors
```{r}
# 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)
```

### Validating Term Vectors
```{r}
# 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]"))
```

### Checking Term Vectors

```{r, error=TRUE}
# 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")
```

### Repairing Term Vectors

```{r error = TRUE}
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
```

## Contribution

Please report any [issues](https://github.com/poissonconsulting/term/issues).

[Pull requests](https://github.com/poissonconsulting/term/pulls) are always welcome.

## Code of Conduct

Please note that the term project is released with a [Contributor Code of Conduct](https://contributor-covenant.org/version/2/0/CODE_OF_CONDUCT.html).
By contributing to this project, you agree to abide by its terms.