Jan,
With -foreach- , -forvalues- and -while- you can cover a lot of looping type
needs.
In this case, I believe the following should work:
*rename dependent variable
local i = 1
foreach var of varlist ep-rv {
rename `var' dep`i'
local i = `i' + 1
}
*generate independent variables to match wide format
local i = 1
while `i' <=4 {
foreach var of varlist x - p {
gen indep`var'`i' = `var'
}
local i = `i' + 1
}
drop x - p
gen year = _n
reshape long dep indepx indepz indepa indepp, i(year) j(id)
tsset id year
tab id, gen(D)
*Generate individual intercepts and independent variables
foreach var1 of varlist D* {
foreach var2 of varlist indep* {
gen `var1'`var2' = `var1'*`var2'
}
}
aorder
drop D1indepp D2indepp D3indepa D4indepa D3indepz D4indepz
xtgls dep D* , nocons p(c) corr(psar1)
**Example**
Suupose your data looks like this:
. cl, noobs
ep ea eo rv x z a
p
12.38898 -.1741565 3.10788 9.493736 .1369841 .2551499 .2769154
.7152805
10.04978 1.450661 2.305812 11.04732 .6432207 .0445188 .1180158
.699069
10.30955 .2076075 1.042386 9.105699 .5578017 .4241557 .4079702
.2322688
10.17632 .8222673 3.237755 11.57764 .6047949 .8983462 .7219492
.0981878
12.61388 .4161032 3.531995 11.05705 .684176 .5219247 .871691
.5948404
9.861345 .2186571 2.543519 10.47454 .1086679 .8414094 .4611429
.3533874
10.94411 -.7736335 1.551358 11.09245 .6184582 .2110077 .4216726
.8071709
13.13602 1.216412 1.486686 9.612597 .0610638 .5644092 .8944746
.1024369
10.61442 -.2561719 2.773349 10.91753 .5552388 .2648021 .0580662
.5773884
12.10055 .8602635 2.08501 10.12703 .8714491 .9477426 .6759487
.1091783
.
. local i = 1
. foreach var of varlist ep-rv {
2. rename `var' dep`i'
3.
. local i = `i' + 1
4. }
.
. local i = 1
. while `i' <=4 {
2. foreach var of varlist x - p {
3. gen indep`var'`i' = `var'
4. }
5. local i = `i' + 1
6. }
.
. drop x - p
. gen year = _n
. reshape long dep indepx indepz indepa indepp, i(year) j(id)
(note: j = 1 2 3 4)
Data wide -> long
-----------------------------------------------------------------------------
Number of obs. 10 -> 40
Number of variables 21 -> 7
j variable (4 values) -> id
xij variables:
dep1 dep2 ... dep4 -> dep
indepx1 indepx2 ... indepx4 -> indepx
indepz1 indepz2 ... indepz4 -> indepz
indepa1 indepa2 ... indepa4 -> indepa
indepp1 indepp2 ... indepp4 -> indepp
-----------------------------------------------------------------------------
. tsset id year
panel variable: id, 1 to 4
time variable: year, 1 to 10
. tab id, gen(D)
id | Freq. Percent Cum.
------------+-----------------------------------
1 | 10 25.00 25.00
2 | 10 25.00 50.00
3 | 10 25.00 75.00
4 | 10 25.00 100.00
------------+-----------------------------------
Total | 40 100.00
. foreach var1 of varlist D* {
2. foreach var2 of varlist indep* {
3. gen `var1'`var2' = `var1'*`var2'
4. }
5. }
.
. aorder
. drop D1indepp D2indepp D3indepa D4indepa D3indepz D4indepz
. xtgls dep D* , nocons p(c) corr(psar1)
Cross-sectional time-series FGLS regression
Coefficients: generalized least squares
Panels: heteroskedastic with cross-sectional correlation
Correlation: panel-specific AR(1)
Estimated covariances = 10 Number of obs = 40
Estimated autocorrelations = 4 Number of groups = 4
Estimated coefficients = 14 Time periods = 10
Wald chi2(14) = 20046.72
Log likelihood = -34.20722 Prob > chi2 = 0.0000
------------------------------------------------------------------------------
dep | Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
D1 | 10.37989 .703485 14.75 0.000 9.001087 11.7587
D1indepa | 4.467583 .8529961 5.24 0.000 2.795741 6.139424
D1indepx | -.4760266 .8182822 -0.58 0.561 -2.07983 1.127777
D1indepz | -2.232169 .889334 -2.51 0.012 -3.975231 -.4891059
D2 | .0150844 .4253509 0.04 0.972 -.818588 .8487569
D2indepa | .8427272 .7865749 1.07 0.284 -.6989314 2.384386
D2indepx | .5912364 .7011174 0.84 0.399 -.7829283 1.965401
D2indepz | -.6293221 .7640201 -0.82 0.410 -2.126774 .8681297
D3 | 2.123046 .575656 3.69 0.000 .9947813 3.251311
D3indepp | .9182996 .8079717 1.14 0.256 -.6652959 2.501895
D3indepx | -.1983896 .8707189 -0.23 0.820 -1.904967 1.508188
D4 | 9.837125 .5677948 17.33 0.000 8.724267 10.94998
D4indepp | .7172952 .7409337 0.97 0.333 -.7349082 2.169499
D4indepx | .743207 .7773819 0.96 0.339 -.7804335 2.266848
------------------------------------------------------------------------------
.
.
.
end of do-file
Hope this helps,
Scott
----- Original Message -----
From: "Jan Pettersson" <[email protected]>
To: <[email protected]>
Sent: Wednesday, March 03, 2004 1:23 PM
Subject: st: Re: Re: Serial Correlation in system estimator
> Thanks a lot, I guess this will work. However, I have an additional
> problem...
> My system has the following form:
> EP = a10 + a11*X + a12*Z + a13*A
> EA = a20 + a21*X + a22*Z + a23*A
> EO = a30 + a31*X + a32*P
> RV = a40 + a41*X + a42*P
>
> Data is now in wide format. As I understand it, I must reshape data into
> long. In order to "tag" e.g. X to all dependent vars, would it be correct to
> first make three "copys" of this variable (as well as one copy of Z, A, and
> P) before reshape? I am thinking of:
>
> rename EP dep1
>
> rename EA dep2
>
> rename EO dep3
>
> rename RV dep4
>
>
>
> rename X indeA1
>
> rename Z indeB1
>
> rename A indeC1
>
> rename P indeD1
>
>
>
> gen indeA2 = indeA1
>
> gen indeA3 = indeA1
>
> gen indeA4 = indeA1
> etc.
>
> and then:
> reshape long dep indeA indeB indeC indeD, i(year) j(id)
>
> But clearly, there must be a better way of doing this... Any help
> appreciated.
> All best,
> Jan
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/