Write data to a JSON file readable by CmdStan
write_stan_json(data, file, always_decimal = FALSE)
data | (list) A named list of R objects. |
---|---|
file | (string) The path to where the data file should be written. |
always_decimal | (logical) Force generate non-integers with decimal
points to better distinguish between integers and floating point values.
If |
write_stan_json()
performs several conversions before writing the JSON
file:
logical
-> integer
(TRUE
-> 1
, FALSE
-> 0
)
data.frame
-> matrix
(via data.matrix()
)
list
-> array
table
-> vector
, matrix
, or array
(depending on dimensions of table)
The list
to array
conversion is intended to make it easier to prepare
the data for certain Stan declarations involving arrays:
vector[J] v[K]
(or equivalently array[K] vector[J] v
as of Stan 2.27)
can be constructed in R as a list with K
elements where each element a
vector of length J
matrix[I,J] v[K]
(or equivalently array[K] matrix[I,J] m
as of Stan
2.27 ) can be constructed in R as a list with K
elements where each element
an IxJ
matrix
These can also be passed in from R as arrays instead of lists but the list
option is provided for convenience. Unfortunately for arrays with more than
one dimension, e.g., vector[J] v[K,L]
(or equivalently
array[K,L] vector[J] v
as of Stan 2.27) it is not possible to use an R
list and an array must be used instead. For this example the array in R
should have dimensions KxLxJ
.
x <- matrix(rnorm(10), 5, 2) y <- rpois(nrow(x), lambda = 10) z <- c(TRUE, FALSE) data <- list(N = nrow(x), K = ncol(x), x = x, y = y, z = z) # write data to json file file <- tempfile(fileext = ".json") write_stan_json(data, file) # check the contents of the file cat(readLines(file), sep = "\n")#> { #> "N": 5, #> "K": 2, #> "x": [ #> [0.552235993548325, 0.721685253131368], #> [1.40374671287315, -0.278162254251497], #> [0.512051509623765, -0.524699374531002], #> [0.660479109910809, 0.0625388737657775], #> [-0.519175154088263, -0.450598871694098] #> ], #> "y": [16, 11, 7, 7, 6], #> "z": [1, 0] #> }# demonstrating list to array conversion # suppose x is declared as `vector[3] x[2]` (or equivalently `array[2] vector[3] x`) # we can use a list of length 2 where each element is a vector of length 3 data <- list(x = list(1:3, 4:6)) file <- tempfile(fileext = ".json") write_stan_json(data, file) cat(readLines(file), sep = "\n")#> { #> "x": [ #> [1, 2, 3], #> [4, 5, 6] #> ] #> }