# Simulate design matrix using Stan

**URL:** <https://discourse.mc-stan.org/t/simulate-design-matrix-using-stan/473>\
**Category:** General\
**Created:** [May 9, 2017, 2:36am UTC](https://discourse.mc-stan.org/t/simulate-design-matrix-using-stan/473 "2017-05-09T02:36:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jys](https://avatars.discourse-cdn.com/v4/letter/j/3e96dc/32.png) [@jys](https://discourse.mc-stan.org/u/jys)\
**Post date:** [May 9, 2017, 2:36am UTC](https://discourse.mc-stan.org/t/simulate-design-matrix-using-stan/473/1 "2017-05-09T02:36:24Z")

</div>

Dear Bob,

I am migrating our recent discussion from Google groups. Thanks for pointing me here.  
Indeed, I am trying to simulate the design matrix using a distribution. Currently, I am using this R code:

```
## Generate response
response <- rep(x = c(1, 0), times = 25)

## Generate covariates conditioned on response
generate_covariates <- function(response_variable){
  ## covariate_i: Bernoulli random variable drawn with probability ranging from 0 to 1
  prob_given_1 <- runif(n = 1, min = 0, max = 1)
  prob_given_0 <- runif(n = 1, min = 0, max = 1)
  ## generate class-conditional distribution
  covar_given_1 <- rbinom(n = length(which(response == 1)), size = 1, prob = prob_given_1)
  covar_given_0 <- rbinom(n = length(which(response == 0)), size = 1, prob = 1 - prob_given_0)
  return(c(covar_given_1, covar_given_0))
}

covariate_list <- sapply(c(1:10), function(x){
  covars <- generate_covariates(response_variable = response)
  return(covars)
  }, simplify = TRUE)

## Put together simulated response and covariates
simulated_data <- data.frame(response, covariate_list, stringsAsFactors = FALSE)

```

Once the simulated\_data is generated, I run a logistic regression on it using Stan.

My question is can I do the simulation process above in Stan itself?

Thanks for any insights!

Best,  
Jy

> On May 8, 2017, at 6:57 PM, Bob Carpenter [carp@alias-i.com](mailto:carp@alias-i.com) wrote:

> You are never going to be able to simulate the design matrix  
> without giving it a distribution—if it’s just covariates,  
> there’s nothing to simulate from. Usually the covariates don’t  
> get modeled because everything’s conditionally independent given  
> their value.

> Which interface are you using?

> - Bob

> P.S. We’re shutting down this list soon. We’ve switched to:  
> [discourse.mc-stan.org](http://discourse.mc-stan.org)

> > On May 7, 2017, at 8:48 PM, Jy [jyoti.shankar@gmail.com](mailto:jyoti.shankar@gmail.com) wrote:
> > 
> > Dear Bob,
> > 
> > Is this possible now in Stan - i.e. a way to simulate the design matrix that will then be provided \> as input to stan? If yes, would you please point me to documentation of how to do this?
> > 
> > Thanks!  
> > Jy
> > 
> > On Saturday, 17 October 2015 17:46:12 UTC-4, Bob Carpenter wrote:  
> > What I’m talking about here is a way to generate a single fake data  
> > set to use as input to Stan to make sure my program is doing the  
> > right thing. Right now, there’s no easy way (that I know of) to convert  
> > Stan output (in any interface) into something that can be fed back in directly  
> > as data.
> > 
> > - Bob

---

<div class="post-metadata">

**Author:** ![sakrejda](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/sakrejda/32/845_2.png) [@sakrejda](https://discourse.mc-stan.org/u/sakrejda)\
**Post date:** [May 9, 2017, 1:20pm UTC](https://discourse.mc-stan.org/t/simulate-design-matrix-using-stan/473/2 "2017-05-09T13:20:18Z")

</div>

Yes you can write your program with a data block where you pass in the simulation parameters and then generate the matrices in the transformed data block, then you fit your model to that data.
