Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You want to get your data into a data frame instead of a matrix, and then use the formula interface to <code>aggregate</code>.</p> <p>Ideally you want to get your MCMC output in a form that you can read directly into a data frame, but if you are stuck with the matrix, then use <code>melt</code> or <code>reshape</code> + <code>as.data.frame</code> or just do something like this (assuming you have a matrix called <code>M</code> with the three dimensions discussed above):</p> <pre><code>d&lt;-data.frame(year=rep(1991:1995,each=40), agency=rep(c("D","O","T","R"),50), iteration=rep(0:9,5,each=4), spend=as.vector(M)) </code></pre> <p>in order to get a data frame that looks like this:</p> <pre><code> year agency iteration spend 1 1996 D 0 49.184181 2 1996 O 0 4.351598 3 1996 R 0 16.051958 4 1996 T 0 -14.896019 5 1996 D 1 45.910362 6 1996 O 1 2.173807 7 1996 R 1 17.161775 ... </code></pre> <p>Now you can use <code>aggregate</code> to apply your function, like this:</p> <pre><code>aggregate(spend~agency+year,d,myfunc) </code></pre> <p>to get</p> <pre><code> agency year spend.V1 spend.2.5% spend.97.5% 1 D 1996 35.380610 3.989422 54.098005 2 O 1996 2.634854 -2.700893 8.223760 3 R 1996 13.607076 3.737874 17.111344 4 T 1996 -32.289610 -66.065034 -4.669537 5 D 1997 37.588003 4.231116 57.039164 6 O 1997 10.818397 3.755926 16.918627 ... </code></pre> <p>and now you can slice and dice as you wish</p> <pre><code>aggregate(spend~year,d,myfunc) aggregate(spend~agency,d,myfunc) etc... </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload