# How to input a matrix with half decided structure?

**URL:** https://discourse.mc-stan.org/t/how-to-input-a-matrix-with-half-decided-structure/3657
**Category:** Developers
**Created:** [March 23, 2018, 6:45pm UTC](https://discourse.mc-stan.org/t/how-to-input-a-matrix-with-half-decided-structure/3657 "2018-03-23T18:45:07Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![ENFP99](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/enfp99/32/1869_2.png) [@ENFP99](https://discourse.mc-stan.org/u/ENFP99)
#### Post date: [March 23, 2018, 6:45pm UTC](https://discourse.mc-stan.org/t/how-to-input-a-matrix-with-half-decided-structure/3657/1 "2018-03-23T18:45:07Z")

</div>

For example, I have a H matrix with 10 columns and I want to estimate the first 4 columns but I know the values of the last 6 columns already. The first 4 columns are like parameters but the last 6 columns are like data, how to achieve that in Stan?

---

<div class="post-metadata">

### Author: ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)
#### Post date: [March 23, 2018, 6:47pm UTC](https://discourse.mc-stan.org/t/how-to-input-a-matrix-with-half-decided-structure/3657/2 "2018-03-23T18:47:09Z")

</div>

Model `H = append_col(unknown_matrix, known_matrix)` somehow.

---

<div class="post-metadata">

### Author: ![ENFP99](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/enfp99/32/1869_2.png) [@ENFP99](https://discourse.mc-stan.org/u/ENFP99)
#### Post date: [March 24, 2018, 7:19pm UTC](https://discourse.mc-stan.org/t/how-to-input-a-matrix-with-half-decided-structure/3657/3 "2018-03-24T19:19:25Z")

</div>

After having H\<- append\_col(H1, H2) (with H1 being the estimators and H2 being the data), which block of stan code should H be put into?

---

<div class="post-metadata">

### Author: ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)
#### Post date: [March 24, 2018, 9:17pm UTC](https://discourse.mc-stan.org/t/how-to-input-a-matrix-with-half-decided-structure/3657/4 "2018-03-24T21:17:31Z")

</div>

The model block; you need to come up with a likelihood for it.

---

<div class="post-metadata">

### Author: ![ENFP99](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/enfp99/32/1869_2.png) [@ENFP99](https://discourse.mc-stan.org/u/ENFP99)
#### Post date: [March 24, 2018, 9:28pm UTC](https://discourse.mc-stan.org/t/how-to-input-a-matrix-with-half-decided-structure/3657/5 "2018-03-24T21:28:06Z")

</div>

Thank you for your reply. But I am still quite confused.

When I put it in the model block it does not work (error message: attempt to assign variable in wrong block. left-hand-side variable origin=parameter

ERROR at line 69), why is that? I need to come up with a likelihood for what? H? H1? H2? What if I just have a prior for H1, no direct likelihood related with H.

---

<div class="post-metadata">

### Author: ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)
#### Post date: [March 24, 2018, 10:05pm UTC](https://discourse.mc-stan.org/t/how-to-input-a-matrix-with-half-decided-structure/3657/6 "2018-03-24T22:05:15Z")

</div>

You should not be declaring `H` in the parameters block, just `H1`. I can’t say how to model it but I presumed you had some joint distribution in mind for the whole matrix. If you just have a conditional distribution of `H2` given `H1`, then you don’t need to glue them together.

---

<div class="post-metadata">

### Author: ![ENFP99](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/enfp99/32/1869_2.png) [@ENFP99](https://discourse.mc-stan.org/u/ENFP99)
#### Post date: [March 24, 2018, 10:28pm UTC](https://discourse.mc-stan.org/t/how-to-input-a-matrix-with-half-decided-structure/3657/7 "2018-03-24T22:28:01Z")

</div>

My situation is not like that. My H has some structure, for example, in the simulated data, the real H is:  
[,1] [,2] [,3] [,4] [,5] [,6] [,7]  
[1,] 1 0 0 1 1 1 0  
[2,] 1 0 0 0 1 1 0  
[3,] 1 1 0 0 1 1 0  
[4,] 1 0 0 0 1 1 0  
[5,] 1 0 0 1 1 1 0  
[6,] 0 0 1 0 1 1 0  
[7,] 1 0 0 0 1 1 0  
[8,] 1 0 1 1 1 1 0  
[9,] 0 0 1 0 1 1 0  
[10,] 1 0 0 0 1 1 0  
[11,] 1 0 0 0 0 0 1  
[12,] 1 1 1 0 0 0 1  
[13,] 0 0 0 0 0 0 1  
[14,] 0 0 0 1 0 0 1  
[15,] 0 0 0 0 0 0 1  
[16,] 1 1 0 0 0 0 1  
[17,] 1 1 0 0 0 0 1  
[18,] 1 0 0 1 0 0 1  
[19,] 1 0 0 0 0 0 1  
While the first 4 columns have 1 and 0s randomly distributed, column 5,6 and 7 has some pattern in it. Then I have  
Phii \<- E .\* H;  
Theta \<- Z\*transpose(Phii); and then Theta is used as a parameter to generated data, so H\_ij =1 means E\_ij is used, H\_ij=- means E\_ij is not used. However, I figured out it is hard to model all three types in H, I will manually input column 5,6, and 7, and I am really interested in estimating in column 1 to 4. How can I tell stan that?

I have tried to put the H\<- append\_col(H1, H2) in the transformed parameter block, the code runs, but H1 have all identical columns, i.e., H1[,1]=H1[,2]=H1[,3]=H1[,4]. I don’t know why it happened and how to fix the code.

---

<div class="post-metadata">

### Author: ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)
#### Post date: [March 24, 2018, 11:59pm UTC](https://discourse.mc-stan.org/t/how-to-input-a-matrix-with-half-decided-structure/3657/8 "2018-03-24T23:59:58Z")

</div>

Well, if `H1` consists of unknown zeros and ones, then you cannot declare that in the parameters block anyway. You would have to figure out some way to integrate those out of the likelihood function.

---

<div class="post-metadata">

### Author: ![ENFP99](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/enfp99/32/1869_2.png) [@ENFP99](https://discourse.mc-stan.org/u/ENFP99)
#### Post date: [March 25, 2018, 12:29am UTC](https://discourse.mc-stan.org/t/how-to-input-a-matrix-with-half-decided-structure/3657/9 "2018-03-25T00:29:03Z")

</div>

I have used stan platform for quite sometime and have been posting questions. I know that in my model, the truth for H is 1 and 0, but I decide to start with a continuous prior that would generate 1 and 0 mainly, for example beta(0.001,0.001). I know this is imperfect, but just to start with as I am not really an expert in bayesian.

---

<div class="post-metadata">

### Author: ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)
#### Post date: [March 25, 2018, 9:22pm UTC](https://discourse.mc-stan.org/t/how-to-input-a-matrix-with-half-decided-structure/3657/10 "2018-03-25T21:22:28Z")

</div>

> [@ENFP99](#):
>
> I know that in my model, the truth for H is 1 and 0, but I decide to start with a continuous prior that would generate 1 and 0 mainly, for example beta(0.001,0.001).

In general, it’s very hard for us to answer questions without a bit more background on what the model is. Here, you haven’t mentioned what the likelihood is, so it’s impossible for us to give you any concrete suggestions.

If there are binary unknowns, you need to marginalize them out. This might be intractable if they’re not conditionally independent, so this might not be a model Stan can fit.

Stan won’t be able to move between the modes at 0 and 1 in a `beta(epsilon, epsilon)` distirbution.
