-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME.Rmd
188 lines (140 loc) · 4.96 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# term <img src="man/figures/logo.png" align="right" />
<!-- badges: start -->
[![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/license/mit)
[![CRAN status](https://www.r-pkg.org/badges/version/term)](https://cran.r-project.org/package=term)
![CRAN downloads](https://cranlogs.r-pkg.org/badges/term)
<!-- badges: end -->
`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.
## 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
```
## Installation
### Release
To install the release version from [CRAN](https://CRAN.R-project.org/package=term).
```r
install.packages("term")
```
The website for the release version is at <https://poissonconsulting.github.io/term/>.
### Development
To install the development version from [r-universe](https://poissonconsulting.r-universe.dev/term).
```r
install.packages("term", repos = c("https://poissonconsulting.r-universe.dev", "https://cloud.r-project.org"))
```
or from [GitHub](https://github.com/poissonconsulting/term)
```r
# install.packages("remotes")
remotes::install_github("poissonconsulting/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.