2016-03-16 35 views
0

我是R2OpenBUGS的新手,非常神祕的錯誤是相當令人沮喪的。R2OpenBUGS - 矩陣,列表和向量的幾個問題

我嘗試運行一個非常簡單的模型。我曾經成功地運行過類似的模型。 我的問題是我有一個二維數組(矩陣)的事實嗎?

我試圖簡化模型沒有成功。

下面是錯誤:

model is syntactically correct 
expected the collection operator c error pos 11 
model compiled 
expected a number or an NA error pos 1449 
initial values generated, model initialized 
model is updating 
200 updates took 0 s 
tau.0 is not a variable in the model 
tau.1 is not a variable in the model 
model is updating 
****** Sorry something went wrong in procedure StdMonitor.Update in module DeviancePlugin ****** 

,這裏是我的代碼使用

rm(list=ls(all=TRUE)) 
cat("\014") 
library(R2OpenBUGS) 
rat.dat<- read.table("BigRatDat.txt",header=FALSE); 

dose = data.matrix(rat.dat[1]) 
weight = data.matrix(rat.dat[3:13]) 
N<- length(dose); 

cat(" 
model{ 
for(i in 1:50){ 
    for(j in 1:11){ 
     weight[i,j]~dnorm(mu[i,j],tau[i]) 
    mu[i,j]<-b.0[i]+b.1[i]*j 
    } 
    b.0[i]~dnorm(mu.0[i],tau.0) 
    b.1[i]~dnorm(mu.1[i],tau.1) 
    mu.0[i] <-b.00+b.01*dose[i] 
    mu.1[i] <-b.00+b.01*dose[i] 
    tau[i]~dgamma(0.01,0.01) 
    dose[i]~dnorm(0,1) 
} 
b.00~dnorm(0,0.001) 
b.01~dnorm(0,0.001) 
b.10~dnorm(0,0.001) 
b.11~dnorm(0,0.001) 
tau.0~dgamma(0.01,0.01) 
tau.1~dgamma(0.01,0.01) 
} 
",file="Rats2OpenBugs.txt") 


data <- list("dose","weight") 

inits <- function(){ 
    b.0<-rnorm(n=N,0); 
    b.1<-rnorm(n=N,0); 
    b.00<-rnorm(1,0); 
    b.01<-rnorm(1,0); 
    b.10<-rnorm(1,0); 
    b.11<-rnorm(1,0); 
    tau = rep(1,N); 
    tau.0 = 1; 
    tau.1 = 1; 
    list(b.0=b.0,b.1=b.1,b.00=b.00,b.01=b.01,b.10=b.10,b.11=b.11,tau=tau,tau.0=1,tau.1=1) 
} 

params <- c("b.0","b.1","b.00","b.01","b.10","b.11","tau","tau.0","tau.1"); 

output.sim <- bugs(data,inits,params,model.file="Rats2OpenBugs.txt", 
        n.chains=1, n.iter=5000, n.burnin=200, n.thin=1 
        ,debug=TRUE) 

數據文件:

0 1 54 60 63 74 77 89 93 100 108 114 124 
0 2 69 75 81 90 97 120 114 119 126 138 143 
0 3 77 81 87 94 101 110 117 124 134 141 151 
0 4 64 69 77 83 88 96 104 109 120 123 131 
0 5 51 58 62 71 74 81 88 93 99 103 113 
0 6 64 71 77 89 90 100 106 114 122 134 139 
0 7 80 91 97 101 111 119 129 131 137 147 154 
0 8 79 85 89 99 104 105 116 121 132 139 147 
0 9 77 82 88 92 101 109 119 127 135 144 158 
0 10 79 84 91 98 107 114 119 131 137 146 155 
.5 1 62 71 75 79 87 91 100 105 111 121 124 
.5 2 68 73 81 89 94 101 110 114 123 132 139 
.5 3 94 102 109 110 128 133 147 151 153 171 184 
.5 4 81 90 95 102 109 120 128 137 141 154 160 
.5 5 64 69 72 76 84 89 97 103 108 114 124 
.5 6 67 74 81 81 84 95 100 109 119 128 130 
.5 7 73 80 86 89 97 101 110 116 117 135 141 
.5 8 71 74 82 84 93 97 102 113 119 124 131 
.5 9 69 74 79 89 94 100 107 113 124 134 139 
.5 10 60 62 67 74 78 85 92 103 112 121 130 
1 1 59 63 66 75 80 87 99 104 110 115 124 
1 2 56 66 70 81 77 88 96 100 113 120 130 
1 3 71 77 84 80 97 106 111 109 128 133 140 
1 4 59 64 69 76 85 88 96 104 110 119 126 
1 5 65 70 73 77 85 92 96 101 111 118 121 
1 6 61 69 77 81 89 92 107 111 118 127 132 
1 7 80 86 95 99 106 113 127 131 142 150 160 
1 8 74 80 84 90 99 101 108 117 126 133 140 
1 9 71 79 88 90 98 102 116 121 127 139 142 
1 10 69 75 80 86 96 97 104 113 122 129 138 

回答

0

的問題是,我試圖用一個矩陣只有一列作爲矢量。 R沒有問題,但在將數據導出到OpenBUGS時不起作用。該程序預計對矩陣的引用有2個索引(對於行和列)。

我不得不更換:

dose = data.matrix(rat.dat[1]) 

有:

dose = unlist(as.vector(rat.dat[1]))