# Generating data from a hypothetical model

Let say I have below model

$$y_{i,t} = 1 + X_1 + X_2 + {\gamma}_i + \epsilon_{i,t}, \epsilon_{i,t} \sim N \left(0, 1 \right), {\gamma}_i \sim N \left(0, \gamma \right)$$

Here, the panel it is represented by suffix $$i$$ and there is time dependent measures for each $$i$$ which is represented by $$t$$

Another constraint is that, within each panel all observations have correlation coefficient as 0.65.

Based on this model, I need to generate 100 datapoints for further analysis.

Can you please help on pointer how I can I generate data based on above data generation process using R?

• $\gamma_i$ is generated from a normal distribution with variance $\gamma$?
– jros
Jun 29, 2022 at 17:15
• What are $X_1$ and $X_2$? Would the correlation coefficient refer to the correlation between $y_{i,t}$ and $y_{i,s}$ for all $i$ and all $t\ne s$? Shall we presume (as is strongly implied) that all explicitly named random variables are independent?
– whuber
Jun 29, 2022 at 19:22
• @whuber I only know that $X_1$ and $X_2$ are 2 exogenous variables (fixed effect). For your remanning questions, yes all are correct Jun 29, 2022 at 21:53
• This looks like some admixture of mathematical and programming notation, making it difficult to understand. Perhaps you mean to write $$y_{i,t}=\beta_0 + \beta_1 X_{i,1} + \beta_2 X_{i,2} + \gamma_i + \epsilon_{i,t}.$$ If not, please clarify what you mean.
– whuber
Jun 29, 2022 at 22:34
• @whuber Your notation is perfect. The main issue I am finding in generating realisations is how to accommodate the correlation of 0.65 Jun 30, 2022 at 6:52

This answer assumes the standard deviation of the Normal distribution you're generating $$\gamma_i$$ from is NOT $$\gamma$$.

You could generate data by defining a few function in R similar to this:

epsilonGenerator <- function() {
eps_i = rnorm(1, mean=0, sd=1)
return eps_i
}

gammaGenerator <- function(stdev) {
gamma_i = rnorm(1, mean=0, sd=stdev)
return gamma_i
}

dataGenerator <- function(X1,X2) {
y_i = 1 + X1 + X2 + gammaGenerator(stdev) + epsilonGenerator()
return y_i
}


Then simply call dataGenerator for each each pair of X1 and X2

• Many thanks. How are we ensuring the constraint that states Another constraint is that, within each panel all observations have correlation coefficient as 0.65.? Jun 29, 2022 at 18:53
• Are X1 and X2 supposed to have correlation = 0.65? Because then we would need to look at how X1 and X2 are generated.
– jros
Jun 29, 2022 at 19:00
• Actually I am not sure. It is not clear from the model documentation. However, being exogenous variables should not they be uncorrelated to avoid multicolinearity? Jun 29, 2022 at 19:08
• exogenous variables are independent, but they could still be correlated and cause multicollinearity
– jros
Jun 29, 2022 at 19:31