Hello Statalist,
I am new to Stata and have unexpected changes to my results in reruns of code with a seed and cusum function.
I am trying to create a simple simulation in these steps:
1. create binary data with a probability using a seed
2. run the CUSUM function to see the result
Results should be the same every time I run this due to seed but aren't. The CUSUM summary data changes slightly each time. Changes in standard deviation noted from previous runs are: 3.5292196, 3.5285664, 3.5287592 - very small changes(!).
I have confirmed that outcome1, time, and seed values remain consistent when I rerun this section of code multiple times.
Having reread cusum and seed documentation, I believe I am implementing the seed correctly (choice of seed might be improved!) and cusum should be repeatable using a seed.
Question: Is this an expected slight change in the code that I am unaware of, or am I using the seed wrongly with the cusum chart?
Note: the change in sd is small enough that I could "run" with this as is, but as I'm new to Stata, I wanted to make sure my code is doing what I think it is(!) and this is an unexpected outcome which I couldn't explain.
I am new to Stata and have unexpected changes to my results in reruns of code with a seed and cusum function.
I am trying to create a simple simulation in these steps:
1. create binary data with a probability using a seed
2. run the CUSUM function to see the result
Results should be the same every time I run this due to seed but aren't. The CUSUM summary data changes slightly each time. Changes in standard deviation noted from previous runs are: 3.5292196, 3.5285664, 3.5287592 - very small changes(!).
Code:
clear graph drop _all * set seed and obs set seed 1236 set obs 4849 // RUN-IN: binary outcome generator gen outcome1 = cond(runiform() < 0.0275, 1, 0) gen time = runiform() * 60 sort time replace time =round(time) // CUSUM cusum outcome1 time, generate(cs_temp1) nograph * Calculate standard deviation of the CUSUM values summarize cs_temp1 gen sigma = r(sd) noi display sigma
Having reread cusum and seed documentation, I believe I am implementing the seed correctly (choice of seed might be improved!) and cusum should be repeatable using a seed.
Question: Is this an expected slight change in the code that I am unaware of, or am I using the seed wrongly with the cusum chart?
Note: the change in sd is small enough that I could "run" with this as is, but as I'm new to Stata, I wanted to make sure my code is doing what I think it is(!) and this is an unexpected outcome which I couldn't explain.
Comment