2016-09-05 2 views
0

私は、10000行と12列の大きなデータフレームを持っています(割引データセット)。 列にはさまざまな変数が含まれています。最初の210行はサブジェクト1(「サブジェクト1」の列もあります)、次の210行はサブジェクト2などを表します。ジャグを使用したループスルー(サブセット)

私は、データフレーム内の52のすべての対象をループし、それぞれに関数を割り当てるために、ジャグとループ関数を使用したいと考えています。私のコードは次のようになります。

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

私の計画は、ループにされ、サブセット内の全ての被験者)に次のジャグの機能を割り当てて、しかし、それは動作しません。私の間違いは、ジャグに渡したい変数"nt", "Choice"が正しく定義されていないか、更新されていないということです。

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) 
+2

"それは機能しません"より具体的なものはありますか?どのようなエラー/警告が表示されますか?それは何を生産すると思いますか?可能であれば、これを最小限の再現可能な例に絞ってください。 –

+0

'subsetdiscount'はdata.frameではありません。それはリストです。 'lapply(subsetdiscount、function(x){ })' – Abdou

+0

はどこから 'discountdataset'ですか?これをどのように再現できますか? –

答えて

0

試してみてください。

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) 
}) 

output_modelsを使用すると、メインデータセットを分割する要因のそれぞれの出力を含むリストでなければなりません。 提供されたデータなしでこれをテストすることは非常に難しいことに注意してください。したがって、これがうまくいかない場合、テストのためにいくつかのデータを提供することができます。

私はそれが役に立ちそうです。

関連する問題