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]

st: number included in smooths when values are missing


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   st: number included in smooths when values are missing
Date   Sat, 6 Jul 2013 14:04:01 +0100

I've seen various posts both here and elsewhere on recording how many
non-missing values were actually used by -tssmooth- in calculating
moving averages.

One technique is best illustrated by example:

webuse grunfeld, clear
set seed 2803
gen invest_m = cond(runiform() < 0.2, ., invest)
tssmooth ma invest_s=invest_m , window(5)
* next command is just one line
by company: gen npresent = sum(!missing(L1.invest_m)) -
sum(!missing(L6.invest_m))
* previous command was just one line
edit

Here the moving average is just the average of the previous 5 values.
So, we want to calculate how many of those values were non-missing.

Taking this step by step

1. -!missing()- negates the function -missing()- so -!missing()- is 1
whenever something is non-missing and 0 when it is missing.

2. -sum()- gives the cumulative sum.

3. One way to get the count of something in a window  is a difference

number so far at end of window - number so far at beginning of window

In this example, the operator L1. gives the end of the window and the
operator L6. gives the beginning of the window (check 6 - 1 = 5, the
length of the window).

In general, the operator F. will also often be useful.

I learned this trick from Michael Blasnik on this list. It may seem
indirect -- indeed it is indirect -- but it often saves you from
writing out several terms, which is tedious and error-prone.

4. Using L. and F. operators matches the way -tssmooth- works.

5. Doing this panelwise, as in this example, is achieved by using
-by:-. If there is no panel structure, the command is simpler.

Another technique is just to form the indicator

gen ismiss = !missing(myvar)

and run that through -tssmooth-.

Nick
[email protected]
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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