[R-sig-hpc] function to combine results from foreach
Kasper Daniel Hansen
kasperdanielhansen at gmail.com
Thu Jan 20 19:58:20 CET 2011
With m defined as In Jeremy's answer, just do
Reduce("+", m)
This all happens because + is a function of two objects, not an
arbitrary number of objects.
Kasper
On Thu, Jan 20, 2011 at 1:51 PM, Jeremy Hetzel <jthetzel at gmail.com> wrote:
> Not a particularly clever or efficient function, but perhaps the following
> helps for now:
>
> # Example matrices
> m1 <- matrix(c(0,1,2,2,0,1,1,2,0), nrow=3)
> m2 <- matrix(c(0,2,4,4,0,2,2,4,0), nrow=3)
> m3 <- matrix(c(0,1,2,2,0,1,-1,-2,0), nrow=3)
>
> # Make list
> m <- list(m1, m2, m3)
>
> # Function to find mean from list
> meanList <- function(x)
> {
> result <- x[[1]]
> for (i in x[-1])
> {
> result <- result + i
> }
>
> result <- result / length(x)
>
> return(result)
> }
>
> # Find mean from list
> result1 <- meanList(m)
>
> # Find mean from matrices
> result2 <- (m1+m2+m3)/3
>
> # Same results
> all.equal(result1, result2)
>
>
>
>
> Jeremy
>
> Jeremy T. Hetzel
> Boston University
>
>
>
>
>
> On Thu, Jan 20, 2011 at 7:17 AM, Maas James Dr (MED) <J.Maas at uea.ac.uk>wrote:
>
>> I've got foreach to produce some nice results for me, using doMPI but
>> having great difficulty extracting the results. Would anyone give me some
>> clues about how to write an appropriate function that would combine results
>> from iterative execution of a MCMC (jags) routine?
>>
>> In this toy example the foreach iterates through jags 3 times. However jags
>> itself iterates 5,000 times but is thinned to 1,000 results. Therefore to
>> get the mean of the 1000 posterior means from jags I use
>>
>> logOR <- data.frame(apply(results$logOR, c(1,2), mean))
>>
>> therefore the results from the 3 iterations of jags are:
>>
>> > results
>> [[1]]
>> X1 X2 X3
>> 1 0.0000000 -0.2929320 -0.5835677
>> 2 0.2929320 0.0000000 -0.2906358
>> 3 0.5835677 0.2906358 0.0000000
>>
>> [[2]]
>> X1 X2 X3
>> 1 0.0000000 -0.2269787 -0.6619444
>> 2 0.2269787 0.0000000 -0.4349657
>> 3 0.6619444 0.4349657 0.0000000
>>
>> [[3]]
>> X1 X2 X3
>> 1 0.0000000 -0.2465974 -0.5616317
>> 2 0.2465974 0.0000000 -0.3150343
>> 3 0.5616317 0.3150343 0.0000000
>>
>> If I can, what I want to do is combine these 3 matrices, and getting the
>> mean matrix. I've tried using .combine="+" bu t get an error message.
>>
>> Error in fun(result.1, result.2) :
>> non-numeric argument to binary operator
>>
>> I'd like to get back
>> >results
>> [[1]]
>> X1 X2 X3
>> 1 0.00 -0.25 -0.60
>> 2 0.25 0.00 -0.34
>> 3 0.60 0.34 0.00
>>
>>
>> Any suggestions most welcome. Thanks
>>
>> J
>> ===============================
>> Dr. Jim Maas
>> University of East Anglia
>>
>> _______________________________________________
>> R-sig-hpc mailing list
>> R-sig-hpc at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-hpc
>>
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-hpc mailing list
> R-sig-hpc at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-hpc
>
More information about the R-sig-hpc
mailing list