Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to create and loop through matrix of unbalanced data in R
    text
    copied!<p>I am trying to conduct an hierarchical bayesian analysis but am having a little trouble with R and WinBUGS code. I don't have balanced data and am struggling with the coding. I have temperature data collected daily with iButtons (temperature recording devices) in transects and am trying to generate a model that relates this to remote sensing data. Unfortunately, each transect has a different number of iButtons so creating a 3D matrix of button(i), in transect(j), repeatedly "sampled" on day(t) is a problem for me.</p> <p>Ultimately, my model will be something like:</p> <p>Level 1 Temp[ijk] ~ N(theta[ijk], tau) theta[ijk] = b0 + b1*x1 + . . . + bn*xn</p> <p>Level 2 b0 = a00 + a01*y1 + . . . an*yn b1 = a10 + a11*y1 ...</p> <p>Level 3 (maybe?) - random level 2 intercepts</p> <p>Normally I would do something like this: Wide &lt;- reshape(Data1, idvar = c("iButton","block"), timevar = "julian", direction = "wide")</p> <pre><code>J &lt;- length(unique(Data$block)) I &lt;- length(unique(Data$iButton)) Ti &lt;- length(unique(Data$julian)) Temp &lt;- array(NA, dim = c(I, Ti, J)) for(t in 1:Ti) { sel.rows &lt;- Wide$block == t Temp[,,t] &lt;- as.matrix(Wide)[sel.rows, 3:Ti] } </code></pre> <p>Then I could have a 3D matrix that I could loop through in WinBUGS or OpenBUGS as such:</p> <pre><code>for(i in 1:J) { # Loop over transects/blocks for(j in 1:I) { # Loop over buttons for(t in 1:Ti) { # Loop over days Temp[i,j,t] ~ dnorm(theta[i,j,t]) theta[i,j,t] &lt;- alpha.lam[i] + blam1*radiation[i,j] + blam2*cwd[i,j] + blam3*swd[i,j] }}} </code></pre> <p>Anyway, don't worry about the details of the code above, it's just thrown together as an example from other analyses. My main question is how to do this type of analysis when I don't have a balanced design with equal numbers of iButtons per transect? Any help would be greatly appreciated. I'm clearly new to R and WinBUGS and don't have much previous computer coding experience.</p> <p>Thanks!</p> <p>oh and here is what the data look like in long (stacked) format:</p> <pre><code> &gt; Data[1:15, 1:4] iButton julian block aveT 1 1 1 1 -4.5000000 2 1 2 1 -5.7500000 3 1 3 1 -3.5833333 4 1 4 1 -4.6666667 5 1 5 1 -2.5833333 6 1 6 1 -3.0833333 7 1 7 1 -1.5833333 8 1 8 1 -8.3333333 9 1 9 1 -5.0000000 10 1 10 1 -2.4166667 11 1 11 1 -1.7500000 12 1 12 1 -3.2500000 13 1 13 1 -3.4166667 14 1 14 1 -2.0833333 15 1 15 1 -1.7500000 </code></pre>
 

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