Hi all,
This is a toy example that illustrates my problem.
Using rstan
, I would like to fit a simple linear regression model with (possibly) zero covariates. This is the generated data
N <- 21
X <- as.matrix(cbind(rep(x = 1, times = N), seq(from = 1, to = 10, length.out = N)))
a <- 1.5
b <- 1.5
y <- X %*% c(a, b) + rnorm(n = N)
# X_prime <- as.matrix(X[, 2]) # This is not being used
X_prime <- as.matrix(data.frame(row.names = 1:N)) # This is a matrix with 0 columns
data_stan <- list(N = N,
n_predictor = ncol(X_prime),
X = X_prime,
y = c(y))
This is the .stan
model
data {
int<lower=0> N;
int<lower=0> n_predictor;
matrix[N, n_predictor] X;
vector[N] y;
}
parameters {
real a;
vector[n_predictor] b;
real sigma;
}
model {
sigma ~ normal(0, 1);
y ~ normal(a + X * b, sigma);
}
But when trying to fit the model with no covariates
fit <- stan(file = "model.stan", data = data_stan)
I got the following error
SAMPLING FOR MODEL 'model' NOW (CHAIN 1).
Chain 1: Unrecoverable error evaluating the log probability at the initial value.
Chain 1: Exception: multiply: m1 must have a positive size, but is 0; dimension size expression = cols() (in 'model455b31f81ef4_model' at line 16)
[1] "Error in sampler$call_sampler(args_list[[i]]) : "
[2] " Exception: multiply: m1 must have a positive size, but is 0; dimension size expression = cols() (in 'model455b31f81ef4_model' at line 16)"
error occurred during calling the sampler; sampling not done
The error message seems to indicate that I should not create a parameter vector with zero length, but this post says otherwise.
Is it possible to have a design matrix with (possibly) zero columns?