楼主: 文雅13
18847 124

[问答] R2winbugs到出数据时出现错误 [推广有奖]

91
文雅13 发表于 2013-1-20 17:18:04
epoh 发表于 2013-1-20 15:55
就以Minmax而言,有三个文件
  code file   : Minmax.ocf
  document  : Minmax.odc (最重要)
缺少函数的话,那不是模型不应该是对的?

92
epoh 发表于 2013-1-21 12:08:53
文雅13 发表于 2013-1-20 17:18
缺少函数的话,那不是模型不应该是对的?
Mv.odc compile后可以得到Mv.ocf,Mv.osf
没有Mv.odc 程序就会产生错
#####
display(log)
check(c:/Bugs/ces_d/ces_d.txt)
unknown type of logical function(betasex<-Mv(a1[1:2], d1[1:9], knotD[],OMEGAu[],v))
data(c:/Bugs/ces_d/data.txt)
command #Bugs:data cannot be executed (is greyed out)
compile(1)
inits(1,c:/Bugs/ces_d/inits1.txt)
command #Bugs:inits cannot be executed (is greyed out)
gen.inits()
command #Bugs:gen.inits cannot be executed (is greyed out)
thin.updater(1)
update(1)
command #Bugs:update cannot be executed (is greyed out)
set(a1)
...
...
#####
wbdev_tutorial.pdf
page 31/46 Appendix B ObservedPlus
library(R2WinBUGS)

### Define the observed successes k and the total trials n
k=9
n=10
### Make a list containing the data to be used by WinBUGS
data=list("k","n")
### Initialize the parameters (in this case, we draw a
### random number from U(0,1)).
inits=function()
{
list(theta=runif(1,0,1))
}
### Make a vector with the parameters that are supposed
### to be returned to R by WinBUGS.
parameters=c("theta")

### Call WinBUGS from R with the bugs command.
observedplus = bugs(data,inits,parameters,"observedplus.txt",n.chains=1,n.iter=10000,n.burnin=1000,
                   n.thin=1,DIC=T,"C:/Program Files/WinBUGS14/",working.directory = "c:/Bugs/webdev/",codaPkg=F,debug=T)
print(observedplus)

Inference for Bugs model at "observedplus.txt", fit using WinBUGS,
1 chains, each with 10000 iterations (first 1000 discarded)
n.sims = 9000 iterations saved
         mean  sd 2.5% 25% 50% 75% 97.5%
theta     0.5 0.1  0.3 0.4 0.5 0.6   0.7
deviance  4.4 1.3  3.5 3.6 3.9 4.7   8.1

DIC info (using the rule, pD = Dbar-Dhat)
pD = 0.9 and DIC = 5.3
DIC is an estimate of expected predictive error (lower deviance is better).
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
ywh19860616 + 5 + 5 + 5 热心

总评分: 学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

93
文雅13 发表于 2013-1-22 09:37:06
epoh 发表于 2013-1-21 12:08
Mv.odc compile后可以得到Mv.ocf,Mv.osf
没有Mv.odc 程序就会产生错
#####
老师,如果y是二分类,是矩阵,那么在R中编写y的程序时应该怎么写?

94
epoh 发表于 2013-2-4 16:09:47
文雅13 发表于 2013-1-22 09:37
老师,如果y是二分类,是矩阵,那么在R中编写y的程序时应该怎么写?
cov数据中没有bmi,smoke,请正确设定
bmi<-cov$bmi          #NULL
smoke<-cov$smoke  #NULL


95
文雅13 发表于 2013-2-4 18:22:38
epoh 发表于 2013-2-4 16:09
cov数据中没有bmi,smoke,请正确设定
bmi
不需要那两个变量  只需要sq和sex,我已修改,但还是出现错误

96
epoh 发表于 2013-2-4 19:53:32
文雅13 发表于 2013-2-4 18:22
不需要那两个变量  只需要sq和sex,我已修改,但还是出现错误
expected key word structure
这个问题容易解决,
因为y是matrix,不是vector.

倒是底下问题,你需要一步一步确认解决,
response数据格式没问题,
问题出在cov.

n1必须是scale,不是vector,因为程序for (i in 1:n1)

n>n1,nobs长度应该是n

97
epoh 发表于 2013-2-4 20:27:42
文雅13 发表于 2013-2-4 18:22
不需要那两个变量  只需要sq和sex,我已修改,但还是出现错误
忘了说,你也没给出variable sumn
sumn should be a vector with length n

98
epoh 发表于 2013-2-5 11:27:02
文雅13 发表于 2013-2-4 18:22
不需要那两个变量  只需要sq和sex,我已修改,但还是出现错误
问题出在你的数据dimension,跟winbugs code完全不符.

1. sumnobs<-length(y)   #1268
   n<-length(nobs)      #317
   y 的长度是1268,结果只用到317   ???

2. for (i in (n1+1):n)
   假设n=317,n>n1,n1=100
   而你给的n1却是个vector
   n1<-cov$n1
    [ 0 0 0 0 0 0...0 0 0 0 3]

3. etaM[i,1]<-beta0all+beta1all*t[(sumn-nobs+1)]}
   你没给出 sumn

4. a[i,j]<-exp(-gamma[i,j])*exp(etaM[i,j])/(1+exp(etaM[i,j]))
   你没给出 gamma

5. for (k in 1:6)
      {
      for (i in nn1[k]:nn2[k])  #nn1: 17  49  81 140 180 233 273 317
        {
        ...
        }
      }

   你的数据nn1长度是8,你只用到6  ??

99
epoh 发表于 2013-2-5 14:12:47
文雅13 发表于 2013-2-4 18:22
不需要那两个变量  只需要sq和sex,我已修改,但还是出现错误
刚仔细看完你的WINBUGS CODE,
y应该是一个 317 x 4 matrix,

100
epoh 发表于 2013-2-6 15:53:16
文雅13 发表于 2013-2-4 18:22
不需要那两个变量  只需要sq和sex,我已修改,但还是出现错误
请在这个帖子回答问题,
这样才有连贯性.
请先确认底下数据:
(一步一步来,后续问题还很多)
y<-response$y
t<-response$t
sumn=response$sumnobs

sqbh<-cov$sq      
sex<-cov$sex      
nobs<-cov$nobs     
n<-length(nobs)


n1必须由你提供,因为只有你自己懂这个模型
上回n1=100,我只是假设而已

sumn数据有误,因为
t[(sumn-nobs+1)]
i=1,sumn[1]-nobs[1]+1= -2,
不应该是负值

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-31 21:14