Search code examples
statadatetime-formattrendline

Stata: tsline graphs values not at the right date


I want to depict the evolution of a variable called share over time. I do so by using tsline, but the resulting graph looks off: Although my data starts in May 1989 and ends in December 1993, the trendline is drawn so that it begins in January 1989 and ends in mid-1993.

gen double time3 = monthly(time2, "YM")
format time3 %tm
tsset time3
tsline share, ///
    ttitle("years") ytitle("") ylabel(0(.2).65) ///
    tlabel(1989m5(12)1994m5, format(%tmY) labsize(small))

I know that Stata stores dates as integers and tried replacing the year-month-indications after tlabel by integers. Since the time variable is defined as months since 1960m1, 1989m5 is stored internally as 352 and 1993m12 as 407. I learned this by running dis tm(1989m5). But even with tlabel(352(12)407), the trendline is not drawn correctly. Has anyone an idea about how to fix this? This is the how the graph looks like by now.

This is a subsample of the data that I used:

[CODE]
* Example generated by -dataex-. To install: ssc install dataex
clear
input str7 time2 double(time3 share)
"1989-05" 352  .1536926147704591
"1989-06" 353  .1665024630541872
"1989-08" 355 .12674650698602793
"1989-09" 356 .18095712861415753
"1989-10" 357 .24629080118694363
"1989-11" 358 .23008849557522124
"1989-12" 359 .17638036809815952
"1990-01" 360 .20521653543307086
"1990-02" 361  .1754473161033797
"1990-03" 362 .17401960784313725
"1990-04" 363 .14173998044965788
"1990-05" 364  .1669970267591675
"1990-06" 365  .1398838334946757
"1990-08" 367 .10461689587426326
"1990-09" 368 .14965312190287414
"1990-10" 369  .1921182266009852
"1990-11" 370 .18038617886178862
"1990-12" 371 .19577735124760076
"1991-01" 372 .10562685093780849
"1991-02" 373 .09596928982725528
"1991-03" 374  .1941747572815534
"1991-04" 375  .1889106967615309
"1991-05" 376  .1794234592445328
"1991-06" 377  .1968390804597701
"1991-08" 379 .17846309403437816
"1991-09" 380 .19425173439048563
"1991-10" 381 .14556962025316456
"1991-11" 382 .15569143932267168
"1991-12" 383  .1694015444015444
"1992-01" 384 .20812928501469147
"1992-02" 385   .257590597453477
"1992-03" 386  .2204724409448819
"1992-04" 387 .22096456692913385
"1992-05" 388 .21601941747572814
"1992-06" 389  .1675025075225677
"1992-07" 390 .22176591375770022
"1992-09" 392 .15128968253968253
"1992-10" 393 .15841584158415842
"1992-11" 394  .1849112426035503
"1992-12" 395 .19642857142857142
"1993-01" 396 .22469252601702933
"1993-02" 397  .2796528447444552
"1993-03" 398   .290811339198436
"1993-04" 399 .24108910891089108
"1993-05" 400  .2562437562437562
"1993-06" 401 .22127872127872128
"1993-07" 402 .27874743326488705
"1993-09" 404  .3391472868217054
"1993-10" 405  .3840155945419103
"1993-11" 406 .45184824902723736
"1993-12" 407 .43987975951903807
end
format %tm time3
[/CODE]

Solution

  • The graph you've posted doesn't seem surprising.

    Using the data and code you've posted

    clear
    input str7 time2 double(time3 share)
    "1989-05" 352  .1536926147704591
    "1989-06" 353  .1665024630541872
    "1989-08" 355 .12674650698602793
    "1989-09" 356 .18095712861415753
    "2019-10" 717 .13052208835341367
    "2019-11" 718 .13559059987631417
    "2019-12" 719 .13997555012224938
    end
    format %tm time3
    
    tsset time3
    tsline share, ///
        ttitle("years") ytitle("") ylabel(0(.2).65) ///
        tlabel(1989m5(12)2019m12, format(%tmY) labsize(small))
    

    it's hard to see what might be a problem.

    tsline doesn't purport to draw a trend line, just a line graph for the data specified.