Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: st: How to convert Daily data with gaps to weekly?
From
Nick Cox <[email protected]>
To
"'[email protected]'" <[email protected]>
Subject
RE: st: How to convert Daily data with gaps to weekly?
Date
Thu, 8 Mar 2012 12:46:58 +0000
It is vital to realise that Stata's -week()- function, etc. and also its week display format do _not_ relate exactly to the method here and will just lead to messes. This should be clear from careful study of my Stata Tip cited in emails cited here.
I think there is one (and quite possibly only one) to proceed here, to map weeks to successive integers and -tsset- in terms of that counter.
Note that -egen, group()-, suggested earlier in the thread, is _dangerous_ here as it cannot be smart about weeks for which there are no data at all.
The following indicates some technique. -labmask- must be installed from the SJ site.
clear
set obs 366
gen time = mdy(12,31,2011) + _n
format time %tdd_n_CY
l in 1
drop if runiform() > 0.9
gen sunday = time - dow(time)
gen s_sunday = string(sunday, "%dd_n_CY")
gen week = floor(sunday/7)
labmask week, values(s_sunday)
tab week
tsset week
Nick
[email protected]
Joao Ricardo F. Lima
Hi Nick,
thanks a lot for the answer. The use of -dow()- and -collapse- was
what I was looking for. With this solved, another problem appeared.
How to -tsset- the data? I cant just -format- and -tsset- the variable
"sunday" how weekly, as you can see below:
********************begin example******************
clear
input str11 date price_guava
7-Oct-2004 9.00
10-Oct-2004 8.00
12-Oct-2004 8.00
14-Oct-2004 7.00
17-Oct-2004 6.00
19-Oct-2004 6.00
21-Oct-2004 7.00
24-Oct-2004 7.00
26-Oct-2004 8.00
28-Oct-2004 9.00
31-Oct-2004 9.00
2-Nov-2004 9.00
4-Nov-2004 10.00
9-Nov-2004 10.00
11-Nov-2004 10.00
14-Nov-2004 10.00
16-Nov-2004 9.00
18-Nov-2004 8.00
21-Nov-2004 8.00
23-Nov-2004 7.00
25-Nov-2004 7.00
28-Nov-2004 7.00
30-Nov-2004 7.00
end
gen eventtime=date(date, "DMY")
gen sunday=eventtime-dow(eventtime)
collapse price_guava, by(sunday)
format sunday %tw
tsset sunday, w
******************************end of example********************
. tsset sunday, w
time variable: sunday, 2274w20 to 2275w24, but with gaps
delta: 1 week
I found a solution, however, not ellegant, but it runs. After the
-collapse- line:
************
local year=year(sunday[1])
local week=week(sunday[1])
gen time=w(`year'w`week')+_n-1
format time %tw
tsset time, w
***************
Best Regards,
Joao Lima
Em 7 de março de 2012 06:01, Nick Cox <[email protected]> escreveu:
> Another way is to aggregate into weeks using date functions. The
> easiest way I know to do this is to classify weeks by their starting
> Sundays. -dow()- returns 0 for Sundays
>
> . di dow(mdy(3,4,2012))
> 0
>
> and so
>
> gen dailydate = date(date, "DMY")
> gen Sundays = dailydate - dow(dailydate)
>
> collapse ... , by(Sundays)
>
> The last presumes that what you in mind is something like averaging in
> each week.
>
> Weeks have puzzled some users of Stata, even to the accusation of that
> the implementation is buggy in
>
> http://www.stata.com/statalist/archive/2012-02/msg01023.html
>
> For the good news, see
>
> http://www.stata.com/statalist/archive/2012-02/msg01027.html
>
> http://www.stata.com/statalist/archive/2012-02/msg01030.html
>
> and in turn the references they give, and so on.
>
> Nick
>
> On Wed, Mar 7, 2012 at 3:10 AM, William Pratt <[email protected]> wrote:
>> It depends on what your goal is. You can use tsfill to create weekend
>> values, though you will have no data for those days. The other method
>> is to use the group function within the egen command. This will create
>> a value that you can use to ts or xt set your data, as well as
>> generation lag variables.
>>
>> tsfill
>>
>> egen (new var) = group ( )
>>
>> On Tue, Mar 6, 2012 at 6:43 PM, Joao Ricardo F. Lima
>> <[email protected]> wrote:
>>> Dear Statalisters,
>>>
>>> I'm working with a time series of guava price like this:
>>>
>>> input str11 date preco_goi
>>> 7-Oct-2004 9.00
>>> 10-Oct-2004 8.00
>>> 12-Oct-2004 8.00
>>> 14-Oct-2004 7.00
>>> 17-Oct-2004 6.00
>>> 19-Oct-2004 6.00
>>> 21-Oct-2004 7.00
>>> 24-Oct-2004 7.00
>>> 26-Oct-2004 8.00
>>> 28-Oct-2004 9.00
>>> 31-Oct-2004 9.00
>>> 2-Nov-2004 9.00
>>> 4-Nov-2004 10.00
>>> 9-Nov-2004 10.00
>>> 11-Nov-2004 10.00
>>> 14-Nov-2004 10.00
>>> 16-Nov-2004 9.00
>>> 18-Nov-2004 8.00
>>> 21-Nov-2004 8.00
>>> 23-Nov-2004 7.00
>>> 25-Nov-2004 7.00
>>> 28-Nov-2004 7.00
>>> 30-Nov-2004 7.00
>>> end
>>>
>>> The time series is daily but with gaps. Thus, I can´t use the -dfgls-
>>> command, for example and others TS commands.
>>>
>>> What can I do in this case? Can I convert the daily data with gaps to
>>> weekly? Is there a better procedure to do the trick?
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/