OpenBUGS 및 R 패키지 R2OpenBUGS
을 사용하는 이항 혼합 모델에서 작업하고 있습니다. 간단한 모델을 성공적으로 만들었지 만 불완전한 검색을위한 다른 레벨을 추가하면 오류 variable X is not defined in model or in data set
가 계속 나타납니다. 필자는 데이터 구조를 변경하고 데이터를 OpenBUGS에 직접 입력하는 것을 포함하여 여러 가지를 시도했습니다. 저는 다른 사람이이 오류에 대한 경험을 가지고 있으며 OpenBUGS가 변수 X를 인식하지 못하는 이유를 알고 있습니다. 분명히 말할 수있는 한 명확하게 정의되어 있습니다.OpenBUGS 오류 정의되지 않은 변수
나는 또한 오류 expected the collection operator c error pos 8
을 얻었습니다.이 오류는 이전에 얻은 오류가 아니지만 유사하게 난처합니다.
모델 및 데이터 시뮬레이션 기능은 Kery의 WinBUGS Introduction to Ecologists (2010)에서 가져온 것입니다. 나는 여기에 설정된 데이터가 내 데이터 대신에 유사하다는 점에 유의할 것입니다.
모델뿐만 아니라 데이터 집합을 작성하는 기능이 포함되어 있습니다. 길이에 대한 사과. 여기
# Simulate data: 200 sites, 3 sampling rounds, 3 factors of the level 'trt',
# and continuous covariate 'X'
data.fn <- function(nsite = 180, nrep = 3, xmin = -1, xmax = 1, alpha.vec = c(0.01,0.2,0.4,1.1,0.01,0.2), beta0 = 1, beta1 = -1, ntrt = 3){
y <- array(dim = c(nsite, nrep)) # Array for counts
X <- sort(runif(n = nsite, min = xmin, max = xmax)) # covariate values, sorted
# Relationship expected abundance - covariate
x2 <- rep(1:ntrt, rep(60, ntrt)) # Indicator for population
trt <- factor(x2, labels = c("CT", "CM", "CC"))
Xmat <- model.matrix(~ trt*X)
lin.pred <- Xmat[,] %*% alpha.veC# Value of lin.predictor
lam <- exp(lin.pred)
# Add Poisson noise: draw N from Poisson(lambda)
N <- rpois(n = nsite, lambda = lam)
table(N) # Distribution of abundances across sites
sum(N > 0)/nsite # Empirical occupancy
totalN <- sum(N) ; totalN
# Observation process
# Relationship detection prob - covariate
p <- plogis(beta0 + beta1 * X)
# Make a 'census' (i.e., go out and count things)
for (i in 1:nrep){
y[,i] <- rbinom(n = nsite, size = N, prob = p)
}
# Return stuff
return(list(nsite = nsite, nrep = nrep, ntrt = ntrt, X = X, alpha.vec = alpha.vec, beta0 = beta0, beta1 = beta1, lam = lam, N = N, totalN = totalN, p = p, y = y, trt = trt))
}
data <- data.fn()
그리고
는 모델 :sink("nmix1.txt")
cat("
model {
# Priors
for (i in 1:3){ # 3 treatment levels (factor)
alpha0[i] ~ dnorm(0, 0.01)
alpha1[i] ~ dnorm(0, 0.01)
}
beta0 ~ dnorm(0, 0.01)
beta1 ~ dnorm(0, 0.01)
# Likelihood
for (i in 1:180) { # 180 sites
C[i] ~ dpois(lambda[i])
log(lambda[i]) <- log.lambda[i]
log.lambda[i] <- alpha0[trt[i]] + alpha1[trt[i]]*X[i]
for (j in 1:3){ # each site sampled 3 times
y[i,j] ~ dbin(p[i,j], C[i])
lp[i,j] <- beta0 + beta1*X[i]
p[i,j] <- exp(lp[i,j])/(1+exp(lp[i,j]))
}
}
# Derived quantities
}
",fill=TRUE)
sink()
# Bundle data
trt <- data$trt
y <- data$y
X <- data$X
ntrt <- 3
# Standardise covariates
s.X <- (X - mean(X))/sd(X)
win.data <- list(C = y, trt = as.numeric(trt), X = s.X)
# Inits function
inits <- function(){ list(alpha0 = rnorm(ntrt, 0, 2),
alpha1 = rnorm(ntrt, 0, 2),
beta0 = rnorm(1,0,2), beta1 = rnorm(1,0,2))}
# Parameters to estimate
parameters <- c("alpha0", "alpha1", "beta0", "beta1")
# MCMC settings
ni <- 1200
nb <- 200
nt <- 2
nc <- 3
# Start Markov chains
out <- bugs(data = win.data, inits, parameters, "nmix1.txt", n.thin=nt,
n.chains=nc, n.burnin=nb, n.iter=ni, debug = TRUE)
감사 아니었다!당신은 아주 옳습니다 - 오래된 코드를 복사하고 수정하는 함정. 당신은 잘못 지정된 것에 대해 옳았습니다.하지만 N : win.data = c (y = y, ...)에 대한 값을 추정하고 싶었으므로 하나의 편집으로 N 대신 _inits_를 제공해야했습니다. – sgo
같은 문제가있는 사람도 추가 할 것입니다. 어제 OpenBUGS가 R에서 보낸 데이터를 읽는 데 어려움을 겪었습니다 (따라서 디버깅을 한 후에도 '변수가 정의되지 않은'오류가 발생했습니다). JAGS를 사용하고 'rjags'패키지를 작성하면 훨씬 잘 작동합니다. – sgo
@sgo @file underwater이 오류는 종종 과학 표기법에서'E' 대신'e'가 나오는 것으로 인해 발생합니다 – Qbik