Learn R Programming

nlist

nlist is an R package to create and manipulate numeric list (nlist) objects.

An nlist is an S3 class list of uniquely named numeric atomic (natomic) objects. An natomic object is an integer or double vector, matrix or array. nlist objects are the raw data inputs for analytic engines such as JAGS, STAN and TMB.

An nlists object is a S3 class list of nlist objects with the same names, dimensionalities and typeofs. nlists objects are useful for storing multiple realizations of simulated data sets. They can be converted to coda::mcmc and coda::mcmc.list objects.

Installation

To install the latest release from CRAN

install.packages("nlist")

To install the developmental version from GitHub

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

Demonstration

natomic

An natomic object is an integer or double vector, matrix or array.

library(nlist)
is.natomic(TRUE)
#> [1] FALSE
is.natomic(1L)
#> [1] TRUE
is.natomic(matrix(1:3))
#> [1] TRUE

nlist

An nlist is an S3 class list of uniquely named numeric atomic (natomic) objects.

It is straightforward to create an new nlist object.

library(nlist)

nlist <- nlist(x = 1, y = matrix(1:9, 3))
nlist
#> $x
#> [1] 1
#> 
#> $y
#>      [,1] [,2] [,3]
#> [1,]    1    4    7
#> [2,]    2    5    8
#> [3,]    3    6    9
#> 
#> an nlist object with 2 natomic elements

Or to coerce from a data frame.

data <- data.frame(
  lgl = c(TRUE, NA),
  dte = as.Date(c("2001-01-02", "2001-01-01")),
  fac = factor(c("b", "a"))
)

data
#>    lgl        dte fac
#> 1 TRUE 2001-01-02   b
#> 2   NA 2001-01-01   a
as.nlist(data)
#> $lgl
#> [1]  1 NA
#> 
#> $dte
#> [1] 11324 11323
#> 
#> $fac
#> [1] 2 1
#> 
#> an nlist object with 3 natomic elements

nlists

An nlists object is a S3 class list of nlist objects with the same names, dimensionalities and typeofs. The nchains attribute is used to keep track of the number of chains.

nlists <- nlists(
  nlist(x = 1, y = matrix(1:9, 3)),
  nlist(x = -2, y = matrix(2:10, 3)),
  nlist(x = 10, y = matrix(22:30, 3)),
  nlist(x = -100, y = matrix(-2:-10, 3))
)

print(nlists)
#> $x
#> [1] -22.75
#> 
#> $y
#>      [,1] [,2] [,3]
#> [1,] 5.75 7.25 8.75
#> [2,] 6.25 7.75 9.25
#> [3,] 6.75 8.25 9.75
#> 
#> an nlists object of 4 nlist objects each with 2 natomic elements

Aggregating an nlists object gives an nlist object.

aggregate(nlists, fun = median)
#> $x
#> [1] -0.5
#> 
#> $y
#>      [,1] [,2] [,3]
#> [1,]  1.5  4.5  7.5
#> [2,]  2.5  5.5  8.5
#> [3,]  3.5  6.5  9.5
#> 
#> an nlist object with 2 natomic elements

An nlists object can have its chains split or collapsed.

nlists <- split_chains(nlists)

And can be converted to an mcmc.list object

coda::as.mcmc.list(nlists)
#> $`1`
#> Markov Chain Monte Carlo (MCMC) output:
#> Start = 1 
#> End = 2 
#> Thinning interval = 1 
#>       x y[1,1] y[2,1] y[3,1] y[1,2] y[2,2] y[3,2] y[1,3] y[2,3] y[3,3]
#> [1,]  1      1      2      3      4      5      6      7      8      9
#> [2,] -2      2      3      4      5      6      7      8      9     10
#> 
#> $`2`
#> Markov Chain Monte Carlo (MCMC) output:
#> Start = 1 
#> End = 2 
#> Thinning interval = 1 
#>         x y[1,1] y[2,1] y[3,1] y[1,2] y[2,2] y[3,2] y[1,3] y[2,3] y[3,3]
#> [1,]   10     22     23     24     25     26     27     28     29     30
#> [2,] -100     -2     -3     -4     -5     -6     -7     -8     -9    -10
#> 
#> attr(,"class")
#> [1] "mcmc.list"

Contribution

Please report any issues.

Pull requests are always welcome.

Code of Conduct

Please note that the nlist project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

Copy Link

Version

Install

install.packages('nlist')

Monthly Downloads

853

Version

0.1.1

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Last Published

June 18th, 2020

Functions in nlist (0.1.1)

as.nlist

Coerce to nlist
as.mcmc.list.nlist

As mcmc.list Object
aggregate.nlist

Aggregate nlist
as.nlists

Coerce to nlists
aggregate.nlists

Aggregate nlists
as.mcmc.nlist

As mcmc Object
as.mcmc.nlists

As mcmc Object
as.mcmc.list.nlists

As mcmc Object
nlists-object

nlists Object
nlist

Create nlist Object
natomic-object

natomic Object
nlist-object

nlist Object
niters

Number of MCMC Iterations
numericise

Numericise (or Numericize)
nterms.nlists

Number of Terms
fill_na

Fill Missing Values
is_natomic

Is natomic, nlist or nlists
nsims

Number of MCMC Simulations
nterms.nlist

Number of Terms
nsams

Number of MCMC Samples
nchains

Number of MCMC chains
npdims.nlists

Number of Parameter Dimensions
pdims.nlist

Parameter Dimensions of an nlist Object
pdims.nlists

Dimensions of an nlists Object
chk_natomic

Check Numeric Atomic, nlist Object or nlists Object
vld_natomic

Validate Numeric Atomic, nlist Object or nlists Object
collapse_chains

Collapse Chains
nlists

Create nlists Object
pars.nlist

Parameter Names
npdims.nlist

Number of Parameter Dimensions
relist_nlist

Relists an unlist nlist Object
split_chains

Split Chains
pars.nlists

Parameter Names
subset.nlist

Subset nlist Object
subset.nlists

Subset nlists Object
unlist_nlist

Flatten nlist Object
unlist.nlist

Flatten nlist Object