Say I wish to generate a random shock per panel unit in my dataset - meaning that it must be constant across time in each panel unit.
I can achieve this as follows:
I'm looking to replace this 2 line code, which generates a random draw per observation and then replaces all the observations within each panel with the value of the 1st "shock" in each panel - with a single line of code. I wish to do this since: this is sure to speed things up in large datasets and I wish to make the code more "elegant". it would also help in making MC simulation codes nicer to work with in panel data. I thought that:
would produce ind_shock as constant inside idcode, but apparently it doesn't!
I can achieve this as follows:
Code:
clear all webuse nlswork bysort idcode: gen ind_shock = rnormal() bysort idcode: replace ind_shock = ind_shock[1]
Code:
bysort idcode: gen ind_shock = rnormal()
Comment