Dimension mismatch problems multiplying matrices

Hi,
I am trying to fit a linear regression in which there are environmental covariates at two levels: site and sampling units (local). So that the response to local covariates depend on covariates at the site level.
It is just a part of a more complex model in which I found a dimension mismatch problem that I could not totally understand. This is why I simulated such toy example.
However, when I run this model which is quite simple I had the same dimension mismatch problem and I cannot figure out the reason. I would be very grateful if you could help, i feel a little dummy by now … ;-(

##################My simulated data
rm(list = ls())
set.seed (234)
n.su = 30 #30 sampling units per site
nsites = 10 # number of sites
Ksite = 2 #Environmental covariates at the site level
Xsite = array(NA, c(nsites, Ksite))
Xsite[,1] = 1
Xsite[,2] = rnorm(nsites, 0, 1)

Klocal = 2##Local environmental covariates
Xlocal = array(NA, c(n.su, Klocal))
Xlocal[,1 ] = 1
Xlocal[,2]= rnorm(n.su, 0, 1)#For simplicity same gradient across sampling units at all sites

alphas = array(NA, c(Ksite, Klocal)) #Effect of site environment on response to local covariates
alphas[,1] = rnorm(Ksite, 0, 1)
alphas[,2] = rnorm(Ksite, 0, 1)
betas = Xsite%*%alphas##Response to local covariates in each site

#Simulate responses
sd = 0.5
y = array(NA, c(n.su, nsites))
for(i in 1:nsites)
{
mu = Xlocal%*% betas[i,]
y[, i] = rnorm(n.su, mu, sd)
}

###My stan model
data {
int<lower=1> Nsu;
int<lower=1> Nsite;
int<lower=1> Klocal;
int<lower=1> Ksite;
matrix[Nsu, Klocal] Xlocal;
matrix[Nsite, Ksite] Xsite;
matrix[Nsu, Nsite] y;
}
parameters {
vector[Ksite * Klocal] alpha;
real<lower=0> sigma;
}
transformed parameters {
matrix[Ksite, Klocal] Alpha = to_matrix(alpha);
}
model {
alpha~normal(0, 1);
matrix[Nsite, Klocal] beta = Xsite * Alpha;
for(i in 1:Nsite)
{
vector[Klocal] b = to_vector(beta[i,]);
vector[Nsu] tmp_y = Xlocal*b;
y[,i] ~normal(tmp_y, sigma);
}
}

// Fitting the model
input data
stan_dat ← list(Nsu = nrow(Xlocal), Nsite = nrow(Xsite), Klocal = ncol(Xlocal), Ksite = ncol(Xsite),
Xlocal = Xlocal, Xsite = Xsite, y =y)

First error
[1] “Error in sampler$call_sampler(args_list[[i]]) : "
[2] " Exception: multiply: Columns of m1 (2) and Rows of m2 (4) must match in size (in ‘string’, line 20, column 2 to column 43)”
[1] “error occurred during calling the sampler; sampling not done”

In L20
matrix[Nsite, Klocal] beta = Xsite*Alpha;
Xsite is [Nsite, Ksite] and Alpha [Ksite, Klocal]
This is why I am totally confused with this error…

Something similar occurs in
vector[Klocal] b = to_vector(beta[i,]);
vector[Nsu] tmp_y = Xlocal*b;
Xlocal is [Nsu, Klocal] and b is a column vector of length Klocal

Thanks in advance!

I solved the problems during these days,
the issue was when defining Alpha, which was matrix[Ksite, Klocal] Alpha = to_matrix(alpha);
while it shoudl have been Alpha = to_matrix (alpha, Ksite, Klocal).
The model that now works and recovers the simulated parameters is

data {
int<lower=1> Nsu;
int<lower=1> Nsite;
int<lower=1> Klocal;
int<lower=1> Ksite;
matrix[Nsu, Klocal] Xlocal;
matrix[Nsite, Ksite] Xsite;
matrix[Nsu, Nsite] y;
}
parameters {
vector[Ksite*Klocal] alpha;
real<lower=0> sigma;
}

model {
alpha~normal(0, 1);
matrix[Ksite, Klocal] Alpha = to_matrix (alpha, Ksite, Klocal);
matrix[Nsite, Klocal] beta = XsiteAlpha;
for(i in 1:Nsite)
{
vector[Klocal] b = to_vector(beta[i,]);
vector[Nsu] tmp_y = Xlocal
b;
y[,i] ~normal(tmp_y, sigma);
}
}

2 Likes