2012-10-20 25 views
2

我正在嘗試將一些代碼從JAGS移植到Stan。說我有以下數據集:在Stan中部分觀測的參數

N <- 10 
nchoices <- 3 
ncontrols <- 3 
toydata <- list("y" = rbinom(N, nchoices - 1, .5), 
       "controls" = matrix(runif(N*ncontrols), N, ncontrols), 
       "N" = N, 
       "nchoices" = nchoices, 
       "ncontrols" = ncontrols) 

和我要運行用下面的代碼多項式Lo​​git(從文檔的第9.5節所):

data { 
    int N; 
    int nchoices; 
    int y[N]; 
    int ncontrols; 
    vector[ncontrols] controls[N]; 
} 

parameters { 
    matrix[nchoices, ncontrols] beta; 
} 

model { 
    for (k in 1:nchoices) 
    for (d in 1:ncontrols) 
     beta[k,d] ~ normal(0,100); 
    for (n in 1:N) 
    y[n] ~ categorical(softmax(beta * controls[n])); 
} 

我現在要修復第一行beta歸零。在JAGS中,我只是在模型塊中聲明

for (i in 1:ncontrols) { 
    beta[1,i] <- 0 
} 

但我不確定在Stan如何做到這一點。我已經嘗試了許多組合沿着文檔(部分已知參數)的6.2節的行,例如,

​​

但他們都沒有工作。有什麼建議麼?

回答

3

這將有助於提及錯誤消息。在這種情況下,如果測試被宣佈爲一個矩陣,那麼你想要的語法是R-樣的語法

beta[1,i] <- 0.0; // you also omitted the semicolon 

要回答你的更廣泛的問題,我相信你在正確的軌道與你的最後的辦法上。我會在稱爲free_beta的參數塊中創建一個參數矩陣,並將這些元素複製到稱爲beta的模型塊中聲明的另一個矩陣,該矩陣頂部有一個額外的行,用於固定的零。像

data { 
    int N; 
    int nchoices; 
    int y[N]; 
    int ncontrols; 
    vector[ncontrols] controls[N]; 
} 

parameters { 
    matrix[nchoices-1, ncontrols] free_beta; 
} 

model { 
    // copy free beta into beta 
    matrix[nchoices,ncontrols] beta; 
    for (d in 1:ncontrols) 
    beta[1,d] <- 0.0; 
    for (k in 2:nchoices) 
    for (d in 1:ncontrols) 
     beta[k,d] <- free_beta[k-1,d]; 

    // priors on free_beta, which execute faster this way 
    for (k in 1:(nchoices-1)) 
    row(free_beta,k) ~ normal(0.0, 100.0); 

    // likelihood 
    for (n in 1:N) 
    y[n] ~ categorical(softmax(beta * controls[n])); 
} 
+0

工程就像一個夢。謝謝。 – griverorz