我一直在尝试运行一个使用OpenBugs的R代码。昨天它一直正常工作,但我不知道做了什么导致出现了一些错误。然后我决定看看是否能成功运行一些简单的示例。因此,我尝试了
文件schools.txt中的模型为:
运行程序的其余代码如下所示:
R2WinBUGS
手册中提供的schools示例。文件schools.txt中的模型为:
sink("schools.txt")
cat("
model {
for (j in 1:J)
{
y[j] ~ dnorm (theta[j], tau.y[j])
theta[j] ~ dnorm (mu.theta, tau.theta)
tau.y[j] <- pow(sigma.y[j], -2)
}
mu.theta ~ dnorm (0.0, 1.0E-6)
tau.theta <- pow(sigma.theta, -2)
sigma.theta ~ dunif (0, 1000)
}",fill=TRUE)
sink()
运行程序的其余代码如下所示:
data(schools)
J <- nrow(schools)
y <- schools$estimate
sigma.y <- schools$sd
data <- list ("J", "y", "sigma.y")
inits <- function()
{
list(theta = rnorm(J, 0, 100), mu.theta = rnorm(1, 0, 100),sigma.theta = runif(1, 0, 100))
}
schools.sim <- bugs(data, inits, model.file = "schools.txt",parameters = c("theta", "mu.theta", "sigma.theta"),n.chains = 3, n.iter = 1000)
很不幸,我遇到了之前相同的错误。
> schools.sim <- bugs(data, inits, model.file = "schools.txt",parameters = c("theta", "mu.theta", "sigma.theta"),n.chains = 3, n.iter = 1000)
Error in sd(as.vector(ai)) : unused argument(s) (as.vector(ai))
我还尝试了指定program=c('OpenBugs')
,但是这给了我另一个奇怪的错误,因为OpenBugs在路径中
Error in bugs(data, inits, model.file = "schools.txt", parameters = c("theta", :
unused argument(s) (program = c("OpenBugs"))
编辑
> sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: i386-w64-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] parallel stats graphics grDevices utils datasets methods base
other attached packages:
[1] abind_1.4-0 rjags_3-9 R2WinBUGS_2.1-18 BRugs_0.8-0 R2OpenBUGS_3.2-2.1
[6] LearnBayes_2.12 coda_0.16-1 lattice_0.20-10
loaded via a namespace (and not attached):
[1] boot_1.3-7 grid_2.15.2 plyr_1.8 stringr_0.6.2 tools_2.15.2
traceback()
给出...
> traceback()
No traceback available
EDIT-2
> getAnywhere("sd")
2 differing objects matching ‘sd’ were found
in the following places
.GlobalEnv
package:stats
namespace:stats
Use [] to view one of them`
我该如何删除我在 .GlobalEnv
中定义的 sd
变量?
编辑-3:
问题已解决。我从工作区中删除了 sd
对象,一切都正常了。
sessionInfo()
的结果是什么?traceback()
呢? - Ben Bolkersd
版本。getAnywhere("sd")$where
的结果是什么?它应该是"package:stats" "namespace:stats"
。如果你执行了attach(schools)
,那么会在你的工作空间中创建另一个名为sd
的对象,但这不应该会导致问题,因为它不是一个函数... - Ben BolkergetAnywhere
。请参见 EDIT-2。 - Stat-R