I am trying to replicate a paper that the data are confidential, however, for the sake of practice in coding and performing similar analysis, I am trying to generate artificial data so that after I run the OLS regression I will get similar results to their result.

Suppose that the model I am trying to replicate is

$$y_i = \alpha +\beta_1 x_{1i}+\beta_2 x_{2i}+\beta_3 x_{3i}+u_i$$

Where say that I need to generate each of the variables $y_1, x_1, x_2, x_3$

to get

enter image description here

How should I do this?

I tried:

Generating each of the $x_1, x_2, x_3$ first and then puting them to the equation to generate $y_1$ but I am not getting even close to the result I want.

I did this in python:

import pandas as pd, numpy as np, statsmodels.formula.api as smf

# Generate the data
mean = [0.5, 1000, 10]
Var = [0.5, 60, 3]

A=np.random.normal(loc=0.5,scale=0.5,size=(Stocks, 1))

for a, b in zip(mean, Var):
    A=np.concatenate((A, np.random.normal(loc=a,scale=b, size=(Stocks,1))), axis=1)

df1=pd.DataFrame(A, columns=['Betas','M/B','Size', 'P/E'])

df1['PAR_stock']=df1['PAR_stock'].values+np.random.normal(loc=0, scale=0.03, size=(Stocks,1))
formula = 'PAR_stock ~ Betas + Size + Q("P/E") + Q("M/B")'
results = smf.ols(formula, df1).fit()

Where the results are

enter image description here

As you can see the results do not match what was expected. Furthermore, the standard errors of the coefficients is very small, which is not what is required.

Therefore, is anyone aware of how to generate data such that it allows me to replicate the OLS regression with similar standard errors of the OLS coefficients?

Thank you so much for your time.


1 Answer 1


Here you go:

# Generate matrix of x predictors
X <- matrix(rnorm(100*3), ncol = 3)

# Multiple this matrix by your coefficient and add the offset for the intercept
y_star <- X %*% c(.8, .36, -.85) + .05

# Add random noise to y
y <- rnorm(100, y_star)  

# fit the model
fit <- lm(y~ X)


This will give you the following coefficients:

            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  0.11179    0.09785   1.142    0.256    
X1           0.65261    0.09335   6.991 3.62e-10 ***
X2           0.52333    0.10487   4.990 2.69e-06 ***
X3          -0.81034    0.09464  -8.562 1.81e-13 ***

It won't be fit exactly because of the random noise being fit (but asymptotically, if repeated many times the coefficients will converge to the true values).

  • $\begingroup$ Thank you for your answer, I just added the codes in python. The idea is not to generate all the explanatory variables from a standard normal what if they are different. I put the codes in python so it makes more sense to my question sorry for the in convenience. But I thought this would make it more complicated. $\endgroup$
    – Anonymous
    Commented Nov 8, 2019 at 11:33
  • $\begingroup$ @rsc05, what SEs are you trying to reproduce? As indicates below, you can reconstruct the X matrix and the Sigma matrix and draw from whatever distribution you want (e.g. normal, t-distribution, pareto...). $\endgroup$
    Commented Nov 8, 2019 at 11:45
  • $\begingroup$ Thank you for this, I just updated my question to make sure to mention the standard errors as you can see in parenthesis. $\endgroup$
    – Anonymous
    Commented Nov 8, 2019 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.