# Fake data with interaction

**URL:** <https://discourse.mc-stan.org/t/fake-data-with-interaction/18644>\
**Category:** Modeling\
**Tags:** rstan\
**Created:** [October 14, 2020, 7:52am UTC](https://discourse.mc-stan.org/t/fake-data-with-interaction/18644 "2020-10-14T07:52:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elef](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/elef/32/4280_2.png) [@Elef](https://discourse.mc-stan.org/u/Elef)\
**Post date:** [October 14, 2020, 7:52am UTC](https://discourse.mc-stan.org/t/fake-data-with-interaction/18644/1 "2020-10-14T07:52:12Z")

</div>

Hi,

I would like to generate a fake response using rstan from a model with interaction of Continuous:Categorical variable.

I have made the following model with two variables,

- continuous with two levels and

- categorical of four levels.

and the fake input is

```
Category <- c('A', 'B','C', 'D')
x1_grid <- seq(0, 1, 1)

data_grid <- expand.grid(Category = Category, 
                        x1_grid = x1_grid)

```

Then I sample by setting my own coefficients using the following,

```
N = nrow(data_grid)
sim_ex <- sampling(
  compiled_example, algorithm = 'Fixed_param',
  data = list( N = N, 
                alpha = 0,
                Category = rep(c(49.4, 50, 50, 52.9), length.out = N), # Coefficient for each level
                Category_sigma = rep(c(2, 1.8, 1.7, 2), length.out = N),#Uncertainty for the coefficients
                x1_b = -17, # Coefficient for the continues 
                x1_b_sigma = 2.3, # Uncertainty for the coefficient
                x1_Category = rep(c(2, -1.5, .3, -.9), length.out = N), # Interaction Coefficients 
                x1_Category_sigma = rep(c(.4, .4, .4, .4), length.out = N), #Uncertainty for the coefficients
                
                x1_grid = data_grid$x1_grid,
                Category_grid = rep(c(1, 2, 3, 4), length.out = N),

                sigma = 1
                
  ), refresh = 0)

```

Now, for the simpler case without interaction seems to work. But there is a mistake in the interaction and I don’t know how to solve it.

Also if you have links of examples in simulating fake data with Stan would be great to share them because I have found only a couple ([1](https://khakieconomics.github.io/2017/04/30/An-easy-way-to-simulate-fake-data-in-stan.html))([2](https://mc-stan.org/users/documentation/case-studies/boarding_school_case_study.html))

Thanks!

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [October 15, 2020, 11:58am UTC](https://discourse.mc-stan.org/t/fake-data-with-interaction/18644/2 "2020-10-15T11:58:33Z")

</div>

> [@Elef](#):
>
> But there is a mistake in the interaction and I don’t know how to solve it.

Are you getting an error at runtime or is it that the results do not look correct?

---

<div class="post-metadata">

**Author:** ![Elef](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/elef/32/4280_2.png) [@Elef](https://discourse.mc-stan.org/u/Elef)\
**Post date:** [October 16, 2020, 6:38am UTC](https://discourse.mc-stan.org/t/fake-data-with-interaction/18644/3 "2020-10-16T06:38:18Z")

</div>

> Are you getting an error at runtime or is it that the results do not look correct?

Hi,  
It runs perfectly fine. My problem is that I don’t know how to model the interaction.

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [October 16, 2020, 1:33pm UTC](https://discourse.mc-stan.org/t/fake-data-with-interaction/18644/4 "2020-10-16T13:33:53Z")

</div>

I see in your code you have an interaction term:

```stan
y[n] = normal_rng(
  alpha + Category_betas[n] + // Categorical 
  x1_beta * x1_grid[n] + // Continues
  x1_Cat_beta[n] * x1_grid[n] * Category_grid[n] // Interaction
  ,sigma);

```

Do you mean that you’re having a problem fitting the data once you generate it?

---

<div class="post-metadata">

**Author:** ![Elef](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/elef/32/4280_2.png) [@Elef](https://discourse.mc-stan.org/u/Elef)\
**Post date:** [October 29, 2020, 10:03am UTC](https://discourse.mc-stan.org/t/fake-data-with-interaction/18644/5 "2020-10-29T10:03:00Z")

</div>

Hi,

I have found the mistake, so I post the code that it works as I would expect.

The packages I used

> library(dplyr)  
> library(rstan)  
> library(tidybayes)  
> library(reshape2)  
> library(nlme)  
> library(AlgDesign)  
> library(stringr)

The model with one discrete variable, 3 continuous and a 2-way interaction of the discrete with a continuous

> sim\_data\_TA4 ←   
> ’  
> data {  
> int N;  
> real CD\_Base[N];  
> real CD\_Base\_sigma[N];  
> real Silicone\_b;  
> real Silicone\_b\_sigma;  
> real Silicone\_b\_sq;  
> real Silicone\_b\_sq\_sigma;  
> real Ts\_b;  
> real Ts\_b\_sigma;  
> real Gl\_b;  
> real Gl\_b\_sigma;  
> real CDb\_Ts[N];  
> real CDb\_Ts\_sigma[N];  
> real\<lower=0\> sigma;
> 
> real Silicone\_x[N];  
> real Ts\_x[N];  
> real Gl\_x[N];  
> }
> 
> parameters {}  
> model {}  
> generated quantities {  
> real y[N];  
> real S\_beta;  
> real S\_beta\_sq;  
> real CD\_betas[N];  
> real Ts\_beta;  
> real Gl\_beta;  
> real CDb\_Ts\_beta[N];
> 
> S\_beta = normal\_rng(Silicone\_b, Silicone\_b\_sigma);  
> S\_beta\_sq = normal\_rng(Silicone\_b\_sq, Silicone\_b\_sq\_sigma);  
> for(n in 1:N)  
> CD\_betas[n] = normal\_rng(CD\_Base[n], CD\_Base\_sigma[n]);  
> Ts\_beta = normal\_rng(Ts\_b, Ts\_b\_sigma);  
> Gl\_beta = normal\_rng(Gl\_b, Gl\_b\_sigma);  
> for(n in 1:N)  
> CDb\_Ts\_beta[n] = normal\_rng(CDb\_Ts[n], CDb\_Ts\_sigma[n]); // coef for interaction
> 
> for (n in 1:N) {  
> y[n] = normal\_rng(  
> CD\_betas[n] +  
> S\_beta \* Silicone\_x[n] +  
> S\_beta\_sq \* square(Silicone\_x[n]) +  
> Ts\_beta \* Ts\_x[n] +  
> Gl\_beta \* Gl\_x[n] +  
> CDb\_Ts\_beta[n] \* Ts\_x[n] // interaction  
> ,sigma);  
> }  
> }  
> ’
> 
> compiled\_TA4 ← stan\_model(model\_code = sim\_data\_TA4)

Make a fake DoE

> cand\_list = expand.grid(CD\_levels = c(‘B’, ‘F’),  
> S\_levels = c(0, .5, 1.5, 3),  
> Ts\_levels = c(2.4, 5.6, 7.5),  
> Gl\_levels = c(0, 1.5, 3))
> 
> doe\_grid ← optFederov( ~ Ts\_levels \* CD\_levels + S\_levels + I(S\_levels^2) + Gl\_levels,  
> data = cand\_list, nTrials = 22, crit=“D”)$design
> 
> doe\_grid

Set the coefficients as I like

> beta\_S ← data.frame(beta = -19.7, sd\_beta = .07) # beta = NA, sd\_beta = 1.7  
> beta\_S\_sq ← data.frame(beta = 3.4, sd\_beta = .02) # beta = NA, sd\_beta = .5  
> beta\_Ts ← data.frame(beta = -.59, sd\_beta = .1) # beta = -.38, sd\_beta = .48  
> beta\_Gl ← data.frame(beta = -.4, sd\_beta = .02) # beta = .4, sd\_beta = .2
> 
> beta\_CD ← data.frame(CD\_levels = c(‘B’, ‘F’),  
> betas = c(51.6, 52.8),  
> sd\_betas = c(.28, .11))  
> #Here are the coefficients for the the interaction  
> beta\_CD\_TS ← data.frame(CD\_levels = c(‘B’, ‘F’),  
> betas\_interaction = c(0, 1.7),  
> sd\_betas\_interaction = c(.07, .07))
> 
> beta\_CD1 ← left\_join(doe\_grid, beta\_CD, by = ‘CD\_levels’)  
> beta\_CD2 ← left\_join(beta\_CD1, beta\_CD\_TS, by = ‘CD\_levels’)

Sample

> N = beta\_CD2 %\>% nrow  
> sim\_TA4 ← sampling(  
> compiled\_TA4, algorithm = ‘Fixed\_param’,  
> data = list( N = N,  
> CD = beta\_CD2$betas,  
> CD\_sigma = beta\_CD2$sd\_betas,
> 
> ```
> S_b = beta_S$beta,
> S_b_sigma = beta_S$sd_beta,
>             
> S_b_sq = beta_S_sq$beta,
> S_b_sq_sigma = beta_S_sq$sd_beta,
>             
> CDb_Ts = beta_CD2$betas_interaction,
> CDb_Ts_sigma = beta_CD2$sd_betas_interaction,
>             
> Ts_b = beta_Ts$beta,
> Ts_b_sigma = beta_Ts$sd_beta,
>             
> Gl_b = beta_Gl$beta, 
> Gl_b_sigma = beta_Gl$sd_beta,
>             
> S_x = doe_grid$S_levels,
> Ts_x = doe_grid$Ts_levels,
> Gl_x = doe_grid$Gl_levels,
> sigma = 6
> 
> ```
> 
> ), refresh = 0)

Test (apologies for using lm function in here :D )

> fake\_data0 ← rstan::extract(sim\_TA4, pars = ‘y’) %\>% as.data.frame %\>% .[1:4,]  
> sample\_nr ←   
> rownames(fake\_data0) %\>% as.numeric %\>% summary %\>% .[c(‘Min.’, ‘Max.’)] %\>% str\_c(collapse = “\_”)
> 
> fake\_data0 ← data.frame(doe\_grid, fake\_data0 %\>% t) %\>%  
> melt(id = c(‘CD\_levels’, ‘S\_levels’, ‘Ts\_levels’, ‘Gl\_levels’)) %\>%  
> rename(y\_fake = value) %\>% select(-variable)
> 
> mod\_fake0 ← lm(y\_fake ~ poly(S\_levels, 2, raw = T) + CD\_levels \* Ts\_levels + Gl\_levels,  
> data = fake\_data0)
> 
> fake\_data0 %\>% head(10)  
> mod\_fake0 %\>% summary
> 
> fake\_coef ← broom::tidy(mod\_fake0) %\>%  
> mutate(  
> low = estimate - 2 \* std.error,  
> high = estimate + 2 \* std.error,  
> Sample\_size = sample\_nr  
> ) %\>% .[,]
> 
> ggplot(fake\_coef, aes(estimate, term, xmin = low, xmax = high, height = 0)) +  
> geom\_point() +  
> geom\_vline(xintercept = 0) +  
> geom\_errorbar(colour = ‘red’, width = 0.25)

---

<div class="post-metadata">

**Author:** ![Elef](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/elef/32/4280_2.png) [@Elef](https://discourse.mc-stan.org/u/Elef)\
**Post date:** [October 29, 2020, 10:05am UTC](https://discourse.mc-stan.org/t/fake-data-with-interaction/18644/6 "2020-10-29T10:05:54Z")

</div>

The problem was that the fake response didn’t have the desired properties because I made a mess with the part of interaction. Thanks for your time :)

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [October 29, 2020, 5:27pm UTC](https://discourse.mc-stan.org/t/fake-data-with-interaction/18644/7 "2020-10-29T17:27:38Z")

</div>

Glad it worked out in the end!
