Hello everyone,
I am trying to optimize my model code for a (multilevel) regression with vaying intercepts.
I try my code on some simulated test data, containing a matrix of predictors (test_matrix
) as well as a group variable with four levels (test_brands
).
test_matrix <- cbind(rnorm(1000, 4, 7),
rnorm(1000, 0, 1))
test_vector <- c(2,4)
test_brands <- data.frame(brand = rep(1:4, 250))
test_dummies <- test_brands %>%
mutate(brand_1 = as.numeric(ifelse(test_brands == 1, 1, 0)),
brand_2 = as.numeric(ifelse(test_brands == 2, 1, 0)),
brand_3 = as.numeric(ifelse(test_brands == 3, 1, 0)),
brand_4 = as.numeric(ifelse(test_brands == 4, 1, 0))) %>%
dplyr::select(-brand) %>%
as.matrix()
test_vector_dummies <- c(1,2,3,4)
test_outcome <- 100 + test_matrix %*% test_vector + test_dummies %*% test_vector_dummies + rnorm(1000, 0, 1)
test_data <- data.frame(test_outcome,
test_matrix,
test_brands)
I am able to estimate the coefficients (test_vector
) for test_matrix
with the following Bayesian model, which I based on descriptions in the rstan manual:
data {
int<lower=0> N; // number of observations
int<lower=0> J; // number of brands
int<lower=0> K; // number of iv's (columns in the predictors matrix)
vector[N] y; // outcome variable
row_vector[K] x[N]; // matrix (array) of predictors
int<lower=1,upper=J> brand[N]; // brands for N observations
}
parameters {
real alpha[J];
vector[K] beta;
real<lower=0> sigma;
}
model {
for(i in 1:N) {
y[i] ~ normal(alpha[brand[i]] + x[i] * beta, sigma);
}
}
With this model I do get the desired results, however, I am wondering, if there is some possibility to make my code more efficient (e.g. vectorization of the model, …) as I ultimately want to apply this model to rather big datasets. I didn’t find any possibility for vectorization here and I’m not even ure if there is any (because I have only varying intercepts and not varying slopes) … but at least none of my versions worked. However, I am certainly not an expert on rstan as I just started to work with it.
Therefore, I would really appreciate if someone could help me!
Thank you very much in advance,
Alexandra