Hi, all
I have a parameter, theta, that is a N X P matrix. However, in the transformed parameters section, I want to transfer the matrix to a 1 x P vector where the element each column in the vector is the average of the initial matrix simultaneously. Any idea how to make it?
parameters {
matrix [N,P] thetas;
}
transformed parameters {
row_vector [P] Thetas=???
}
For row average you can post multiply by a vector of ones and then divide by the number of columns. For column average you pre multiply by a row vector of ones and then divide by the number of rows.
> r <- 10
> c <- 3
> rmat <- matrix(rnorm(r * c), nr = r, nc = c)
>
> # rowsums
> all.equal(c(rmat %*% rep(1, c)), rowSums(rmat))
[1] TRUE
>
> # colsums
> all.equal(c(rep(1, r) %*% rmat), colSums(rmat))
[1] TRUE
1 Like
Thanks @spinkney . You provide a good idea. I will go to play around with it.