Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: plot confidence intervals with time series

From   annoporci <>
To   "" <>
Subject   st: plot confidence intervals with time series
Date   Sat, 22 Dec 2012 17:13:28 +0800

Dear statalist,

A very simple question: I want to plot confidence intervals onto a time series variable.

The problem I have is that in some cases the dates display as dates while in other cases they display as numbers. I would like the dates to display as dates.

I used rates.dta as an example, from the web data repository, even though it doesn't produce interesting confidence intervals. Feel free to use another dataset for illustration.

The following is my test code. Attempt 1 is what I understand to be the natural approach: the graph has problems with the dates in this case. Attempt 2 is my attempt to do things "by hand": the dates display as expected in this case.

Follow-up question 1: is there a better way to plot confidence intervals with a flexible value of the significance level, than my second attempt below?

Follow-up question 2: can I plot the confidence intervals for the logarithm of the variable "on the fly" or do I need to define a new variable equal to the log of the level variable?

Follow-up question 3: does stata provide a confidence intervals command under non-normal assumptions (e.g. Pareto)? or do I need to do it by hand along the lines of my second attempt below?

I suspect my questions are FAQ so please do feel free to simply direct me to some relevant url.

many thanks.

 cap log close
 set more off
 webuse rates.dta

 global x1 dow
 format date %td
 tsset date
 global t date

 /* Construct a 95 percent confidence interval, attempt 1 */
 graph twoway (lfitci $x1 $t) ///
              (scatter $x1 $t) ///
			  , title("95% Confidence Interval (dates wrong!)")
 graph export,replace

 /* Construct a 95 percent confidence interval, attempt 2 */
 reg $x1 $t, robust
 predict yhat, xb
 predict shat, stdp
 scalar define alpha = 0.9 /*significance level*/
 gen crit = invttail(e(df_r),alpha/2)
 gen upper = yhat + crit*shat
 gen lower = yhat - crit*shat
 graph twoway (scatter $x1 $t) ///
              (line yhat $t) ///
			  (line lower $t) ///
			  (line upper date) ///
              , title("Confidence Interval, significance level = `=alpha'")
 graph export,replace

*   For searches and help try:

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