title: How to efficiently specify a large predictor matrix for stan data block
body: <p>I would appreciate any help to create a large predictor matrix for stan data block.</p>
  
  <p>I want to use variables <code>w_1</code> to <code>w_K</code> from the data below as predictor "matrix" <code>real&lt;lower=0&gt; weights[N, W];</code> in my model. <code>K=W</code> is the number of variables weights (columns of weights), <code>N</code> is the number of observation (rows of weights), so <code>K</code> and <code>N</code> are <code>int</code>.</p>
  
  <p><code>my current approach</code> below works for a few columns (e.g., <code>K=10</code>) but I have more, <code>K&gt;100</code> columns, therefore, given the data below, I need a function that provides an efficient and scalable way to do this: </p>
  
  <pre><code>#for the desired data block 
      dat1 &lt;- list (N = N, 
      ncases = ncases, A = A, B = B, id = id, P = imput, 
      nn = nn, W = 10, 
      weights = cbind(w_1, w_2, w_3, w_4, w_5, w_6, w_7, w_8, w_9, w_10))
  </code></pre>
  
  <p>I explored <code>compose_data</code> from <a href="https://mjskay.github.io/tidybayes/reference/compose_data.html" rel="nofollow noreferrer">tidybayes</a> but I fail to see how I could use that to accomplish what I want for <code>desired data block</code>. Therefore, Any help would be much appreciated.</p>
  
  <p><strong>#sample data</strong></p>
  
  <pre><code>dat &lt;- data.frame(
  id = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4),
  imput = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5),
  A = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
  B = c(1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0),
  Pass = c(278, 278, 278, 278, 278, 100, 100, 100, 100, 100, 153, 153, 153, 153, 153, 79, 79, 79, 79, 79), 
  Fail = c(740, 743, 742, 743, 740, 7581, 7581, 7581, 7581, 7581, 1231, 1232, 1235, 1235, 1232, 1731, 1732, 1731, 1731, 1731), 
  W_1= c(4, 3, 4, 3, 3, 1, 2, 1, 2, 1, 12, 12, 11, 12, 12, 3, 5, 3, 3, 3),
  W_2= c(3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 12, 12, 12, 12, 12, 3, 3, 3, 3, 3),
  W_3= c(4, 3, 3, 3, 3, 1, 2, 1, 1, 1, 12, 12, 11, 12, 12, 3, 3, 3, 3, 3),
  W_4= c(3, 3, 4, 3, 3, 1, 1, 1, 2, 1, 12, 12, 13, 12, 12, 3, 2, 3, 3, 3),
  W_5= c(3, 3, 3, 3, 3, 1, 0, 1, 1, 1, 12, 12, 12, 12, 12, 3, 3, 3, 3, 3),
  W_6= c(4, 3, 3, 3, 3, 1, 1, 1, 1, 1, 12, 12, 12, 12, 12, 3, 3, 3, 3, 3),
  W_7= c(3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 12, 12, 12, 12, 12, 3, 3, 3, 3, 3),
  W_8= c(3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 15, 12, 12, 12, 12, 3, 3, 3, 3, 3),
  W_9= c(3, 3, 3, 4, 3, 1, 1, 1, 1, 1, 12, 12, 12, 12, 12, 2, 3, 3, 3, 3),
  W_10= c(3, 3, 4, 3, 3, 1, 1, 1, 1, 1, 12, 10, 12, 12, 12, 3, 3, 3, 3, 3)
        )
  </code></pre>
  
  <p><strong>#my current approach</strong></p>
  
  <pre><code>N &lt;- nrow(dat)
  ncases &lt;- dat$Pass
  nn &lt;- dat$Fail + dat$Pass
  A &lt;- dat$A
  B &lt;- dat$B
  id &lt;- dat$id
  imput &lt;- dat$imput
  w_1 &lt;- dat$W_1
  w_2 &lt;- dat$W_2
  w_3 &lt;- dat$W_3
  w_4 &lt;- dat$W_4
  w_5 &lt;- dat$W_5
  w_6 &lt;- dat$W_6
  w_7 &lt;- dat$W_7
  w_8 &lt;- dat$W_8
  w_9 &lt;- dat$W_9
  w_10 &lt;- dat$W_10
  
  #for current data block
      dat_list &lt;-dat %&gt;%compose_data(.n_name = n_prefix("N"))
  
  #for desired data block
      dat1 &lt;- list (N = N, 
                ncases = ncases, A = A, B = B, id = id, P = imput, nn = nn, W = 10,
                weights = cbind(w_1, w_2, w_3, w_4, w_5, w_6, w_7, w_8, w_9, w_10))
  </code></pre>
  
  <p><strong>#current data block</strong></p>
  
  <pre><code>    data{
      int N;                                    // number of observations
      int ncases[N];                        
      int A[N];                                 
      int B[N];                                
      int nn[N];                               
      int id[N];                                
      real&lt;lower=0&gt; w_1[N];                     // variable w_1
      real&lt;lower=0&gt; w_2[N];                     // variable w_2       
      real&lt;lower=0&gt; w_3[N];                     // variable w_3      
      real&lt;lower=0&gt; w_4[N];                     // variable w_4       
      real&lt;lower=0&gt; w_5[N];                     // variable w_5       
      real&lt;lower=0&gt; w_6[N];                     // variable w_6       
      real&lt;lower=0&gt; w_7[N];                     // variable w_7       
      real&lt;lower=0&gt; w_8[N];                     // variable w_8       
      real&lt;lower=0&gt; w_9[N];                     // variable w_9       
      real&lt;lower=0&gt; w_10[N];                    // variable w_10
      }
  </code></pre>
  
  <p><strong>#desired data block</strong></p>
  
  <pre><code>data{
  int N;                                        // number of observations
  int ncases[N];                        
  int A[N];                                  
  int B[N];                                
  int nn[N];                              
  int id[N];                                
  real&lt;lower=0&gt; weights[N, W];                  // N by W block of weights 
  }
  </code></pre>
  
  <p>This question has also been posted <a href="https://github.com/mjskay/tidybayes/issues/157" rel="nofollow noreferrer">here</a>. Thanks in advance for any help.</p>