R中的R2WinBUGS错误

时间:2015-08-12 10:01:00

标签: r winbugs r2winbugs

我试图复制一些代码,并且遇到了WinBUGS的麻烦。该代码是在2010年编写的,我认为那时候,软件包安装了R正在寻找并且无法找到的其他文件(因此错误),但我不确定。

R停止尝试运行#bugs.directory(请参阅代码),错误是:

Error in file(con, "rb") : cannot open the connection
In addition: Warning message:
In file(con, "rb") :
  cannot open file 'C:/Users/Hiwi/Documents/R/Win-library/3.0/R2winBUGS/System/Rsrc/Registry.odc': No such file or directory
Error in bugs.run(n.burnin, bugs.directory, WINE = WINE, useWINE = useWINE,  : 
  WinBUGS executable does not exist in C:/Users/Hiwi/Documents/R/Win-library/3.0/R2winBUGS

我有分析的结果,所以如果有另一种方法对" rawdata进行贝叶斯分析"文件(在带有[-3,0]事件窗口的14天模型中)或者如果有人愿意解释代码有什么问题,我将永远感激不尽。

代码是:

rm(list=ls(all=TRUE))   

setwd("C:/Users/Hiwi/Dropbox/Oracle/Oracle CD files/analysis/chapter6_a")

library(foreign)

rawdata <- read.dta("nyt.dta",convert.factors = F)

library(MASS)
summary(glm.nb(rawdata$num_events_14 ~ rawdata$nyt_num))


# WinBUGS code

library("R2WinBUGS")
nb.model <- function(){
        for (i in 1:n){ # loop for all observations 
               # stochastic component
               dv[i]~dnegbin( p[i], r)
               # link and linear predictor
               p[i] <- r/(r+lambda[i])
               log(lambda[i] ) <- b[1] + b[2] * iv[i]
        }
        #
        # prior distributions
        r <- exp(logr)
    logr ~ dnorm(0.0, 0.01)

    b[1]~dnorm(0,0.001)  # prior  (please note: second element is 1/variance)
    b[2]~dnorm(0,0.001)  # prior
}


write.model(nb.model, "negativebinomial.bug")


n <- dim(rawdata)[1] # number of observations


winbug.data <- list(dv = rawdata$num_events_14, 
                    iv = rawdata$nyt_num,
                    n=n)

winbug.inits <- function(){list(logr = 0 ,b=c(2.46,-.37)
                                )} # Ausgangswerte aus der Uniformverteilung zwischen -1 und 1

bug.erg <- bugs(data=winbug.data,
    inits=winbug.inits, 
    #inits=NULL,
    parameters.to.save = c("b","r"),
    model.file="negativebinomial.bug",
    n.chains=3, n.iter=10000, n.burnin=5000,
    n.thin=1,
    codaPkg=T,
    debug=F,
    #bugs.directory="C:/Users/Hiwi/Documents/R/Win-library/3.0/R2winBUGS/"
    bugs.directory="C:/Users/Hiwi/Documents/R/Win-library/3.0/R2winBUGS"
    )


tempdir()
setwd(tempdir())
file.rename("codaIndex.txt","simIndex.txt")
file.rename("coda1.txt","sim1.txt")
file.rename("coda2.txt","sim2.txt")
file.rename("coda3.txt","sim3.txt")

posterior <- rbind(read.coda("sim1.txt","simIndex.txt"),read.coda("sim2.txt","simIndex.txt"),read.coda("sim3.txt","simIndex.txt"))
post.df <- as.data.frame(posterior)
summary(post.df)
quantile(post.df[,2],probs=c(.025,.975))
quantile(post.df[,2],probs=c(.05,.95))
quantile(post.df[,2],probs=c(.10,.90))
tempdir()

1 个答案:

答案 0 :(得分:2)

如果没有坐在你的电脑上很难说肯定...也许这与R2WinBUGS在WinBUGS.exe的错误目录中有关?您可以使用bugs.directory函数中的bugs参数将R2WinBUGS指向正确的位置。

如果没有,请尝试安装OpenBUGS,然后再次R2OpenBUGS