Hello,
I’m trying to fit a hand-coded hierarchical regression mode with random intercepts for subject and item. I have a model structure which samples fine without the subject- and item-specific offset to the intercept for “shift”, but when I run it with these enrichments I eventually get a “bad alloc” error:
Chain 1: Exception: std::bad_alloc (in ‘model688c6fe52ad2_5_interceptsforsubjectsanditems’ at line 45)
[origin: bad_alloc]
[1] “Error in sampler$call_sampler(args_list[[i]]) : "
[2] " Exception: std::bad_alloc (in ‘model688c6fe52ad2_5_interceptsforsubjectsanditems’ at line 45)”
[3] " [origin: bad_alloc]"
[1] “error occurred during calling the sampler; sampling not done”
I’ve looked around on these forums and followed the advice I saw re: setting the iter to 1 etc and closing all other open programs, but my memory is still maxing out eventually before I get any samples back. Below is the stan code:
data {
int<lower = 1> N_trials;
int<lower = 1,upper = 4> w_ans[N_trials];
int<lower = 0, upper = 1> heavy_syll_where_stress_shifts_to[N_trials];
real rem_freq[N_trials];
int<lower = 1> N_subject;
int<lower = 1> N_item;
int<lower = 1, upper = N_subject> subject[N_trials];
int<lower = 1, upper = N_item> item[N_trials];
}
parameters {
real<lower = 0, upper = 1> know_remote;
vector[N_subject] shift_subject_intercept_adjustor;
vector[N_subject] shift_subject_intercept_adjustment_z;
vector[N_item] shift_item_intercept_adjustor;
vector[N_item] shift_item_intercept_adjustment_z;
real BETA_heavy_syll_where_stress_shifts_to;
real BETA_rem_freq;
real INTERCEPT_shift_when_remote_known;
real INTERCEPT_shift_when_remote_unknown;
}
transformed parameters {
simplex[4] theta[N_trials];
for (n in 1:N_trials){
for (s in 1:N_subject){
for (i in 1:N_item){
real overall_intercept_when_remote_known = INTERCEPT_shift_when_remote_known+(shift_subject_intercept_adjustment_z[s]*shift_subject_intercept_adjustor[s])+(shift_item_intercept_adjustment_z[i]*shift_item_intercept_adjustor[i]);
real overall_intercept_when_remote_unknown = INTERCEPT_shift_when_remote_unknown+(shift_subject_intercept_adjustment_z[s]*shift_subject_intercept_adjustor[s])+(shift_item_intercept_adjustment_z[i]*shift_item_intercept_adjustor[i]);
real shift_when_remote_known = inv_logit(overall_intercept_when_remote_known+ BETA_heavy_syll_where_stress_shifts_to*heavy_syll_where_stress_shifts_to[n] + BETA_rem_freq*rem_freq[n]);
real shift_when_remote_unknown = inv_logit(overall_intercept_when_remote_unknown + BETA_heavy_syll_where_stress_shifts_to*heavy_syll_where_stress_shifts_to[n]);
theta[n, 1] = know_remote * shift_when_remote_known ; //yes remote, yes shift
theta[n, 2] = know_remote * (1-shift_when_remote_known); //yes remote, no shift
theta[n, 3] = (1 - know_remote) * shift_when_remote_unknown; // no remote, yes shift
theta[n, 4] = (1 - know_remote) * (1-shift_when_remote_unknown) ; //no remote, no shift
}
}}}
model {
target += beta_lpdf(know_remote | 1, 1);
target += normal_lpdf(shift_subject_intercept_adjustment_z|0, 1 );
target += normal_lpdf(shift_subject_intercept_adjustor|0, 1 );
target += normal_lpdf(shift_item_intercept_adjustment_z|0, 1 );
target += normal_lpdf(shift_item_intercept_adjustor|0, 1 );
target += normal_lpdf(INTERCEPT_shift_when_remote_known|0, 1 );
target += normal_lpdf(INTERCEPT_shift_when_remote_unknown|0, 1 );
target += normal_lpdf(BETA_heavy_syll_where_stress_shifts_to|0, 1 );
target += normal_lpdf(BETA_rem_freq|0, 1 );
for(n in 1:N_trials)
target += categorical_lpmf(w_ans[n] | theta[n]);
}
Any tips for specifying the model in such a way that I can avoid this error? I have 16gb of memory on my local machine, and would really like to be able to run this model. Alternatively if there’s another error I’m missing that is causing the issue, I’d be very glad of any advice in fixing that also.
Below is the R code I’m using:
library(rstanarm)
library(rstantools)
library(tidyverse)
simplified_data <- read_csv("dataforstanforums.csv")
n_subjects <- simplified_data %>%
select(BetterSubject) %>%
distinct() %>%
summarise(
count = n()
)
n_items <- simplified_data %>%
select(LocalBaseID) %>%
distinct() %>%
summarise(
count = n()
)
data_list_5 <- list(N_trials = nrow(simplified_data),
w_ans = simplified_data$MPT_answer,
N_subject = n_subjects$count,
N_item = n_items$count,
subject = simplified_data$BetterSubject,
item = simplified_data$LocalBaseID,
heavy_syll_where_stress_shifts_to = simplified_data$HeavySyllWhereStressShiftsTo,
rem_freq = simplified_data$rem_freq
#subject = simplified_data$Subject
)
fit_one <- stan(file = '5_interceptsforsubjectsanditems.stan', data = data_list_5, chains = 1, warmup = 0, iter = 1, verbose = T, cores = 4, control = list(adapt_delta = .9))
dataforstanforums.csv (96.4 KB) is the anonymized dataset I’m using.
Any help with this would be very much appreciated!