Announcement

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

  • Forward demeaned variable




    Hi all,

    I am trying to construct a moving forward demeaned variable and below is general information.




    Attached is an example of the panel dataset.

    I've tried:

    tssmooth ma wanted = var, window(0 1 ??)

    However, it's tricky since ?? varies for each id.

    Any comments would be helpful.




    Click image for larger version

Name:	Untitled.png
Views:	1
Size:	27.6 KB
ID:	1745813

    Attached Files
    Last edited by John Hauger; 06 Mar 2024, 20:15.

  • #2
    Please read the Forum FAQ for excellent guidance on how to maximize your chances of getting a timely and helpful response on this Forum. Among the things you will learn there are that attachments are discouraged. The preferred way to show example data is with the -dataex- command. If you are running version 18, 17, 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.

    Also, the formulas you show are inconsistent. The "generic" formula you give shows that the mean to be subtracted from the t-1'th value of X starts with the t'th value of X and extends up to the end of the series (i.e. at 12). Now, for the illustrations for Xi,4 and Xi,8 have the means calculated starting from Xi,4 and Xi,8, respectively, and ending at 11. But if we apply the generic formula, those means should be calculated starting from Xi,5 and Xi,9. So you need to figure out whether our examples are correct or the generic formula is, because they contradict each other.

    That said, I have made a demonstration data set that shows how you can do this. You will need to adapt the code to the actual names of your variables.
    Code:
    clear*
    
    set obs 3
    gen i = _n
    expand 12
    by i, sort: gen t = _n
    
    set seed 1234
    gen x = runiform()
    
    //    CALCULATION OF "DEMEANED" VARIABLE HERE
    rangestat (mean) x, by(i) interval(t 1 .)
    gen demeaned = x - x_mean
    This code assumes that the generic formula is correct. If, in fact, the examples you gave for Xi,4 and Xi,8 are correct, then it gets a little more complicated--post back for the changes.

    -rangestat- is written by Robert Picard, Nick Cox, and Roberto Ferrer. It is available from SSC.

    Comment

    Working...
    X