[R] Building a list
Noah Silverman
noah at smartmediacorp.com
Mon May 31 02:31:39 CEST 2010
Nice.
Thanks!
-N
On 5/30/10 3:16 PM, jim holtman wrote:
> Let initialize Chain:
>
> Chain <- vector('list', 5)
> groups <- 1:5
> for(j in 1:10000){
>
> for(g in groups){
>
> coef <- c(1,2,3,4,5,6,7,8,9,10) #would be actual MCMC samples
>
> Chain[[g]] <- rbind(Chain[[g]], coef)
> }
> }
>
>
> On Sun, May 30, 2010 at 6:05 PM, Noah Silverman <noah at smartmediacorp.com> wrote:
>
>> That would be great, except I just realized I made a typo when sending
>> my code.
>>
>> I'm tracking 20 coefficents for 10 groups. So I need a "top" list of 10
>> groups. Then each of the 10,000 samples for each of the 20 coefficients.
>>
>> It should be more like this:
>>
>> for(j in 1:10000){
>> for(g in groups){
>> coef <- c(1,2,3,4,5,6,7,8,9,10) #would be actual MCMC samples
>> Chain[[g]] <- rbind(Chain[[g]], coef)
>> }
>> }
>>
>> So, there are 10 lists in "Chain" (One for each group.) Each list is
>> then a matrix/data.frame of values for each coef. (Hence the "rbind" in
>> my code.)
>>
>> R gives me an error about the subscript, as Chain[[g]] is empty for the
>> first iteration.
>>
>>
>>
>>
>> On 5/30/10 3:00 PM, Joshua Wiley wrote:
>>
>>> Hello Noah,
>>>
>>> Does this work for you?
>>>
>>> Chain <- vector("list", 10000)
>>> for (j in 1:10000){
>>> coef <- c(1,2,3,4,5,6,7,8,9,10) #would be actual MCMC samples
>>> Chain[[j]] <- rbind(Chain[[j]], coef)
>>>
>>> If it does, this has the additional advantage that it tends to be
>>> faster to initialize the list at size rather than expanding it as
>>> needed.
>>>
>>> HTH,
>>>
>>> Josh
>>>
>>> On Sun, May 30, 2010 at 2:52 PM, Noah Silverman <noah at smartmediacorp.com> wrote:
>>>
>>>
>>>> Hello,
>>>>
>>>> I need to build a "list of lists"
>>>>
>>>> We have 20 groups we are generating MCMC samples for. There are 10
>>>> coefficients, and 10000 MCMC iterations.
>>>>
>>>> I would like to store each iteration by-group in a list. My problem is
>>>> with the first iteration.
>>>>
>>>> Here is a toy example:
>>>>
>>>> Chain <- list()
>>>> for (j in 1:10000){
>>>> coef <- c(1,2,3,4,5,6,7,8,9,10) #would be actual MCMC samples
>>>> Chain[[j]] <- rbind(Chain[[j]], coef)
>>>> }
>>>>
>>>> This returns an error, UNLESS I initialize the first row of Chain[[j]]
>>>> with something.
>>>>
>>>> The idea is that for any group, I can quickly extract, plot, average,
>>>> etc the values for each coefficient.
>>>>
>>>> for example:
>>>>
>>>> Chain[[5]][,3] will give me all 10,000 values of coefficient 3 for group
>>>> 5.
>>>>
>>>> Again, this seems to work, but I can't initialize the chain with a
>>>> random value as it will cause problems with the data summary later.
>>>> (Each row in Chain[[j]] will be out of sync by 1, subsequently all
>>>> summary and plotting work will have to account for this - it can get
>>>> messy in a large program.)
>>>>
>>>> Is there an easier way to do this? Am I missing something?
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>>>>
>>>>
>>>
>>>
>>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>
>
>
More information about the R-help
mailing list