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

Identifying observations within a rolling window with further conditions - minimum increase threshold

$
0
0
Dear Community,
As I am a greenhorn in Stata, I hope you can help me out with the following. I use Stata 12 on a Windows 10 computer. The dataset is a panel dataset with 30+ companies over a multi-year period (5 years). It might be important to mention, that each company can have multiple observations in the same second; however each company also can have not one single observation for several months. (I did not set the dataset as a panel data set, as this did not work due to multiple observations at the same time for certain companies).

My intention is to mark observations of a panel data set that lie in between a specific absulute value increase of a specific variable over a specific rolling time period/window for a specific company.

So basically it´s about identifying peaks: periods that show a strong increase of one variable value within a given time period.

My preferred methodology would be like this:
  1. Look at rolling x-days windows...
  2. …and identify the maximum and minimum within this rolling window.
  3. If the absolute difference between the maximum and minimum is equal or larger than threshold x...
  4. …set a dummy_variable=1 for all observations that lie in between (time perspective) the minimum and maximum observation and also include further relevant observations at the same day of the minimum and maximum observation.
  5. If possible the window moves forward on the shortest possible basis. However a daily moving window would be fine, as it might need less calculation time.
  6. Dummy variables have to be calculated for each company individually.
In uploaded a example picture which marks all observations with red and green that should be have a dummy_variable=1.
Array


Below I also included a structure of my dataset. However the real dataset includes observations for several companies!

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input str36 CompanyID double time float Ratio
"Company_1" 1551191502000     .9409
"Company_1" 1551263205000     .8935
"Company_1" 1551263205000     .8935
"Company_1" 1551264008000     .8935
"Company_1" 1551264007000     .8935
"Company_1" 1551264209000     .8935
"Company_1" 1551264209000     .8935
"Company_1" 1551350618000     .9409
"Company_1" 1551350618000     .9409
"Company_1" 1551350895000     .9409
"Company_1" 1551350895000     .9409
"Company_1" 1551351179000     .9409
"Company_1" 1551351179000     .9409
"Company_1" 1551358929000     .9409
"Company_1" 1551358928000     .9409
"Company_1" 1551362331000     .9409
"Company_1" 1551362666000     .9409
"Company_1" 1551607702000 1.0198001
"Company_1" 1551608391000 1.0198001
"Company_1" 1551614962000 1.0198001
"Company_1" 1551614962000 1.0198001
"Company_1" 1551694001000    1.0345
"Company_1" 1551694001000    1.0345
"Company_1" 1551695534000    1.0345
"Company_1" 1551695534000    1.0345
"Company_1" 1551696853000    1.0345
"Company_1" 1551696853000    1.0345
"Company_1" 1551791952000     .9874
"Company_1" 1551791952000     .9874
"Company_1" 1551867074000 1.1290001
"Company_1" 1551867073000 1.1290001
"Company_1" 1551867299000 1.1290001
"Company_1" 1551867299000 1.1290001
"Company_1" 1551954787000    1.1287
"Company_1" 1551954875000    1.1287
"Company_1" 1551955159000    1.1287
"Company_1" 1552212120000    1.1285
"Company_1" 1552212276000    1.1285
"Company_1" 1552217153000    1.1285
"Company_1" 1552217059000    1.1285
"Company_1" 1552217153000    1.1285
"Company_1" 1552232444000    1.1285
"Company_1" 1552297735000 1.1755999
"Company_1" 1552298051000 1.1755999
"Company_1" 1552298552000 1.1755999
"Company_1" 1552318978000 1.1755999
"Company_1" 1552387492000    1.1757
"Company_1" 1552472652000    1.2939
"Company_1" 1552474844000    1.2939
"Company_1" 1552474843000    1.2939
"Company_1" 1552474844000    1.2939
"Company_1" 1552475163000    1.2939
"Company_1" 1552558072000    1.3174
"Company_1" 1552564328000    1.3174
"Company_1" 1552817103000    1.2494
"Company_1" 1552817276000    1.2494
"Company_1" 1552817515000    1.2494
"Company_1" 1552817862000    1.2494
"Company_1" 1552826589000    1.2494
"Company_1" 1552826589000    1.2494
"Company_1" 1552826644000    1.2494
"Company_1" 1552826644000    1.2494
"Company_1" 1552826926000    1.2494
"Company_1" 1552827022000    1.2494
"Company_1" 1552837923000    1.2494
"Company_1" 1552903166000    1.3457
"Company_1" 1552903370000    1.3457
"Company_1" 1552903369000    1.3457
"Company_1" 1552903681000    1.3457
"Company_1" 1552903681000    1.3457
"Company_1" 1552903966000    1.3457
"Company_1" 1552903966000    1.3457
"Company_1" 1552905847000    1.3457
"Company_1" 1552905846000    1.3457
"Company_1" 1552905846000    1.3457
"Company_1" 1552905846000    1.3457
"Company_1" 1552910050000    1.3053
"Company_1" 1552910175000    1.3053
"Company_1" 1552910582000    1.3053
"Company_1" 1552915864000    1.3457
"Company_1" 1552986683000    1.3997
"Company_1" 1552986851000    1.3997
"Company_1" 1552986851000    1.3997
"Company_1" 1552992306000    1.3997
"Company_1" 1552994397000    1.3997
"Company_1" 1552994396000    1.3997
"Company_1" 1552994397000    1.3997
"Company_1" 1553075883000    1.3053
"Company_1" 1553079281000    1.3053
"Company_1" 1553079281000    1.3053
"Company_1" 1553079281000    1.3053
"Company_1" 1553166721000    1.2823
"Company_1" 1553166884000    1.2823
"Company_1" 1553167016000    1.2823
"Company_1" 1553167160000    1.2823
"Company_1" 1553167160000    1.2823
"Company_1" 1553421702000    1.2512
"Company_1" 1553435166000    1.2512
"Company_1" 1553505928000    1.1889
"Company_1" 1553505928000    1.1889
"Company_1" 1553506077000    1.1889
"Company_1" 1553511875000    1.1889
"Company_1" 1553511875000    1.1889
"Company_1" 1553511875000    1.1889
"Company_1" 1553519526000    1.1889
"Company_1" 1553590588000    1.2768
"Company_1" 1553590834000    1.2768
"Company_1" 1553590834000    1.2768
"Company_1" 1553599378000    1.2768
"Company_1" 1553599377000    1.2768
"Company_1" 1553599377000    1.2768
"Company_1" 1553685683000     1.209
"Company_1" 1553685683000     1.209
end
format %tc tradetime

Thanks a lot.

Viewing all articles
Browse latest Browse all 65160

Trending Articles



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