Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • repeating a smoothing function in a time series

    Hello
    I have a series of mortality data for 100 countries.

    I have (clumsily) coded the following for the first country:


    tssmooth ma Albania_5yrmortality_mean =yearly_mort_Albania, window(0 1 4)
    gen ismiss = missing(yearly_mort_Albania)
    replace Albania_5yrmean = . if ismiss > (4/5)
    drop ismiss



    is there a way to repeat this for all countries?

    many thanks
    Catherine


  • #2
    As you do not show example data, I'll illustrate it with StataCorp's online grunfeld.dta.

    Code:
    clear*
    
    webuse grunfeld
    
    capture program drop one_company
    program define one_company
    tsset year
    tssmooth ma company_mean_invest = invest, window(0 1 4)
    exit
    end
    
    runby one_company, by(company)clear*
    -runby- is written by Robert Picard and me, and is available from SSC.

    This code will get you the smoothed series for each company (or in your case country), and then you can do the rest from there with the same commands you already have. That said, I don't understand what you are doing with those other commands. The variable ismiss is always going to be 0 or 1, so comparing it to 4/5 seems odd. You can get the same result without even creating the intermediate ismiss variable. Again in the grunfeld data set, you just need
    Code:
    replace invest = . if missing(company_mean_invest)
    In the future, when asking for help with code, always show example data, and when showing data examples, please use the -dataex- command to do so. If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      I don't understand what is going on with missing values in #1 either, but tssmooth ma supports a panel structure:


      Code:
      . webuse grunfeld , clear
      
      . tsset
             panel variable:  company (strongly balanced)
              time variable:  year, 1935 to 1954
                      delta:  1 year
      
      . tssmooth ma wanted=invest, w(0 1 4)
      The smoother applied was
           by company : (1/5)*[1*x(t) + x(t+1) + x(t+2) + x(t+3) + x(t+4)]; x(t)=
           invest
      Any appearance to the contrary may be because your Stata is some versions old and doesn't support panel smoothing, in which case @Clyde Schechter's technique would be needed. runby is naturally a great command for when you need it.

      Comment

      Working...
      X