I want to generate a variable recursively with certain conditions. Let's call the variable x. x is generated differently under different conditions.
- x_t=L1.x_t^a + b/2 if L1.x_t^a+b<c and L1.x_t > d
- x_t=1+0.5x_t^a if L1.x_t^a+b>c and L1.x_t > d
- x_t=L1.x_t^a if L1.x_t < d
I know how to implement this in R but not sure how I go about doing this in Stata. I have tried the following 2 pieces of code but none worked.
gen x = 1
forvalues i = 2/_N {
scalar temp = x[`i'-1]^a + b
replace x = x[`i'-1]^a + b/2 if temp < c & x[`i'-1] > d
replace x = 1 + x[`i']/2 if temp > c & x[`i'-1] > d
replace x = x[`i'-1]^a if x[`i'-1] < d
}
gen x = 1
gen temp = L.x^a + b
replace x = L.x^a + b/2 if L.x > d & _n > 1 & temp < c
replace x = 1 + L.x^a/2 if temp > c & _n > 1 & L.x > d
replace x = L.x^a if L.x < d & _n > 1
The first piece of code gives an error invalid syntax
. The second piece of code didn't deliver what I thought it would. I know where the problem lies, that is temp
should be replaced after every call of x
, but I don't know how to implement this.