ch8.2.do:17 |
[3] | [+] lmathpnl_long | ||
ch8.2.do:17 |
[19] | mathpnl_long2.dta | ||
[50] | [+] lgrunfeld | |||
ch8.2.do:77 |
[72] | ldinvest.dta | ||
ch8.2.do:77 |
[79] | ldinvest.dta | ||
ch8.2.do:106 |
[108] | fictprice.dta | ||
master.do:42 | ||||
→ | ch8.2.do | ↘ | ||
ldinvest.dta |
[77] |
ch8.2.do:72 ch8.2.do:79 |
||
check82 |
[83] | |||
fictprice.dta |
[106] |
ch8.2.do:108 |
||
myvolat |
[127] | |||
mathpnl_long2.dta |
[17] |
ch5.01.do:126 ch5.01.do:151 ch5.01.do:162 ch5.01.do:196 ch8.2.do:3 ch8.2.do:19 |
ch8.2.do open
file:script:do
# | content |
---|---|
1 [+] | // ITSP 8.2 - |
3 [+] | use mathpnl_long , |
4 [+] | su |
6 [+] | egen math4sat = cut(math4score) , |
7 [+] | label var math4sat "Quartile of percent satisfactory, 4th grade" |
8 [+] | egen math7sat = cut(math7score) , |
9 [+] | label var math7sat "Quartile of percent satisfactory, 7th grade" |
10 [+] | label def satis 1 "Poor" 2 "Good" 3 "Better" 4 "Best" |
11 [+] | label val math4sat satis |
12 [+] | label val math7sat satis |
14 [+] | label var expp "Expenditure per pupil" |
15 [+] | label var avgsal "Average teacher salary" |
16 [+] | keep distid year expp avgsal math4sat math7sat |
17 [+] | save mathpnl_long2 , |
19 [+] | use mathpnl_long2 , |
20 [+] | desc |
22 [+] | foreach v of varlist * { |
23 [+] | local lab`v' : variable label `v' |
24 [+] | } |
25 [+] | levelsof year , |
27 [+] | // reshape has carried the value labels, but lost the variable labels |
29 [+] | reshape wide expp avgsal math4sat math7sat , |
30 [+] | desc |
32 [+] | local vars expp avgsal math4sat math7sat |
33 [+] | foreach v of local vars { |
34 [+] | foreach y of local yrlist { |
35 [+] | label variable `v'`y' "`lab`v'': `y'" |
36 [+] | } |
37 [+] | } |
38 [+] | desc |
40 [+] | reshape long expp avgsal math4sat math7sat , |
41 [+] | desc |
43 [+] | foreach v of varlist * { |
44 [+] | label variable `v' "`lab`v''" |
45 [+] | } |
46 [+] | desc |
48 [+] | * webuse grunfeld, clear |
50 [+] | use grunfeld , |
51 [+] | generate linvest = log(invest) |
52 [+] | mvsumm LD.linvest , |
54 [+] | label var invrate "Three-year average investment rate" |
55 [+] | tsline invrate if inrange(company, 1, 4) & year >= 1937 , /// scheme(s2mono) |
59 [+] | /* forvalues i=1/4 { tsline invrate if company==`i', yline(0) ti("Firm `i'") /// nodraw name(tsline`i',replace) /// yscale(range(-0.2 0.3)) ytick(-0.2(0.1)0.3) ylab(-0.2(0.1)0.3) } graph combine tsline1 tsline2 tsline3 tsline4, /// ti("Three-year average investment rate") */ |
69 [+] | qui rolling r(mean) , |
71 [+] | preserve |
72 [+] | use ldinvest , |
73 [+] | rename end year |
74 [+] | rename _stat_1 rolling_ldinvest |
75 [+] | keep if year >= 1939 |
76 [+] | xtset company year |
77 [+] | save ldinvest , |
78 [+] | restore |
79 [+] | merge company year using ldinvest , |
80 [+] | drop _merge |
81 [+] | summarize invrate rolling_ldinvest |
83 [+] | save check82 , |
87 [+] | /* * assume that s is the variable for which volatility is desired, * and that it is only recorded on trading days. * assume date is a proper daily Stata date variable (i.e. consecutive * integers for each calendar day). * scale dscorr by the sqrt of the number of intervening days * square the variable generate deld2 = deld + deld[_n-1] if dscorr[_n-1] == . replace deld2 = deld if deld2 == . generate dscorrd = dscorr / sqrt(deld2) */ |
99 [+] | clear |
100 [+] | set obs 3000 |
101 [+] | g date = 14000+_n |
102 [+] | format date %td |
103 [+] | drop if mod(_n,7)==0 | mod(_n,6)==0 |
104 [+] | g p = 10*(uniform()+1) + 40 |
105 [+] | su |
106 [+] | save fictprice , |
108 [+] | use fictprice , |
109 [+] | qui generate dscorr = 100 * (p - p[_n-1]) |
110 [+] | qui generate deld = date - date[_n-1] |
111 [+] | qui generate dscorrd = dscorr / sqrt(deld) |
112 [+] | qui generate ds2 = dscorrd^2 |
114 [+] | * generate a month variable * cumulate over the month * generate the month-end value * drop if ssq is missing, which will keep only one value per month * place the new dataset on a monthly calendar and save |
120 [+] | gen mon = mofd(date) |
121 [+] | bysort mon: generate cumds2 = sum(ds2) |
123 [+] | qui bysort mon: generate ssq = sqrt(cumds2) if _n == _N |
125 [+] | drop if ssq == . |
126 [+] | tsset mon , |
127 [+] | save myvolat , |
129 [+] | label var mon month |
130 [+] | label var ssq "Estimated volatility" |
131 [+] | tsline ssq , |
// ITSP 8.2 - use mathpnl_long, clear su egen math4sat = cut(math4score), at(40.6 55.5 71.2) label var math4sat "Quartile of percent satisfactory, 4th grade" egen math7sat = cut(math7score), at(35.5 49.3 62.5) label var math7sat "Quartile of percent satisfactory, 7th grade" label def satis 1 "Poor" 2 "Good" 3 "Better" 4 "Best" label val math4sat satis label val math7sat satis label var expp "Expenditure per pupil" label var avgsal "Average teacher salary" keep distid year expp avgsal math4sat math7sat save mathpnl_long2, replace use mathpnl_long2, clear desc foreach v of varlist * { local lab`v' : variable label `v' } levelsof year, local(yrlist) // reshape has carried the value labels, but lost the variable labels reshape wide expp avgsal math4sat math7sat, i(distid) j(year) desc local vars expp avgsal math4sat math7sat foreach v of local vars { foreach y of local yrlist { label variable `v'`y' "`lab`v'': `y'" } } desc reshape long expp avgsal math4sat math7sat, i(distid) j(year) desc foreach v of varlist * { label variable `v' "`lab`v''" } desc * webuse grunfeld, clear use grunfeld, clear generate linvest = log(invest) mvsumm LD.linvest, generate(invrate) stat(mean) window(3) end label var invrate "Three-year average investment rate" tsline invrate if inrange(company, 1, 4) & year >= 1937, by(company, ti(" ")) /// yline(0) ysc(range(-0.2 0.3)) ytick(-0.2(0.1)0.3) ylab(-0.2(0.1)0.3) /// scheme(s2mono) /* forvalues i=1/4 { tsline invrate if company==`i', yline(0) ti("Firm `i'") /// nodraw name(tsline`i',replace) /// yscale(range(-0.2 0.3)) ytick(-0.2(0.1)0.3) ylab(-0.2(0.1)0.3) } graph combine tsline1 tsline2 tsline3 tsline4, /// ti("Three-year average investment rate") */ qui rolling r(mean), window(3) saving(ldinvest, replace): summarize LD.linvest preserve use ldinvest, clear rename end year rename _stat_1 rolling_ldinvest keep if year >= 1939 xtset company year save ldinvest, replace restore merge company year using ldinvest, unique drop _merge summarize invrate rolling_ldinvest save check82, replace /* * assume that s is the variable for which volatility is desired, * and that it is only recorded on trading days. * assume date is a proper daily Stata date variable (i.e. consecutive * integers for each calendar day). * scale dscorr by the sqrt of the number of intervening days * square the variable generate deld2 = deld + deld[_n-1] if dscorr[_n-1] == . replace deld2 = deld if deld2 == . generate dscorrd = dscorr / sqrt(deld2) */ clear set obs 3000 g date = 14000+_n format date %td drop if mod(_n,7)==0 | mod(_n,6)==0 g p = 10*(uniform()+1) + 40 su save fictprice, replace use fictprice, clear qui generate dscorr = 100 * (p - p[_n-1]) qui generate deld = date - date[_n-1] qui generate dscorrd = dscorr / sqrt(deld) qui generate ds2 = dscorrd^2 * generate a month variable * cumulate over the month * generate the month-end value * drop if ssq is missing, which will keep only one value per month * place the new dataset on a monthly calendar and save gen mon = mofd(date) bysort mon: generate cumds2 = sum(ds2) qui bysort mon: generate ssq = sqrt(cumds2) if _n == _N drop if ssq == . tsset mon, monthly save myvolat, replace label var mon month label var ssq "Estimated volatility" tsline ssq, ti("Monthly volatility from Merton method") scheme(s2mono)