rstan help - convering list of list of numeric to array of arrays of vector

304 views
Skip to first unread message

andy...@cubist-asia.com

unread,
Oct 29, 2015, 12:21:10 AM10/29/15
to Stan users mailing list
Hi guys,

In my stan code, i have the following:

int I;
int J;
int K;
vector[K] X[I, J]

and in R, i have used list (length=I) and each item of this list is a of list (length of J) of numeric (length K) to represent this:

x_r := <list of length I, each is x_r_1>
x_r_1 := <list of length J, each is x_leaf>
x_leaf := <numeric, length K>

When I pass this structure to stan, rstan would complain:
Error in data_list2array(x) : all elements of the list should be numeric

My questions are:
- generally, it seems that list of numeric can be converted into array of vector. Am I doing something with my list of list construction or list of list of vector cannot be automatically converted into 3D array of real?
- If auto conversion is not supported, is there any generic function that can help convert list of (list of ....) numeric from R into the corresponding N-array real/int in stan?

Many thanks,

Andy







Tamas Papp

unread,
Oct 29, 2015, 2:57:39 AM10/29/15
to stan-...@googlegroups.com
Hi Andy,

I don't know of a generic function, but it is easy to write one
(horribly non generic):

lll2array <- function(lll) {
l <- length(lll)
m <- length(lll[[1]])
n <- length(lll[[1]][[1]])
a <- array(0,c(l,m,n))
for (i in 1:l) {
l2 <- lll[[i]]
stopifnot(length(l2) == m)
for (j in 1:m) {
l3 <- l2[[j]]
stopifnot(length(l3) == n)
a[i,j,] <- as.vector(l3,"numeric")
}
}
a
}

## test it
test_lll <- local({
test1 <- list(1,2)
test2 <- list(test1,test1,test1)
list(test2,test2,test2,test2)
})
test_lll # nasty
lll2array(test_lll) # nice

That said, possibly building up your data as an array in the first place
could be a better choice.

Best,

Tamas

andy...@cubist-asia.com

unread,
Oct 29, 2015, 3:42:42 AM10/29/15
to Stan users mailing list
Hi Tamas,

Many thanks.

I was using list of list to build my nested data as I felt it's easier for debugging as I can attached names to list and I can check the data structure was indeed correctly assembled from a number of data sources.

Best,
Andy

Tamas Papp

unread,
Oct 29, 2015, 4:08:10 AM10/29/15
to stan-...@googlegroups.com
Hi Andy,

I understand, I frequently do this too --- but if names are the same
across indexes, you can also use dimnames for the array.

I recently estimated a model where column order was exchanged at some
point but I forgot to update all uses. After spending a day figuring out
what happened, in a momentary bout of frustration I recoded the Stan
model as

data {
int index_somevariablename;
...
}
model {
for (j in something)
someothervar[j] ~ thedata[j,index_somevariablename];
...
}

and provided these indexes as

match("somevariablename", colnames(mydata))

Pretty verbose, but pretty robust too. This was just an experiment, I am
not sure I will do this ever again --- seemed like a good idea at the
moment.
Reply all
Reply to author
Forward
0 new messages