尝试在R中创建并循环通过不平衡数据的矩阵 [英] Trying to create and loop through matrix of unbalanced data in R
问题描述
最终,我的模型会是这样的:
$ 1
Temp [ijk]〜N(theta [ijk],tau )
theta [ijk] = b0 + b1 * x1 +。 。 。 + bn * xn
等级2
b0 = a00 + a01 * y1 + 。 。等级3(也许?) - 随机等级2拦截$ b $ a $ y
b1 = a10 + a11 * y1 ... $ / b>
$ b通常我会这样做:
Wide < - reshape(Data1,idvar = c(iButton,block),timevar =julian,direction =宽度)
J < - length(unique(Data $ block))
I < - length (数据$ iButton))
Ti < - length(unique(Data $ julian))
Temp < - array(NA,dim = c(I,Ti,J) )
for(t in 1:Ti){
sel.rows< - Wide $ block == t
Temp [,, t]< - as.matrix (宽)[sel.rows,3:Ti]
}
然后我可以一个3D矩阵,我可以在WinBUGS或OpenBUGS循环这样的:
pre $ for(i in 1:J){#Loop (#1循环)$#
$ b $(b
$ 1
$ b Temp [i,j,t] 〜dnorm(theta [i,j,t])
theta [i,j,t]-α.lam[i] + blam1 * radiation [i,j] + blam2 * cwd [i,j] + blam3 * swd [i,j]
}}}
无论如何,不要担心上面的代码的细节,它只是作为一个例子从其他分析。我的主要问题是如何进行这种类型的分析,当我没有一个平衡的设计与相同数量的iButtons每个横断面?任何帮助将不胜感激。我明显是新的R和WinBUGS,并没有太多的电脑编码经验。
$ b $ p
谢谢!
<这里是数据看起来像长(堆)格式:
>数据[1:15,1:4]
iButton julian块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
$ div class =h2_lin>解决方案 这允许每个变量的长度在这个列表中,每个索引都对应于横断面。
所以像这样:
theta < - list()
for(i (Data $ block)){
ibuttons< - unique(Data $ iButton [Data $ block == i])
days< - unique(Data $ julian [Data $ block == (i,长度(ibuttons))的空矩阵与NA的
对于(j中的1:长度(ibuttons)) {
for(t in 1:length(days)){
theta [[i]] [j,t] <-fn(i,ibuttons [j],days [t])
}
}
}
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.
Ultimately, my model will be something like:
Level 1
Temp[ijk] ~ N(theta[ijk], tau)
theta[ijk] = b0 + b1*x1 + . . . + bn*xn
Level 2
b0 = a00 + a01*y1 + . . . an*yn
b1 = a10 + a11*y1 ...
Level 3 (maybe?) - random level 2 intercepts
Normally I would do something like this:
Wide <- reshape(Data1, idvar = c("iButton","block"), timevar = "julian", direction = "wide")
J <- length(unique(Data$block))
I <- length(unique(Data$iButton))
Ti <- length(unique(Data$julian))
Temp <- array(NA, dim = c(I, Ti, J))
for(t in 1:Ti) {
sel.rows <- Wide$block == t
Temp[,,t] <- as.matrix(Wide)[sel.rows, 3:Ti]
}
Then I could have a 3D matrix that I could loop through in WinBUGS or OpenBUGS as such:
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] <- alpha.lam[i] + blam1*radiation[i,j] + blam2*cwd[i,j] + blam3*swd[i,j]
}}}
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.
Thanks!
oh and here is what the data look like in long (stacked) format:
> 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
解决方案 Can you try using a list
instead?
This allows a variable length for each item in the list where each index would correspond to the transect.
So something like this:
theta <- list()
for(i in unique(Data$block)) {
ibuttons <- unique(Data$iButton[Data$block==i])
days <- unique(Data$julian[Data$block==i])
theta[[i]] <- matrix(NA, length(ibuttons), length(days)) # Empty matrix with NA's
for(j in 1:length(ibuttons)) {
for(t in 1:length(days)) {
theta[[i]][j,t] <- fn(i, ibuttons[j], days[t])
}
}
}
这篇关于尝试在R中创建并循环通过不平衡数据的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!