Quantcast
Channel: Statalist
Viewing all articles
Browse latest Browse all 65044

Recognizing spells of negative and positive values

$
0
0
Hello,

I'm having trouble identifying spells in my dataset, which is set as a timeseries. The goal is to identify observations that have had >= 3 negative value changes, followed by one positive value change (so I'm not interested in those that have had 2 negative value changes and then a positive, or those that have had 3 negative changes and then no change).

My dataset looks like this:

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input double date int ID byte value double difference byte spell
1.7758656e+12 1234 12                   . .
 1.775952e+12 1234 11 -.08333333333333337 0
1.7760384e+12 1234 10 -.09090909090909094 0
1.7761248e+12 1234  9 -.09999999999999998 0
1.7762112e+12 1234  8 -.11111111111111116 0
1.7762976e+12 1234  8                   0 0
 1.776384e+12 1234  7               -.125 1
1.7764704e+12 1234  6  -.1428571428571429 1
1.7765568e+12 1234  5 -.16666666666666663 1
1.7766432e+12 1234  6  .19999999999999996 1
1.7767296e+12 1234  5 -.16666666666666663 0
 1.776816e+12 1234  4 -.19999999999999996 0
1.7769024e+12 1234 10                 1.5 0
end
format %tc date
Only the 7th-10th observation would be in a spell here, since they are (-)(-)(-)(+)

I've tried the following:
Code:
tsegen value = rall(L(0/2).difference,3) , c(@ <0)
but this only gives me the consecutive negative spells, doesn't account for the fact that I need at least 3 negative changes (and not just 3) and doesn't account for the fact that I need a positive value change to end my spell

Code:
gen byte begin = (difference<0) & (difference[_n-1] >= 0)
gen spell = cond(difference<0|(difference[_n-1]<0&difference!=0), sum(begin), 0)
which identifies the spells that are negative & followed by a positive, but doesn't leave those out that are only negative (ie not followed by a positive). I have the same issue when using tsspell.

Thank you in advance for your help!

Viewing all articles
Browse latest Browse all 65044

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>