Parseltongue
Parseltongue

Reputation: 11697

Resolving gaps in the data in Stata with Weekly Time Series Data

I have weekly Google Trends Search query data in Stata. Here is a sample of what the data looks like:

enter image description here

I converted the date string into a date object like so:

gen date2 = date(date, "YMD")
gen year= year(date2)
gen w = week(date2)
gen weekly = yw(year,w)
format weekly %tw

I now want to declare "date2" as my time series reference, so I did the following:

tsset date2, weekly

However, upon using tsreport I get the following information

enter image description here

However, I should have no gaps in the data, as it is weekly. For some reason, Stata is still assuming I have daily data.

I cannot take first differences on any of these variables because of this issue. How do I resolve this?

Upvotes: 0

Views: 3083

Answers (2)

Nick Cox
Nick Cox

Reputation: 37208

I agree with William Lisowski's general advice but have different specific recommendations.

You have weekly data with daily flags for each week.

Stata weeks are likely to be of little or no use to you for reasons documented in detail in references that

search week, sj 

will disclose. Specifically,

SJ-12-4 dm0065_1  . . . . . Stata tip 111: More on working with weeks, erratum
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q4/12   SJ 12(4):765                                     (no commands)
        lists previously omitted key reference

SJ-12-3 dm0065  . . . . . . . . . .  Stata tip 111: More on working with weeks
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q3/12   SJ 12(3):565--569                                (no commands)
        discusses how to convert data presented in yearly and weekly
        form to daily dates and how to aggregate such data to months
        or longer intervals

SJ-10-4 dm0052  . . . . . . . . . . . . . . . . Stata tip 68: Week assumptions
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q4/10   SJ 10(4):682--685                                (no commands)
        tip on Stata's solution for weeks and on how to set up
        your own alternatives given different definitions of the
        week

Issuing that search command will give you links to .pdf copies of each paper.

I suggest simply

gen date2 = daily(date, "YMD")
format date2 %td
tsset date2, delta(7) 

daily() is the same function as date() but I think the name is a better signal to all of precisely what it does. The more important detail is that delta(7) is sufficient to indicate daily data spaced 7 days apart, which is precisely what you have.

To expand on the problem you had: when you converted to daily dates, then you got a numeric variable with values like 18755 in steps of 7 to your last date. You then told Stata through your tsset ..., weekly that these are really weeks. Stata uses an origin for all dates like these of the beginning of 1960. So, Stata is working out what 18755 weeks (etc.) from the beginning of 1960 would be. And your numeric variable is still in steps of 7. So, the reason that Stata is misinterpreting your data is that you gave it incorrect information. tsset will never change a date variable; it just interprets it as you instruct.

Note also that you created a weekly date variable but then did not use it. That wouldn't have been a good solution either, but it would have been closer to what you want. It appears that all your dates are Sundays, so in some years there would be 53 and in other years 52; that's not true of Stata's own weeks.

Upvotes: 1

user4690969
user4690969

Reputation:

The problem would be more helpfully stated if it included a listing of the data, rather than a picture, so that others could test and demonstrate correct code.

With that said, you need to carefully review the output help datetime to improve your understanding of how to work with Stata Internal Format (SIF) date and time data, and of the meaning of a "weekly date" in Stata. I believe that something like the following will start you along the correct path.

gen date2 = date(date, "YMD")
gen weekly = wofd(date2)
format weekly %tw

or in a one fewer steps

gen weekly = wofd(date(date, "YMD"))
format weekly %tw

Upvotes: 0

Related Questions