Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Stan syntax in all models #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 39 additions & 32 deletions inst/extdata/2pl_latent_reg.stan
Original file line number Diff line number Diff line change
Expand Up @@ -6,63 +6,70 @@ functions {
matrix[2, cols(W)] adj;
adj[1, 1] = 0;
adj[2, 1] = 1;
if(cols(W) > 1) {
for(k in 2:cols(W)) { // remaining columns
min_w = min(W[1:rows(W), k]);
max_w = max(W[1:rows(W), k]);
if (cols(W) > 1) {
for (k in 2 : cols(W)) {
// remaining columns
min_w = min(W[1 : rows(W), k]);
max_w = max(W[1 : rows(W), k]);
minmax_count = 0;
for(j in 1:rows(W))
minmax_count = minmax_count + W[j,k] == min_w || W[j,k] == max_w;
if(minmax_count == rows(W)) { // if column takes only 2 values
adj[1, k] = mean(W[1:rows(W), k]);
adj[2, k] = (max_w - min_w);
} else { // if column takes > 2 values
adj[1, k] = mean(W[1:rows(W), k]);
adj[2, k] = sd(W[1:rows(W), k]) * 2;
for (j in 1 : rows(W)) {
minmax_count = minmax_count + W[j, k] == min_w || W[j, k] == max_w;
}
if (minmax_count == rows(W)) {
// if column takes only 2 values
adj[1, k] = mean(W[1 : rows(W), k]);
adj[2, k] = max_w - min_w;
} else {
// if column takes > 2 values
adj[1, k] = mean(W[1 : rows(W), k]);
adj[2, k] = sd(W[1 : rows(W), k]) * 2;
}
}
}
return adj;
}
}
data {
int<lower=1> I; // # questions
int<lower=1> J; // # persons
int<lower=1> N; // # observations
int<lower=1, upper=I> ii[N]; // question for n
int<lower=1, upper=J> jj[N]; // person for n
int<lower=0, upper=1> y[N]; // correctness for n
int<lower=1> K; // # person covariates
matrix[J,K] W; // person covariate matrix
int<lower=1> I; // # questions
int<lower=1> J; // # persons
int<lower=1> N; // # observations
array[N] int<lower=1, upper=I> ii; // question for n
array[N] int<lower=1, upper=J> jj; // person for n
array[N] int<lower=0, upper=1> y; // correctness for n
int<lower=1> K; // # person covariates
matrix[J, K] W; // person covariate matrix
}
transformed data {
matrix[2,K] adj; // values for centering and scaling covariates
matrix[J,K] W_adj; // centered and scaled covariates
matrix[2, K] adj; // values for centering and scaling covariates
matrix[J, K] W_adj; // centered and scaled covariates
adj = obtain_adjustments(W);
for(k in 1:K) for(j in 1:J)
W_adj[j,k] = (W[j,k] - adj[1,k]) / adj[2,k];
for (k in 1 : K) {
for (j in 1 : J) {
W_adj[j, k] = (W[j, k] - adj[1, k]) / adj[2, k];
}
}
}
parameters {
vector<lower=0>[I] alpha;
vector[I-1] beta_free;
vector[I - 1] beta_free;
vector[J] theta;
vector[K] lambda_adj;
}
transformed parameters {
vector[I] beta;
beta[1:(I-1)] = beta_free;
beta[I] = -1*sum(beta_free);
beta[1 : I - 1] = beta_free;
beta[I] = -1 * sum(beta_free);
}
model {
alpha ~ lognormal(1, 1);
target += normal_lpdf(beta | 0, 3);
lambda_adj ~ student_t(3, 0, 1);
theta ~ normal(W_adj*lambda_adj, 1);
y ~ bernoulli_logit(alpha[ii].*theta[jj] - beta[ii]);
theta ~ normal(W_adj * lambda_adj, 1);
y ~ bernoulli_logit(alpha[ii] .* theta[jj] - beta[ii]);
}
generated quantities {
vector[K] lambda;
lambda[2:K] = lambda_adj[2:K] ./ to_vector(adj[2,2:K]);
lambda[1] = W_adj[1, 1:K]*lambda_adj[1:K] - W[1, 2:K]*lambda[2:K];
lambda[2 : K] = lambda_adj[2 : K] ./ to_vector(adj[2, 2 : K]);
lambda[1] = W_adj[1, 1 : K] * lambda_adj[1 : K]
- W[1, 2 : K] * lambda[2 : K];
}

89 changes: 51 additions & 38 deletions inst/extdata/gpcm_latent_reg.stan
Original file line number Diff line number Diff line change
Expand Up @@ -13,72 +13,85 @@ functions {
matrix[2, cols(W)] adj;
adj[1, 1] = 0;
adj[2, 1] = 1;
if(cols(W) > 1) {
for(k in 2:cols(W)) { // remaining columns
min_w = min(W[1:rows(W), k]);
max_w = max(W[1:rows(W), k]);
if (cols(W) > 1) {
for (k in 2 : cols(W)) {
// remaining columns
min_w = min(W[1 : rows(W), k]);
max_w = max(W[1 : rows(W), k]);
minmax_count = 0;
for(j in 1:rows(W))
minmax_count = minmax_count + W[j,k] == min_w || W[j,k] == max_w;
if(minmax_count == rows(W)) { // if column takes only 2 values
adj[1, k] = mean(W[1:rows(W), k]);
adj[2, k] = (max_w - min_w);
} else { // if column takes > 2 values
adj[1, k] = mean(W[1:rows(W), k]);
adj[2, k] = sd(W[1:rows(W), k]) * 2;
for (j in 1 : rows(W)) {
minmax_count = minmax_count + W[j, k] == min_w || W[j, k] == max_w;
}
if (minmax_count == rows(W)) {
// if column takes only 2 values
adj[1, k] = mean(W[1 : rows(W), k]);
adj[2, k] = max_w - min_w;
} else {
// if column takes > 2 values
adj[1, k] = mean(W[1 : rows(W), k]);
adj[2, k] = sd(W[1 : rows(W), k]) * 2;
}
}
}
return adj;
}
}
data {
int<lower=1> I; // # items
int<lower=1> J; // # persons
int<lower=1> N; // # responses
int<lower=1,upper=I> ii[N]; // i for n
int<lower=1,upper=J> jj[N]; // j for n
int<lower=0> y[N]; // response for n; y = 0, 1 ... m_i
int<lower=1> K; // # person covariates
matrix[J,K] W; // person covariate matrix
int<lower=1> I; // # items
int<lower=1> J; // # persons
int<lower=1> N; // # responses
array[N] int<lower=1, upper=I> ii; // i for n
array[N] int<lower=1, upper=J> jj; // j for n
array[N] int<lower=0> y; // response for n; y = 0, 1 ... m_i
int<lower=1> K; // # person covariates
matrix[J, K] W; // person covariate matrix
}
transformed data {
int m[I]; // # parameters per item
int pos[I]; // first position in beta vector for item
matrix[2,K] adj; // values for centering and scaling covariates
matrix[J,K] W_adj; // centered and scaled covariates
array[I] int m; // # parameters per item
array[I] int pos; // first position in beta vector for item
matrix[2, K] adj; // values for centering and scaling covariates
matrix[J, K] W_adj; // centered and scaled covariates
m = rep_array(0, I);
for(n in 1:N)
if(y[n] > m[ii[n]]) m[ii[n]] = y[n];
for (n in 1 : N) {
if (y[n] > m[ii[n]]) {
m[ii[n]] = y[n];
}
}
pos[1] = 1;
for(i in 2:(I))
pos[i] = m[i-1] + pos[i-1];
for (i in 2 : I) {
pos[i] = m[i - 1] + pos[i - 1];
}
adj = obtain_adjustments(W);
for(k in 1:K) for(j in 1:J)
W_adj[j,k] = (W[j,k] - adj[1,k]) / adj[2,k];
for (k in 1 : K) {
for (j in 1 : J) {
W_adj[j, k] = (W[j, k] - adj[1, k]) / adj[2, k];
}
}
}
parameters {
vector<lower=0>[I] alpha;
vector[sum(m)-1] beta_free;
vector[sum(m) - 1] beta_free;
vector[J] theta;
vector[K] lambda_adj;
}
transformed parameters {
vector[sum(m)] beta;
beta[1:(sum(m)-1)] = beta_free;
beta[sum(m)] = -1*sum(beta_free);
beta[1 : sum(m) - 1] = beta_free;
beta[sum(m)] = -1 * sum(beta_free);
}
model {
alpha ~ lognormal(1, 1);
target += normal_lpdf(beta | 0, 3);
theta ~ normal(W_adj*lambda_adj, 1);
theta ~ normal(W_adj * lambda_adj, 1);
lambda_adj ~ student_t(3, 0, 1);
for (n in 1:N)
target += pcm(y[n], theta[jj[n]].*alpha[ii[n]],
for (n in 1 : N) {
target += pcm(y[n], theta[jj[n]] .* alpha[ii[n]],
segment(beta, pos[ii[n]], m[ii[n]]));
}
}
generated quantities {
vector[K] lambda;
lambda[2:K] = lambda_adj[2:K] ./ to_vector(adj[2,2:K]);
lambda[1] = W_adj[1, 1:K]*lambda_adj[1:K] - W[1, 2:K]*lambda[2:K];
lambda[2 : K] = lambda_adj[2 : K] ./ to_vector(adj[2, 2 : K]);
lambda[1] = W_adj[1, 1 : K] * lambda_adj[1 : K]
- W[1, 2 : K] * lambda[2 : K];
}
75 changes: 42 additions & 33 deletions inst/extdata/grsm_latent_reg.stan
Original file line number Diff line number Diff line change
Expand Up @@ -13,68 +13,77 @@ functions {
matrix[2, cols(W)] adj;
adj[1, 1] = 0;
adj[2, 1] = 1;
if(cols(W) > 1) {
for(k in 2:cols(W)) { // remaining columns
min_w = min(W[1:rows(W), k]);
max_w = max(W[1:rows(W), k]);
if (cols(W) > 1) {
for (k in 2 : cols(W)) {
// remaining columns
min_w = min(W[1 : rows(W), k]);
max_w = max(W[1 : rows(W), k]);
minmax_count = 0;
for(j in 1:rows(W))
minmax_count = minmax_count + W[j,k] == min_w || W[j,k] == max_w;
if(minmax_count == rows(W)) { // if column takes only 2 values
adj[1, k] = mean(W[1:rows(W), k]);
adj[2, k] = (max_w - min_w);
} else { // if column takes > 2 values
adj[1, k] = mean(W[1:rows(W), k]);
adj[2, k] = sd(W[1:rows(W), k]) * 2;
for (j in 1 : rows(W)) {
minmax_count = minmax_count + W[j, k] == min_w || W[j, k] == max_w;
}
if (minmax_count == rows(W)) {
// if column takes only 2 values
adj[1, k] = mean(W[1 : rows(W), k]);
adj[2, k] = max_w - min_w;
} else {
// if column takes > 2 values
adj[1, k] = mean(W[1 : rows(W), k]);
adj[2, k] = sd(W[1 : rows(W), k]) * 2;
}
}
}
return adj;
}
}
data {
int<lower=1> I; // # items
int<lower=1> J; // # persons
int<lower=1> N; // # responses
int<lower=1,upper=I> ii[N]; // i for n
int<lower=1,upper=J> jj[N]; // j for n
int<lower=0> y[N]; // response for n; y in {0 ... m_i}
int<lower=1> K; // # person covariates
matrix[J,K] W; // person covariate matrix
int<lower=1> I; // # items
int<lower=1> J; // # persons
int<lower=1> N; // # responses
array[N] int<lower=1, upper=I> ii; // i for n
array[N] int<lower=1, upper=J> jj; // j for n
array[N] int<lower=0> y; // response for n; y in {0 ... m_i}
int<lower=1> K; // # person covariates
matrix[J, K] W; // person covariate matrix
}
transformed data {
int m; // # steps
matrix[2,K] adj; // values for centering and scaling covariates
matrix[J,K] W_adj; // centered and scaled covariates
int m; // # steps
matrix[2, K] adj; // values for centering and scaling covariates
matrix[J, K] W_adj; // centered and scaled covariates
m = max(y);
adj = obtain_adjustments(W);
for(k in 1:K) for(j in 1:J)
W_adj[j,k] = (W[j,k] - adj[1,k]) / adj[2,k];
for (k in 1 : K) {
for (j in 1 : J) {
W_adj[j, k] = (W[j, k] - adj[1, k]) / adj[2, k];
}
}
}
parameters {
vector<lower=0>[I] alpha;
vector[I-1] beta_free;
vector[m-1] kappa_free;
vector[I - 1] beta_free;
vector[m - 1] kappa_free;
vector[J] theta;
vector[K] lambda_adj;
}
transformed parameters {
vector[I] beta;
vector[m] kappa;
beta = append_row(beta_free, rep_vector(-1*sum(beta_free), 1));
kappa = append_row(kappa_free, rep_vector(-1*sum(kappa_free), 1));
beta = append_row(beta_free, rep_vector(-1 * sum(beta_free), 1));
kappa = append_row(kappa_free, rep_vector(-1 * sum(kappa_free), 1));
}
model {
alpha ~ lognormal(1, 1);
target += normal_lpdf(beta | 0, 3);
target += normal_lpdf(kappa | 0, 3);
theta ~ normal(W_adj*lambda_adj, 1);
theta ~ normal(W_adj * lambda_adj, 1);
lambda_adj ~ student_t(3, 0, 1);
for (n in 1:N)
for (n in 1 : N) {
target += rsm(y[n], theta[jj[n]] .* alpha[ii[n]], beta[ii[n]], kappa);
}
}
generated quantities {
vector[K] lambda;
lambda[2:K] = lambda_adj[2:K] ./ to_vector(adj[2,2:K]);
lambda[1] = W_adj[1, 1:K]*lambda_adj[1:K] - W[1, 2:K]*lambda[2:K];
lambda[2 : K] = lambda_adj[2 : K] ./ to_vector(adj[2, 2 : K]);
lambda[1] = W_adj[1, 1 : K] * lambda_adj[1 : K]
- W[1, 2 : K] * lambda[2 : K];
}
Loading