poissonconsulting / term

An R package to manipulate the names of parameters terms
https://poissonconsulting.github.io/term/
Other
10 stars 1 forks source link

Add parameter class and dims class extending vctr? #42

Closed joethorley closed 4 years ago

joethorley commented 4 years ago

Then put together to produce term?

krlmlr commented 4 years ago

Some ideas:

library(vctrs)

term::term("par", foo = 2)
#> <term[3]>
#> [1] par    foo[1] foo[2]

term::as_term(c("par", "b[2]"))
#> <term[2]>
#> [1] par  b[2]

term <- new_rcrd(list(
  names = c("par", "foo", "foo"),
  dimensions = list(integer(), 1L, 2L)
))

field(term, "names")
#> [1] "par" "foo" "foo"
field(term, "dimensions")
#> [[1]]
#> integer(0)
#> 
#> [[2]]
#> [1] 1
#> 
#> [[3]]
#> [1] 2

term <- new_rcrd(list(
  names = c("par", "foo"),
  dimensions = list(integer(), 2L)
))

field(term, "names")
#> [1] "par" "foo"
field(term, "dimensions")
#> [[1]]
#> integer(0)
#> 
#> [[2]]
#> [1] 2

term <- list(
  list(names = "par", dimensions = structure(1L, max = 1L)),
  list(names = "foo", dimensions = structure(1L, max = 3L)),
  list(names = "foo", dimensions = structure(2L, max = 3L))
)

term
#> [[1]]
#> [[1]]$names
#> [1] "par"
#> 
#> [[1]]$dimensions
#> [1] 1
#> attr(,"max")
#> [1] 1
#> 
#> 
#> [[2]]
#> [[2]]$names
#> [1] "foo"
#> 
#> [[2]]$dimensions
#> [1] 1
#> attr(,"max")
#> [1] 3
#> 
#> 
#> [[3]]
#> [[3]]$names
#> [1] "foo"
#> 
#> [[3]]$dimensions
#> [1] 2
#> attr(,"max")
#> [1] 3

Created on 2020-07-10 by the reprex package (v0.3.0)

joethorley commented 4 years ago
# A term vector is a vector of labels each of which refers to a particular element of a numeric object.

# term vectors can be readily created from a numeric object
library(term)
as_term(matrix(c(1,5,6,10), nrow = 2))

# or from multiple numeric objects
library(nlist)
nlist <- nlist(x = c(3,6,9), zz = matrix(c(1, 5, 6, 11), nrow = 2))
nlist
as_term(nlist)

# by themselves such term vectors are not very interesting
# however since they are use in a range of structures to identify elements they

mcmc <- as_mcmc(nlist)
mcmc

# are interesting because they can be used to start to manipulate the elements
#
as_nlist(mcmc)

# its important to note that elements can be missing, duplicated, inconsistent

mcmc <- mcmc[,-c(2,7),drop = FALSE]

mcmc

as_nlist(mcmc)

# the term packages is designed to manipulate term vectors.
#
# currently it is based on a character vector as a vctrs_vctr which has to be parsed into its component parts for each term ie the name of the parameter and the index.
#
# the question is whether its worth defining each element in a term vector as a composite of scalars of other class (possibly vctr_vctrs) one class is the parameter
# name and the other is the index which must be a 1 or more positive integers?
krlmlr commented 4 years ago

Implement one more class, and coercion methods between "term" and the new class "term_rcrd". Reconsider use of rcrd because we want to do computations on the data -- validity, consistency, completeness. Explicit conversion to rcrd would simplify both the checks and other computation.