Dear members,
I'm trying to combine the command "statsby" in a loop to obtain a new dataset composed only by the R2 derived from equations done on this dataset
This is how my dataset looks like:
YEAR | VAR A | VAR B | VAR C
1 | z | y | d
2 | z | y | d
3 | z | y | d
4 | z | y | d
5 | z | y | d
6 | z | y | d
For each year and each VAR, I would like to run the following AR(2) equation: reg VARx L1.VARx L2.VARx and store the R2 of each equation to derive a new dataset that would look like:
YEAR | VAR A | VAR B | VAR C
1 | | |
2 | | |
3 | r2(y3a) | r2(y3b) | r2(y3c)
4 | r2(y4a) | r2(y4b) | r2(y4c)
5 | r2(y5a) | r2(y5b) | r2(y5c)
6 | r2(y6a) | r2(y6b) | r2(y6c)
I've therefore created created the following loop:
________________________
foreach var of varlist _all {
local vl = "`vl' `var'"
}
local exclude "year"
local vl: list vl - exclude // this is to remove year from the var list
foreach var of local vl {
preserve
keep year `var' // this is to consider only one variable per time
tsset year
levelsof year, local(levels)
foreach x of local levels {
statsby e(r2), by(year) clear: `var' L1.`var' L2.`var' if year== `x' // this should create the R2 for each year
}
restore
}
________________________
unfortunately it doesn't work. Could you please give me some advice?
many thanks,
Alessandro Giovannini
I'm trying to combine the command "statsby" in a loop to obtain a new dataset composed only by the R2 derived from equations done on this dataset
This is how my dataset looks like:
YEAR | VAR A | VAR B | VAR C
1 | z | y | d
2 | z | y | d
3 | z | y | d
4 | z | y | d
5 | z | y | d
6 | z | y | d
For each year and each VAR, I would like to run the following AR(2) equation: reg VARx L1.VARx L2.VARx and store the R2 of each equation to derive a new dataset that would look like:
YEAR | VAR A | VAR B | VAR C
1 | | |
2 | | |
3 | r2(y3a) | r2(y3b) | r2(y3c)
4 | r2(y4a) | r2(y4b) | r2(y4c)
5 | r2(y5a) | r2(y5b) | r2(y5c)
6 | r2(y6a) | r2(y6b) | r2(y6c)
I've therefore created created the following loop:
________________________
foreach var of varlist _all {
local vl = "`vl' `var'"
}
local exclude "year"
local vl: list vl - exclude // this is to remove year from the var list
foreach var of local vl {
preserve
keep year `var' // this is to consider only one variable per time
tsset year
levelsof year, local(levels)
foreach x of local levels {
statsby e(r2), by(year) clear: `var' L1.`var' L2.`var' if year== `x' // this should create the R2 for each year
}
restore
}
________________________
unfortunately it doesn't work. Could you please give me some advice?
many thanks,
Alessandro Giovannini
Comment