首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“函数中的非一致性参数:”使用JAGS的简单线性回归

“函数中的非一致性参数:”使用JAGS的简单线性回归
EN

Stack Overflow用户
提问于 2019-11-08 08:50:04
回答 1查看 399关注 0票数 2

我对JAGS和贝叶斯统计是超级新手,只是一直在尝试遵循Crawley的第二版R书中关于贝叶斯统计的第22章。对于简单的线性模型,我完全按照书中的代码复制下来: growth =a+b *tannin,其中有9行两个连续变量: growth和tannins。数据和包是这样的:

代码语言:javascript
复制
install.packages("R2jags")
library(R2jags)

growth <- c(12,10,8,11,6,7,2,3,3)
tannin <- c(0,1,2,3,4,5,6,7,8)
N <- c(1,2,3,4,5,6,7,8,9)
bay.df <- data.frame(growth,tannin,N)

ASCII文件如下所示:

代码语言:javascript
复制
model{
  for(i in 1:N) {
    growth[i] ~ dnorm(mu[i],tau)
    mu[i] <- a+b*tannin[i]
  }
  a ~ dnorm(0.0, 1.0E-4)
  b ~ dnorm(0.0, 1.0E-4)
  sigma <- 1.0/sqrt(tau)
  tau ~ dgamma(1.0E-3, 1.0E-3)
}

但是,当我使用下面的代码时:

代码语言:javascript
复制
> practicemodel <- jags(data=data.jags,parameters.to.save = c("a","b","tau"),
+                   n.iter=100000, model.file="regression.bugs.txt", n.chains=3)

我收到一条错误消息,上面写着:

代码语言:javascript
复制
module glm loaded
Compiling model graph
 Resolving undeclared variables
Deleting model

Error in jags.model(model.file, data = data, inits = init.values, n.chains = n.chains,  : 
  RUNTIME ERROR:
Non-conforming parameters in function :
EN

回答 1

Stack Overflow用户

发布于 2019-11-23 02:39:03

问题已经解决了!

基本上更改是从N <- (1,2...)N <- 9,但还有另一个解决方案,在开始时没有指定N。您可以在data.jags函数中将N指定为数据框中的行数;data.jags = list(growth=bay.df$growth, tannin=bay.df$tannin, N=nrow(bay.df))

下面是新的代码:

代码语言:javascript
复制
# Make the data frame
growth <- c(12,10,8,11,6,7,2,3,3)
tannin <- c(0,1,2,3,4,5,6,7,8)
# CHANGED : This is for the JAGS code to know there are 9 rows of data
N <- 9 code
bay.df <- data.frame(growth,tannin)

library(R2jags)

# Now, write the Bugs model and save it in a text file
sink("regression.bugs.txt") #tell R to put the following into this file
cat("
model{
  for(i in 1:N) {
    growth[i] ~ dnorm(mu[i],tau)
    mu[i] <- a+b*tannin[i]
  }
  a ~ dnorm(0.0, 1.0E-4)
  b ~ dnorm(0.0, 1.0E-4)
  sigma <- 1.0/sqrt(tau)
  tau ~ dgamma(1.0E-3, 1.0E-3)
}
", fill=TRUE)
sink() #tells R to stop putting things into this file.

#tell jags the names of the variables containing the data
data.jags <- list("growth","tannin","N")

# run the JAGS function to produce the function:
practicemodel <- jags(data=data.jags,parameters.to.save = c("a","b","tau"),
                  n.iter=100000, model.file="regression.bugs.txt", n.chains=3)

# inspect the model output. Important to note that the output will
# be different every time because there's a stochastic element to the model
practicemodel 

# plots the information nicely, can visualize the error 
# margin for each parameter and deviance
plot(practicemodel) 

谢谢你的帮助!我希望这对其他人有帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58758911

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档