Announcement

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

  • forvalues egen rmean

    Hey,

    I have variables (wk1, wk2 etc.) which contain the number of trainings weekly. 110 weeks altogether
    I tried to creat 4weeks moving averages, but it doesn't want to work:

    forvalues i=1/107 {
    egen mmean`i'=rowmean(wk`i' wk`i+1' wk`i+2' wk`i+3')
    }

    It always gives me the value of wk`i', and not the average of the 4 consequtive years.

    Do you have any idea?

    Thank you

  • #2
    Code:
    `i+1'
    if it meant anything would be the local macro with name i+1 -- except that + is not allowed in Stata names.

    In fact, as you've found Stata just ignores the text such as +1 as trailing garbage, which is not obviously a feature.

    You want to do three things at once,

    get the macro
    increment it
    use the incremented value

    and you can do that, but you must do it differently, say by

    Code:
    local i1 = `i' + 1
    local i2 = `i' + 2
    local i3 = `i' + 3
    egen mmean`i' = rowmean(wk`i' wk`i1' wk`i2' wk`i3')
    or by using references such as

    Code:
    ``= `i' + 1''
    as exemplified by

    Code:
    . local i = 42
    
    . di `i' + 1
    43
    
    . local text42 "frog"
    
    . local text43 "toad"
    
    . di "`text`=`i' + 1''"
    toad
    All that said, your strategy is doomed to awkwardness. You have a wide layout and would be better off long. You have 110 variables and want to create another 107 variables, and so forth. How you would even plot or model any of these results? I strongly recommend reshape long, after which any running mean is just one more variable.

    Detail: The running mean of an even number of values is also often more awkward to work with than that for an odd number of values.
    Last edited by Nick Cox; 14 Jan 2024, 05:38.

    Comment


    • #3
      Thank you, it helped a lot. However, I have problems again.
      I tried this:

      local i1 = `i' + 1
      local i2 = `i' + 2
      local i3 = `i' + 3


      forvalues i=1/107 {
      egen mmean`i' = rowmean(wk`i' wk`i1' wk`i2' wk`i3')
      }

      And it works, but only for those cases which have ninmissing values in the wirst week. Maybe do you have an idea why and how yould I fox it?

      (As for your strategical advice, well, I will not use all these number, I will probably need the standard deviations and means only for the last few weeks, so they are only a step before the final variables.)

      Comment


      • #4
        The local macros i1 i2 i3 must be defined within the loop, not before it. You want their values to change.

        But even with your code -- although results will be wrong -- missing values in the first week are not an issue, as can be shown directly.

        Code:
        clear 
        input wk1 wk2 wk3 wk4 
        . 1 2 3 
        end 
        
        forval i = 1/1 {
            local i1 = `i' + 1 
            local i2 = `i' + 2 
            local i3 = `i' + 3 
            egen mean`i' = rowmean(wk`i' wk`i1' wk`i2' wk`i3')
        }
        
        list 
        
             +-------------------------------+
             | wk1   wk2   wk3   wk4   mean1 |
             |-------------------------------|
          1. |   .     1     2     3       2 |
             +-------------------------------+

        Comment

        Working...
        X