Bookmark and Share

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: number of dates in x axis


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: number of dates in x axis
Date   Wed, 25 May 2011 07:00:05 +0100

Thanks for the details.

By re-running bits of your code you are adding elements again and
again to the local -mydates-. It's like a bag and you keep putting
things into it. You would need to empty the bag first, by

local mydates

However, that turns to be unnecessary for other reasons.

You are declaring these dates to be monthly, but from the values they
can only be daily dates, as explained in my previous post. The fact
that they are spaced monthly is separate. Look again at the result of
your -xtset-: -xtset- is interpreting your data as months in the years
3459 to 3519, about which all that is written is as yet science
fiction.

> . xtset person date1,  monthly
>       panel variable:  person (unbalanced)
>        time variable:  date1, 3459m1 to 3519m11, but with gaps
>                delta:  1 month

Also, all this code does is ask for dates within 2009.

> . forval i = 4(2)11 {
>  2.         local thisdate = mdy(`i', 1, 2009)
>  3.         local mydates `mydates' `thisdate'
>  4.         }
>

However, the good news is that your data, evidently recorded once a
month, can be recast, at least for graphical purposes, as monthly
data.

gen month = mofd(date1)
xtset person month, monthly
su month, meanonly
xtline calories, overlay xlabel(`r(min)'(2)`r(max)', format(%tm_m_Y) ang(v))

That also gives an answer to your earlier question about automating
the calculation of the limits. -summarize- leaves in its wake r(min)
and r(max) and you feed their local macro personas to -xtline-. You
can of course tweak the format to some other %tm format.

The fact that you have monthly-spaced daily dates can now be seen as
tacit in your first post, but I didn't pick that up. I saw only values
like 17988 which were familiar as recent daily dates.

Nick

On Tue, May 24, 2011 at 8:34 PM, Maria Ana Vitorino
<[email protected]> wrote:
> Hi Nick,
> I really appreciate your help and patience.
> Here is the code and some descriptives that may help to find the bug...
> Thanks so much,
> Ana
>
> generate date1=date(date,"MY")
>
> format date1 %tm
> list date1 date in 1
>
>>    +------------------+
>>    |  date1      date |
>>    |------------------|
>>  1. | 3469m3   Aug2009 |
>>    +------------------+
>
> . egen newvar = axis(date1), label(date)
>
> . tab newvar
>
>      date1 |      Freq.     Percent        Cum.
> ------------+-----------------------------------
>    Apr2009 |        119        4.38        4.38
>    Aug2009 |        130        4.78        9.15
>    Sep2009 |        126        4.63       13.79
>    Oct2009 |        126        4.63       18.42
>    Nov2009 |        134        4.93       23.35
>    Dec2009 |        151        5.55       28.90
>    Jan2010 |        153        5.62       34.52
>    Feb2010 |        150        5.51       40.04
>    Mar2010 |        150        5.51       45.55
>    Apr2010 |        149        5.48       51.03
>    May2010 |        146        5.37       56.40
>    Jul2010 |        153        5.62       62.02
>    Aug2010 |        151        5.55       67.57
>    Sep2010 |        153        5.62       73.20
>    Oct2010 |        153        5.62       78.82
>    Dec2010 |        161        5.92       84.74
>    Feb2011 |        142        5.22       89.96
>    Mar2011 |        154        5.66       95.62
>    Apr2011 |        119        4.38      100.00
> ------------+-----------------------------------
>      Total |      2,720      100.00
>
>
> . sum date1
>
>    Variable |       Obs        Mean    Std. Dev.       Min        Max
> -------------+--------------------------------------------------------
>       date1 |      2720    18380.68    198.1158      17988      18718
>
>
> . xtset person date1,  monthly
>       panel variable:  person (unbalanced)
>        time variable:  date1, 3459m1 to 3519m11, but with gaps
>                delta:  1 month
>
> . forval i = 4(2)11 {
>  2.         local thisdate = mdy(`i', 1, 2009)
>  3.         local mydates `mydates' `thisdate'
>  4.         }
>
> .
> . display `thisdate'
> 18171
>
> . display `mydates'
> 17988180491811018171
>
> .
> .
> . local thisdate = mdy(4,1,2011)
>
> . local mydates `mydates' `thisdate'
>
> .
> . display `thisdate'
> 18718
>
> . display `mydates'
> 1798818049181101817118718
>
>
> . xtline calories, overlay xlabel(`mydates', format(%d_m_Y) ang(v))
> invalid label specifier, :  15341 15400 15461 15522 15584 15645 15645 17901
> 17960 18021 18082 18144 1820
>> 5 17988 18049 18110 18171 18232 . . . . 18718 15341 15400 15461 15522
>> 15584 15645 15706 15341 15400 15
>> 461 15522 15584 15645 15706 17988 18049 18110 18171 18718 18718 18628
>> 18628 18628 17898 17957 18018 18
>> 079 18141 18628 18628:
> r(198);
>
>
>
>
>
> On May 24, 2011, at 3:05 PM, Nick Cox wrote:
>
>> You are burdening me with questions that I can't answer! You are not
>> showing me the code that fails. Also, you are guessing again.
>>
>> But it seems that you have daily dates.
>>
>> Applying a %tm format to daily dates is legal but pointless. It gives you
>> a result that does not apply to your dates.
>>
>> The number 18110 is
>>
>> (D) 1 August 2009 as a daily date
>>
>> _but_
>>
>> (M) March 3469 as a monthly date, i.e. a date almost 1460 years into the
>> future. That's what
>>
>> But that is not going to fail.
>>
>> In my first post I gave an example of a format for daily dates that shows
>> month and year: %d_m_Y.
>>
>> I am going to try a third and last time. If you show me the code that
>> fails, I will try to tell you your mistake. Otherwise what you don't tell us
>> remains inscrutable.
>>
>> Nick
>> [email protected]
>>
>> Maria Ana Vitorino
>>
>> Thanks. My code is a little more complicated than the example so I
>> didn't want to burden you with it. But I understand your position.
>> I think the issue has to do with the formatting of the date
>> variable...I can't seem to get it to work.
>> This is what I'm getting:
>>
>> . generate date1=date(date,"MY")
>>
>> . format date1 %tm
>>
>> . list date1 date in 1
>>
>>    +------------------+
>>    |  date1      date |
>>    |------------------|
>>  1. | 3469m3   Aug2009 |
>>    +------------------+
>>
>> What am I doing wrong?
>>
>> Thanks,
>> Ana
>>
>>
>> On May 24, 2011, at 2:18 PM, Nick Cox wrote:
>>
>>> % sacrificial line.
>>>
>>> Nick
>>> [email protected]
>>>
>>>
>>> -----Original Message-----
>>> From: Nick Cox
>>> Sent: 24 May 2011 19:16
>>> To: '[email protected]'
>>> Subject: RE: st: number of dates in x axis
>>>
>>> Please: I want to see the _exact_ code you typed. No commentary, no
>>> paraphrase, no speculation. If you want to debug your code yourself,
>>> feel free to do so. If you want me to try to debug your code, I am
>>> happy to try, but that means your code, not your thoughts about what
>>> might be wrong with your code.
>>>
>>> Nick
>>> [email protected]
>>>
>>>
>>> -----Original Message-----
>>> From: [email protected]
>>> [mailto:[email protected]
>>> ] On Behalf Of Maria Ana Vitorino
>>> Sent: 24 May 2011 19:11
>>> To: [email protected]
>>> Subject: Re: st: number of dates in x axis
>>>
>>> I tried your code again (this time by pasting it into the do-file
>>> editor instead into the command window) and it worked. But my version
>>> is not working (I still get the invalid label specifier message).
>>> Could it be because my time variable has gaps? Also, how can one setup
>>> the "11" in your for loop without having to make mental calculations
>>> regarding the number of months?
>>> Thanks for the help!
>>> Ana
>>>
>>>
>>> On May 24, 2011, at 1:31 PM, Nick Cox wrote:
>>>
>>>> Yes. Show me the _exact_ code you typed.
>>>>
>>>> Nick
>>>> [email protected]
>>>>
>>>>
>>>> -----Original Message-----
>>>> From: [email protected]
>>>> [mailto:[email protected]
>>>> ] On Behalf Of Maria Ana Vitorino
>>>> Sent: 24 May 2011 18:29
>>>> To: [email protected]
>>>> Subject: Re: st: number of dates in x axis
>>>>
>>>> Thanks Nick.
>>>> I tried running your example but I got the error message "invalid
>>>> label specifier, :  15341 15400 15461 ..."
>>>> Any suggestions?
>>>> Thanks!
>>>> Ana
>>>>
>>>> On May 24, 2011, at 4:20 AM, Nick Cox wrote:
>>>>
>>>>> There is relevant material at
>>>>>
>>>>> Stata 7: How can I get "nice" date labels on a graph?
>>>>>
>>>>> http://www.stata.com/support/faqs/graphics/nicedate.html
>>>>>
>>>>> StataCorp have added this annotation:
>>>>>
>>>>> "Note: This FAQ is relevant for users of releases prior to Stata 8.
>>>>> For Stata 8 see update from 12 September 2003."
>>>>>
>>>>> In fact the techniques there do remain useful for some problems.
>>>>>
>>>>> Here's an example with different data. Here I decide that I want
>>>>> labels every 2 months from 1 January 2002 to 1 January 2003. I loop
>>>>> over the dates and put them in a bag, namely a local macro.
>>>>>
>>>>> sysuse xtline1
>>>>> xtset person day
>>>>>
>>>>> forval i = 1(2)11 {
>>>>>        local thisdate = mdy(`i', 1, 2002)
>>>>>        local mydates `mydates' `thisdate'
>>>>> }
>>>>>
>>>>> local thisdate = mdy(1,1,2003)
>>>>> local mydates `mydates' `thisdate'
>>>>>
>>>>> xtline calories, overlay xlabel(`mydates', format(%d_m_Y) ang(v))
>>>>>
>>>>> See also
>>>>>
>>>>> SJ-7-4  gr0030  . Stata tip 55: Better axis labeling for time
>>>>> points/
>>>>> intervals
>>>>>   . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
>>>>> N. J. Cox
>>>>>   Q4/07   SJ 7(4):590--592                                 (no
>>>>> commands)
>>>>>   tip on labeling graphs for time points and time intervals
>>>>>
>>>>>
>>>>> On Tue, May 24, 2011 at 4:45 AM, Maria Ana Vitorino
>>>>> <[email protected]> wrote:
>>>>>
>>>>>> I have the following data:
>>>>>>
>>>>>>
>>>>>> +------------------------+
>>>>>> | Person   Weight      Date    |
>>>>>> |------------------------|
>>>>>> 1. |  Name1       50      17988  |
>>>>>> 2. |  Name1       55      18110  |
>>>>>> 3. |  Name1       50      18141  |
>>>>>> 4. |  Name1       45      18171  |
>>>>>> 5. |    ...        .      . |
>>>>>> |------------------------|
>>>>>> 6. |  Name2       85      18110 |
>>>>>> 7. |  Name2       88      18171 |
>>>>>> 8. |  Name3       68      17988 |
>>>>>> 9. |    ...        .                       . |
>>>>>> 10. |     ..        .      .                 |
>>>>>> |------------------------|
>>>>>> 11. |  NameN        .               . |
>>>>>> +------------------------+
>>>>>>
>>>>>> The labels for the date variables are the months' names (plus year)
>>>>>> (e.g.
>>>>>> "jan2006", "feb2007", etc)
>>>>>>
>>>>>> If I do something like:
>>>>>>
>>>>>> xtline Weight , overlay  i(Person) t(Date) xlab(, valuelabel)
>>>>>>
>>>>>> The month names show up on the xaxis BUT only every 6 months or so.
>>>>>> How can
>>>>>> I control the number of "ticks" (AND month names ) that show up on
>>>>>> the
>>>>>> x-axis? (suppose I want a tick every 2 months or even every month)
>>>>>>

*
*   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index