gen new_date_variable=date(date_variable ,"YMD")
format date_new %td
or
gen new_date_variable=date(date_variable ,"DMY")
format date_new %tw
%tc Daytime 01jan1960 00:00:00
%td Daily 01jan1960
%tw Weekly 1960w1
%tm Monthly 1960m1
%tq Quarterly 1960q1
%ty yearly 0
## how to use if conditions with dates
>gen season=.
>replace season=1 if Injury_Date >= date("01012015","DMY") & Injury_Date<= date("21032015","DMY") | Injury_Date >= date("21122015","DMY") & Injury_Date<= date("21032016","DMY") | Injury_Date >= date("21122016","DMY") & Injury_Date<= date("21032017","DMY") | Injury_Date >= date("21122017","DMY")
>replace season=2 if Injury_Date >= date("21032015","DMY") & Injury_Date<= date("21062015","DMY") | Injury_Date >= date("21032016","DMY") & Injury_Date<= date("21062016","DMY") | Injury_Date >= date("21032017","DMY") & Injury_Date<= date("21062017","DMY")
>replace season=3 if Injury_Date >= date("21062015","DMY") & Injury_Date<= date("21092015","DMY") | Injury_Date >= date("21062016","DMY") & Injury_Date<= date("21092016","DMY") | Injury_Date >= date("21062017","DMY") & Injury_Date<= date("21092017","DMY")
>replace season=4 if Injury_Date >= date("21092015","DMY") & Injury_Date<= date("21122015","DMY") | Injury_Date >= date("21092016","DMY") & Injury_Date<= date("21122016","DMY") | Injury_Date >= date("21092017","DMY") & Injury_Date<= date("21122017","DMY")
label define season 1 winter 2 spring 3 summer 4 fall
label values season season
How to model using dates.
Assuming we want to fit a fourth order auto-regressive model:
Yt=μ+β1yt-1+ β2yt-2
+ β3yt-3 + β4yt-4 + ε
There are two ways to go about using lag variables:
gen y1 = y[_n-1]
gen y2 = y[_n-2]
gen y3 = y[_n-3]
gen y4 = y[_n-4]
regress y y1 y2 y3 y4
After setting the data into ts mode we can use the L operator to indicate lag variables
regress y L.y L2.y L3.y L4.y
or
regress L(0/4).y
The lead operator F is the inverse of L. Where F.y indicates yt+1.
The lead operator D indicates the arithmetical difference of adjacent observations Where D.y indicates Δ≡yt-yt-1
and
D2.y indicates Δ≡yt-yt-2
No comments:
Post a Comment