第7章のStataコード

第7章 外生変数と内生変数

Rによるシミュレーション演習

clear
set seed 2022


postfile sim2 lambda beta0 beta1 using data2, replace

forvalues lambda = 0.00(0.01)0.60 {
    
    postfile sim1 beta0 beta1 using data1, replace
    
    forvalues i = 1/100 {
    quietly capture drop b0 b1 U X Y
    quietly set obs 1000    // サンプルサイズ 
    generate b0 = 1  // 切片の真の値
    generate b1 = 2  // 係数の真の値
    quietly generate U = rnormal()
    quietly generate X = (1 + `lambda' * U) * runiform()
    quietly generate Y = b0 + b1 * X + U
    quietly regress Y X
    
    post sim1 (e(b)[1, 2]) (e(b)[1, 1])
    
    display "`lambda': `i'"
    }
    
    postclose sim1
    
    use data1, clear
    summarize beta0
    scalar b0mean = r(mean)
    quietly summarize beta1
    scalar b1mean = r(mean)
    post sim2 (`lambda') (b0mean) (b1mean)
 
 
}


postclose sim2

use data2, clear
generate bias0 = beta0 - 1
generate bias1 = beta1 - 2
twoway (scatter bias0 lambda)

twoway (scatter bias1 lambda)