Hi all,
I have a data set that is basically a collection of 12 subsets of data. Each subset has data for quarterly Gross Domestic Product (GDP) and aggregates of economic variables that are observed at a certain week of the quarter. These certain points are the quarter's weeks. A variable called series corresponds to the week number. Therefore, when series=1, my quarterly aggregates are those observed at week 1 of the quarter. when series=2, my quarterly aggregates are those observed at week 2 of the quarter, and so on.
My aim is to investigates the evolution of the relations between GDP and the aggregates as at different weeks of the quarter. I give an example of my data at the end of this post.
I have a code to calculate mean squared forecast errors. This code is to be done for each "series" (i.e. each subset that corresponds to aggregates observed at a week of the quarter). The code is:
My question:
1- How can I improve my loop so that the same steps are done for each series, while the bold variables picks up the new values corresponding to the new series. I imagine something like MSFEgrs1, MSFEgrs2, MSFEgrs3.......till MSFEgrs12 // and also grs_rw1, grs_rw2, grs_rw3.....and so on for all bold variables.
2- Then displaying a table that has rows spf_rw1, spf_rw2, ....till spf_rw12 in one column,, and grs_rw1 , grs_rw2, grs_rw3 til grs_rw12 in the next column
an example of my data created using dataex is:
I have a data set that is basically a collection of 12 subsets of data. Each subset has data for quarterly Gross Domestic Product (GDP) and aggregates of economic variables that are observed at a certain week of the quarter. These certain points are the quarter's weeks. A variable called series corresponds to the week number. Therefore, when series=1, my quarterly aggregates are those observed at week 1 of the quarter. when series=2, my quarterly aggregates are those observed at week 2 of the quarter, and so on.
My aim is to investigates the evolution of the relations between GDP and the aggregates as at different weeks of the quarter. I give an example of my data at the end of this post.
I have a code to calculate mean squared forecast errors. This code is to be done for each "series" (i.e. each subset that corresponds to aggregates observed at a week of the quarter). The code is:
Code:
gen Lrgdp_first=L.rgdp_first gen MSFEgrs1=. if series==1 { rolling _b, window(33) recursive saving(newdata,replace): reg rgdp_first grs if series==1 & year<2005 preserve use newdata.dta,clear rename end quarter_date tsset series quarter_date save newdata.dta,replace restore merge 1:1 series quarter_date using newdata.dta keep if _merge==3 tsset quarter_date gen Forecast=_b_cons + _b_grs* F.grs gen Error=f.rgdp_first-Forecast gen SqError=(Error)^2 egen MSFE=mean(SqError) if SqError!=. replace MSFEgrs1=MSFE drop Forecast Error SqError MSFE gen spferror=rgdp_first-DRGDP2 gen sq_spferror=(spferror)^2 egen MSFEspf1=mean(sq_spferror) gen RWerror=rgdp_first-Lrgdp_first gen sqRWerror=(RWerror)^2 egen MSFErw1=mean(sqRWerror) gen spf_rw1=MSFEspf1/MSFErw1 gen grs_rw1=MSFEgrs1/MSFErw }
1- How can I improve my loop so that the same steps are done for each series, while the bold variables picks up the new values corresponding to the new series. I imagine something like MSFEgrs1, MSFEgrs2, MSFEgrs3.......till MSFEgrs12 // and also grs_rw1, grs_rw2, grs_rw3.....and so on for all bold variables.
2- Then displaying a table that has rows spf_rw1, spf_rw2, ....till spf_rw12 in one column,, and grs_rw1 , grs_rw2, grs_rw3 til grs_rw12 in the next column
an example of my data created using dataex is:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float(quarter_date series) double rgdp_first float(DRGDP2 grs) 108 1 4.299 3 -1.7400645 109 1 2.591 1.78 -2.8215795 110 1 3.838 2.46 -3.8739974 111 1 4.151 2.19 -3.122698 112 1 2.266 .33 -3.5722666 113 1 3.089 2.14 -1.486287 114 1 2.237 3 -3.54055 115 1 1.99 2.46 -1.0885705 116 1 5.546 3.9 -2.506453 117 1 1.676 2.04 -2.459989 118 1 2.501 1.73 1.6824546 119 1 .501 1.27 2.3742833 120 1 2.096 1.61 .9221073 121 1 1.22 2.31 -1.8166744 122 1 1.793 1.12 1.9894003 123 1 -2.131 -1.28 4.6542344 124 1 -2.811 -1.93 7.097381 125 1 .418 -.08 9.544506 126 1 2.371 2.56 5.090754 127 1 .297 1.55 -.3905046 128 1 1.978 .72 1.9397452 129 1 1.386 2.99 2.390985 130 1 2.65 2.2 -1.3339542 131 1 3.79 1.98 .8567627 132 1 1.799 3.1 -1.7540462 133 1 1.577 2.55 -2.0117745 134 1 2.844 3.06 -1.0230411 135 1 5.87 3.39 -2.672692 136 1 2.581 3.27 -4.1622634 137 1 3.708 3.9 -2.737124 138 1 3.438 2.52 -6.106498 139 1 4.532 3.28 -3.720529 140 1 2.819 3.11 -4.5580325 141 1 .528 1.77 -2.739282 142 1 4.205 2.14 2.9432015 143 1 . 2.45 -.3020817 144 1 2.809 1.42 -.7189608 145 1 4.221 2.92 -.3356397 146 1 2.171 2.59 -4.650067 147 1 4.717 2.16 -3.525065 148 1 5.611 2.31 -1.606285 149 1 2.163 2.38 -2.3640177 150 1 3.521 2.73 -3.76021 151 1 4.298 2.81 -3.24092 152 1 4.242 2.49 -4.067624 153 1 1.417 2.59 -1.483128 154 1 3.288 2.21 -1.802051 155 1 5.585 2.4 .12005798 156 1 4.492 2.95 -1.8684953 157 1 2.288 3.19 -2.4996736 158 1 4.824 3.33 -2.8045006 159 1 5.798 3.85 -2.264354 160 1 5.391 3 -4.6063266 161 1 5.19 4.19 -2.2261415 162 1 2.745 3.25 -.8391379 163 1 1.373 3.22 1.8297783 164 1 1.982 .8 1.295785 165 1 .735 1.16 6.782963 166 1 -.355 1.42 5.368276 167 1 .224 -1.99 6.823607 168 1 5.836 1.36 10.673666 169 1 1.059 2.54 2.260923 170 1 3.137 2.4 .5870026 171 1 .744 1.53 .9940487 172 1 1.598 2.1 1.8984247 173 1 2.37 1.65 1.793958 174 1 7.155 3.74 3.913879 175 1 4.024 3.98 -.25143445 176 1 4.158 4.42 -4.654116 177 1 3.044 4.42 -3.2208996 178 1 3.711 3.43 -2.5053735 179 1 3.147 3.67 -1.2607688 180 1 3.088 3.76 -.883205 181 1 3.414 2.98 -2.734065 182 1 3.805 4.15 -1.9732573 183 1 1.119 3.2 -2.869404 184 1 4.818 4.11 -2.463879 185 1 2.458 3.28 -3.900098 186 1 1.583 2.81 -.5839613 187 1 3.473 2.5 1.2229854 188 1 1.26 2.55 .6914418 189 1 3.382 2.41 -.772797 190 1 3.896 2.6 -1.419048 191 1 .636 1.78 1.9809887 192 1 .597 .66 2.1079657 193 1 1.889 .08 3.705761 194 1 -.252 1.34 7.907846 195 1 -3.804 -2.63 9.775786 196 1 -6.144 -4.91 28.71871 197 1 -1.017 -1.31 33.14579 198 1 3.534 2.31 16.748924 199 1 5.731 2.59 2.3695307 200 1 3.239 2.63 3.368487 201 1 2.387 3.33 -1.2413363 202 1 2.01 2.41 -4.7454147 203 1 3.173 2.23 -.5648026 204 1 1.748 3.54 -.6299318 205 1 1.282 3.24 -1.1670973 206 1 2.464 2.54 -.8973433 207 1 2.752 2.5 .04872819 end format %tq quarter_date
Comment