> On Apr 17, 2016, at 8:44 PM, sepehr akhavan <
akhavan...@gmail.com> wrote:
>
> Hi Bob,
> Following your solution on slicing and indexing my matrices and vectors, I wrote my code.
> I have to define some of my matrices and vectors in the model block as they both depend on parameters and data. In particular, in the model section, I define a matrix of the form below:
>
> matrix[1,max(Mi)] K_transpose_div_SigmaPred;
>
> As a reminder, Mi is a vector of number of measurements per subject. So if Mi[1] is 12, it means subject 1 has 12 within-subj measurements. Note that subjects may have different number measurements. So the idea is let's define a big row matrix and if a subject has fewer observations, let's slice this row matrix as K_transpose_div_SigmaPred[1, 1:Mi_i] where Mi_i <- Mi[i]
Why do that? What is Sigma for and what are the model parameters?
> Later in my data block, I have a loop on subjects and in a statement in that loop, I compute K_transpose_div_SigmaPred as:
>
> K_transpose_div_SigmaPred <- to_matrix((K[1:(Mi_i), 1])' * inverse(SigmaPred[1:Mi_i, 1:Mi_i]));
You don't want to compute inverses. Rather than A * inv(B), it's more
stable to use A / B.
And you want to organize your data so it doesn't
need to be transposed if you can help it.
>
> I successfully compile the code. However, I get run-time error when I run the code and the run time is:
> if a subject has 10 measurements, in the line above, the right hand side is a 1 by 10 row matrix where as the left hand side is a bigger row matrix (suppose max(Mi) is 12).
They have to have compatible dimensions.
> I tried to do this:
>
> K_transpose_div_SigmaPred[1, 1:Mi_i] <- to_matrix((K[1:(Mi_i), 1])' * inverse(SigmaPred[1:Mi_i, 1:Mi_i]));
>
> But the line above is not valid and leads to compile error. Also it's not possible to declare K_transpose_div_SigmaPred within the loop as it seems that all variable declaration should be done on top of the model block.
You can define local variables within any block. You can even use
{ and } to create a new block any place just to define local variables.
> What would you think I should do?
I'm not sure what you're trying to do, but you can't multiply
matrices of different dimensionalities.
> Also, I'm so grateful to your guidance and help and so sorry I'm asking a specific question.
Specific is easier to answer than vague.