[英]finding parameters with jags in r
I'm given the data that contains 1000 numbers from uniform distribution ~[a,b] and I need to use jags in r to find it/ I tried this code 我得到的数据包含均匀分布〜[a,b]中的1000个数字,我需要在r中使用尖头来查找它/我尝试了此代码
library(arm)
library('rjags')
library(coda)
library(readr)
x <- read.csv(file='C:/Users/Amir/Desktop/אבנר/data analysis/תרגילים/תרגיל
3/Unif.csv', header=FALSE)
N<-length(x)
y <- x[1:1000,1]
dat<- list("y" = y, "N" = N)
jags.inits <- function() {list (a = -3, b = 30)}
parameters<- c("a", "b")
reg.jags <- jags.model(file='1.txt', data=dat, n.chains = 4, n.adapt = 1000)
update(jags, n.iter=1000)
regression.sim<-coda.samples(reg.jags, variable.names=parameters,
n.iter=15000)
summary(regression.sim)
and the model is 而模型是
model {
for (i in 1:N) {
y[i] ~ dunif(a, b)
}
a ~ dnorm(-5, .0001)
b ~ dnorm(15, .0001)
}
but the result are very bad, instead of around [-3,23] I get around [-42,65] 但是结果非常糟糕,而不是[-3,23]左右我得到了[-42,65]
any help? 有什么帮助吗?
I can't replicate your findings as your code is not reproducible: we don't have access to your data. 我无法复制您的发现,因为您的代码不可复制:我们无权访问您的数据。 But using simulated data with an essentially identical model gives results that seem to be very sensible: 但是,使用具有基本相同模型的模拟数据得出的结果似乎非常明智:
library('runjags')
m <- 'model{
for(i in 1:N){
Obs[i] ~ dunif(a, b)
}
a ~ dnorm(0, 10^-6)
b ~ dnorm(0, 10^-6)
#data# N, Obs
#monitor# a, b
#inits# a, b
}'
N <- 1000
Obs <- runif(N, 1, 7)
a <- 0
b <- 10
results <- run.jags(m)
plot(results)
results
range(Obs)
The upper and lower 95% confidence intervals for a and b respectively are very close to the range of Obs, with mode estimates that are closer to the respective upper and lower 95% CI (the maximum likelihood solution would be exactly at the range of the data), and varying the size of N gives narrower/wider 95% CI. a和b的上限和下限95%置信区间分别非常接近Obs范围,且模式估计值分别接近上限和下限95%CI(最大似然解将恰好在数据),改变N的大小可得出更窄/更宽的95%CI。 So all as expected. 所以一切都如预期。
My best guess at your problem is that your y are somehow all (or nearly all) missing. 我对您的问题的最佳猜测是,您的y某种程度上全部(或几乎全部)丢失了。 If that does not help solve your problem I think you will need to post your dataset. 如果那不能帮助解决您的问题,我认为您将需要发布数据集。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.