R2OpenBUGS——矩阵、列表和向量的几个问题
R2OpenBUGS - several problems with matrices, list and vectors
我是 R2OpenBUGS 的新手,非常神秘的错误非常令人沮丧。
我尝试运行一个非常简单的模型。我以前曾成功 运行ning 类似的模型。
我的问题是因为我有一个二维数组(矩阵)吗?
我尝试简化模型但没有成功。
错误如下:
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("4")
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
问题是我试图使用只有一列的矩阵作为向量。 R 对此没有问题,但在将数据导出到 OpenBUGS 时它不起作用。该程序期望对矩阵的引用具有 2 个索引(用于行和列)。
我只需要更换:
dose = data.matrix(rat.dat[1])
与:
dose = unlist(as.vector(rat.dat[1]))
我是 R2OpenBUGS 的新手,非常神秘的错误非常令人沮丧。
我尝试运行一个非常简单的模型。我以前曾成功 运行ning 类似的模型。 我的问题是因为我有一个二维数组(矩阵)吗?
我尝试简化模型但没有成功。
错误如下:
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("4")
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
问题是我试图使用只有一列的矩阵作为向量。 R 对此没有问题,但在将数据导出到 OpenBUGS 时它不起作用。该程序期望对矩阵的引用具有 2 个索引(用于行和列)。
我只需要更换:
dose = data.matrix(rat.dat[1])
与:
dose = unlist(as.vector(rat.dat[1]))