2011-11-15 67 views
2

我想使用R2WinBUGS使用這個例子:R2WinBUGS - 警告消息

code

(請只考慮部分:### 5.4用分析WinBUGS軟件。)

我得到這個錯誤信息:

Error in file(con, "wb") : cannot open the connection 
In addition: Warning messages: 
1: In file.create(to[okay]) : 
    cannot create file 'c:/Program Files/WinBUGS14//System/Rsrc/Registry_Rsave.odc', reason 'Permission denied' 
2: In file(con, "wb") : 
    cannot open file 'c:/Program Files/WinBUGS14//System/Rsrc/Registry.odc': Permission denied 
Warning message: 
running command '"c:/Program Files/WinBUGS14//WinBUGS14.exe" /par "D:/R2WinBUGS/normal/script.txt"' had status 1 
> 

我不知道這是否是正確的功能至關重要的(一切似乎看起來OK)。有沒有辦法擺脫這個?

謝謝。

基督教

PS:

這是R代碼:

library(R2WinBUGS) 
setwd("D:/R2WinBUGS/normal") 

y10 <- rnorm(n = 10, mean = 600, sd = 30) # Sample of 10 birds 
y1000 <- rnorm(n = 1000, mean = 600, sd = 30) # Sample of 1000 birds 

# Save BUGS description of the model to working directory 
sink("model.txt") 
cat(" 
model { 

# Priors 
population.mean ~ dunif(0,5000)  # Normal parameterized by precision 
precision <- 1/population.variance # Precision = 1/variance 
population.variance <- population.sd * population.sd 
population.sd ~ dunif(0,100) 

# Likelihood 
for(i in 1:nobs){ 
    mass[i] ~ dnorm(population.mean, precision) 
} 
} 
",fill=TRUE) 
sink() 

# Package all the stuff to be handed over to WinBUGS 
# Bundle data 
win.data <- list(mass = y1000, nobs = length(y1000)) 

# Function to generate starting values 
inits <- function() 
    list (population.mean = rnorm(1,600), population.sd = runif(1, 1, 30)) 

# Parameters to be monitored (= to estimate) 
params <- c("population.mean", "population.sd", "population.variance") 

# MCMC settings 
nc <- 3     # Number of chains 
ni <- 1000    # Number of draws from posterior (for each chain) 
nb <- 1     # Number of draws to discard as burn-in 
nt <- 1     # Thinning rate 

# Start Gibbs sampler: Run model in WinBUGS and save results in object called out 
out <- bugs(data = win.data, inits = inits, parameters.to.save = params, model.file = "model.txt", 
n.thin = nt, n.chains = nc, n.burnin = nb, n.iter = ni, debug = TRUE, DIC = TRUE, working.directory = getwd()) 

ls() 

out     # Produces a summary of the object 

names(out) 

str(out) 

hist(out$summary[,8])   # Rhat values in the eighth column of the summary 
which(out$summary[,8] > 1.1)  # None in this case 

par(mfrow = c(3,1)) 
matplot(out$sims.array[1:999,1:3,1], type = "l") 
matplot(out$sims.array[,,2] , type = "l") 
matplot(out$sims.array[,,3] , type = "l") 

par(mfrow = c(3,1)) 
matplot(out$sims.array[1:20,1:3,1], type = "l") 
matplot(out$sims.array[1:20,,2] , type = "l") 
matplot(out$sims.array[1:20,,3] , type = "l") 

par(mfrow = c(3,1)) 
hist(out$sims.list$population.mean, col = "grey") 
hist(out$sims.list$population.sd, col = "blue") 
hist(out$sims.list$population.variance, col = "green") 

par(mfrow = c(1,1)) 
plot(out$sims.list$population.mean, out$sims.list$population.sd) 

pairs(cbind(out$sims.list$population.mean, out$sims.list$population.sd, out$sims.list$population.variance)) 

summary(out$sims.list$population.mean) 
summary(out$sims.list$population.sd) 
sd(out$sims.list$population.mean) 
sd(out$sims.list$population.sd) 

summary(lm(y1000 ~ 1)) 
+0

你運行的是Windows Vista還是7? –

+0

謝謝。 Windows 7.它可能是一個權限問題? – cs0815

+0

很可能,默認情況下,UAC不允許程序寫入幾乎除用戶文件夾以外的任何內容。您可以通過以管理員身份運行R來更改它。但我認爲這將改變庫文件夾,除非它在Renviron.site中被硬編碼,但我不是100%確定的。 –

回答

7

可能是windows UAC故障。默認情況下,UAC不允許程序寫入幾乎除用戶文件夾以外的任何內容。您可以通過以管理員身份運行R來更改它。但我認爲這將改變庫文件夾,除非它在Renviron.site(在R \ etc文件夾內)硬編碼,但我不能100%確定。

+0

簡單地運行R作爲管理員爲我做了訣竅 –

1

您的鏈接出去跨越一本書的許多章節一個巨大的文件。在評論部分它說:

# You may have to add a 'working.directory' argument to calls to 
# the function bugs(). 

你做到了嗎?還有一堆用戶特定的東西,如:

setwd("C:/_Marc Kery/_WinBUGS book/Naked code") # May have to adapt that 

您是否適當修改了這些項目?

+0

對不起,我沒有意識到這一點。我只對這部分感興趣:租約只考慮部分:### 5.4。使用WinBUGS進行分析 – cs0815

+0

第一個報價適用於整套程序,第二個報價來自您遇到問題的部分。 –

+0

我已經完成了這一步。我不確定第一點(working.directory)。這是指R的一部分的bug功能 - 但是winbugs似乎會導致問題。如果我運行R作爲管理員,我沒有這些問題。所以我不確定你的建議是否是一個「更清潔」的解決方案。 – cs0815

3

我能夠通過定義「bugs.directory」來解決這個問題。

out < - bugs(data = win.data,inits = inits,parameters.to.save = params,model.file =「model.txt」,n.thin = nt,n.chains = nc,n .burnin = nb,n.iter = ni,debug = FALSE,DIC = TRUE,working.directory = getwd(),bugs.directory ='c:/ WinBUGS14')

+0

嗨,我使用win8並定義「bugs.directory」足以解決問題。 – Rafael