Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用JAG循环通过(子集)_R_Loops_Jags - Fatal编程技术网

R 使用JAG循环通过(子集)

R 使用JAG循环通过(子集),r,loops,jags,R,Loops,Jags,我有一个10000行12列的大数据框(折扣数据集)。 这些列包含不同的变量。前210行表示主题1(还有一列带有“subject1”),后210行表示主题2,依此类推 我想使用jags和一个循环函数来循环数据帧中的所有52个主题,并为每个主题分配一个函数。我的代码如下所示: #subsetting the dataframe by the variable subjectid subsetdiscount <- split(discountdataset, as.factor(discoun

我有一个10000行12列的大数据框(折扣数据集)。 这些列包含不同的变量。前210行表示主题1(还有一列带有“subject1”),后210行表示主题2,依此类推

我想使用jags和一个循环函数来循环数据帧中的所有52个主题,并为每个主题分配一个函数。我的代码如下所示:

#subsetting the dataframe by the variable subjectid
subsetdiscount <- split(discountdataset, as.factor(discountdataset$subjectid))
尝试:

库(rjags)
图书馆(R2jags)

你能比“它不起作用”更具体一点吗?您收到了哪些错误/警告?你期望它能产生什么?如果可能,请将其缩减为最小的可重复示例。
subsetdiscount
不是数据帧;这是一张单子。使用
lappy(子搜索,函数(x){})
折扣数据集从哪里来?我们如何复制这个?你好,谢谢。是的SubsetDiscovery是一个矩阵列表,我希望下面的代码在这个列表中运行,在一次运行中获取列表中的每个元素(矩阵)。传递给jags nt的变量
library(rjags)

for (i in 1:length(subsetdiscount))
{

nt <- nrow (subsetdiscount)
Choice <- subsetdiscount$choice
amountSS <- subsetdiscount$val_basic
amountLL <- subsetdiscount$val_d
delayDIFF <- subsetdiscount$delay
con <- subsetdiscount$condition


data <- list("nt", "Choice", "amountSS", "amountLL", "delayDIFF", "con") #      to be passed on to JAGS

myinits <- list(
list(k = (c(0.01, 0.01))),
list(temp = (c(6, 6))))


parameters <- c("k", "temp")


samples <- jags(data, inits=myinits, parameters,
             model.file ="singlesubmodel_Ben_roundedchoice.txt", n.chains=2,         n.iter=20000, 
     n.burnin=1, n.thin=1, DIC=T)
library(rjags)
library(R2jags)

subsetdiscount <- split(discountdataset, as.factor(discountdataset$subjectid))

output_models <- lapply(subsetdiscount, function(x) {
  nt <- nrow(x)
  Choice <- x$choice
  amountSS <- x$val_basic
  amountLL <- x$val_d
  delayDIFF <- x$delay
  con <- x$condition
  data <- list("nt", "Choice", "amountSS", "amountLL", "delayDIFF", "con") #      to be passed on to JAGS
  myinits <- list(list(k = (c(0.01, 0.01))),
    list(temp = (c(6, 6))))
  parameters <- c("k", "temp")
  samples <- jags(data, inits=myinits, parameters,
                  model.file ="singlesubmodel_Ben_roundedchoice.txt", 
                  n.chains=2, n.iter=20000, 
                  n.burnin=1, n.thin=1, DIC=T)
  return(samples)
})