Announcement

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

  • Calculating the difference between two paired observations ignoring missing values

    Hi,

    I'm sure there's a simple way to do this but I can't seem to find a solution.

    I have two variables, one containing both the start and peak values of a signal (stored in start) and a second variable indicating where there's a pair (i.e. the start (location == 1) and the peak (location == 2) of each signal increase (sometimes there's no peak)).

    I want to create a new variable to store the difference between each pair (i.e. the amplitude of the signal: peak – start for each pair and store this at location == 2 in the new variable). I'm not sure how to loop back over the missing values and unpaired 1s from the peak value at location == 2 to the start value at location == 1 for each pair. Can someone help please? :-S

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(start location)
    3.7167106 1
            . .
    3.9673915 2
            . .
            . .
            . .
     3.625683 1
            . .
     3.852861 2
            . .
            . .
     3.636759 1
            . .
            . .
     3.811554 2
            . .
            . .
            . .
            . .
            . .
            . .
            . .
     3.471214 1
            . .
            . .
            . .
            . .
            . .
            . .
            . .
            . .
     3.317838 1
            . .
            . .
            . .
            . .
            . .
      3.26407 1
            . .
     3.495139 2
            . .
      3.29939 1
            . .
            . .
            . .
            . .
            . .
            . .
            . .
            . .
            . .
            . .
     3.233487 1
            . .
            . .
            . .
            . .
            . .
     3.188093 1
            . .
            . .
            . .
            . .
            . .
            . .
            . .
            . .
            . .
            . .
            . .
    3.0768125 1
            . .
            . .
            . .
            . .
    3.2485664 2
            . .
            . .
            . .
            . .
            . .
            . .
     3.096682 1
            . .
            . .
            . .
            . .
            . .
            . .
            . .
            . .
            . .


  • #2
    This may help:

    Code:
    gen locn = location
    replace locn = location[_n-1] if missing(locn) 
    gen prev = start[_n-1] 
    replace prev = prev[_n-1] if missing(prev) 
    gen locn_prev = locn[_n-1] 
    gen change = start - prev if abs(locn - locn_prev) == 1 
    . 
    . list if change < . 
    
         +--------------------------------------------------------------+
         |    start   location   locn       prev   locn_p~v      change |
         |--------------------------------------------------------------|
      3. | 3.967391          2      2   3.716711          1    .2506809 |
      9. | 3.852861          2      2   3.625683          1    .2271779 |
     40. | 3.495139          2      2    3.26407          1    .2310688 |
     42. |  3.29939          1      1   3.495139          2   -.1957488 |
         +--------------------------------------------------------------+

    Comment

    Working...
    X