I need to run the same exercise (Monte Carlo simulations) from Stata to R.
The codes I have used in Stata are the codes bellow. How can I do this using R? (I have searched for many tutorials, but I still didn't manage to do it in R).
* Simulations (10, 100 and 1000 sample replications/iterations)
clear
drop _all
set obs 100
set seed 54231
gen x = ((rnormal()))*10 + 40
* Generating true_y, considering Beta = 0,035
gen true_y = 5+0.03500*x
save truth, replace
twoway scatter true_y x
program hetero1
version 13
args c
use truth, clear
gen y = true_y + rnormal()
regress y x
end
foreach i in 10 100 1000 {
simulate _b, reps (`i'): hetero1
sum _b_x
twoway histogram _b_x, fraction xline(+0.03500, lcolor(red)) xline(`r(mean)', lcolor(green)) fcolor(none) lcolor(gs0) legend(off) title(`i' Repetições)
graph save graf`i'.gph, replace
}
gr combine graf10.gph graf100.gph graf1000.gph
graph export "graf.png", as(png) replace
At the end, I need to obtain these 3 histograms (of estimated beta/coefficients), considering 10, 100 and 1000 sample replications. The red line refers to the "true" coefficient and the green one is the mean of the estimated coefficients - [see the image in the link]