Announcement

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

  • Using preserve and restore within a loop

    I’ve been trying to run the runtest command on a measured variable (which I call mv) which is split by site. I’ve been trying to run it on groups of observations within each site. The whole lot are numbered 1 to 5894, but not all numbers are used, and I used 1100 just as an example.

    I couldn’t get the by option to work with the command, while trying preserve and restore thus:

    sort site id

    forvalues i = 1/1100{

    preserve

    keep if site==`i'

    display "site", "`i'"

    runtest mv

    restore

    }

    gave me an error message:

    . forvalues i = 1/1100{

    2. preserve

    3. keep if site==`i'

    4. display "site", "`i'"

    5. runtest mv

    6. restore

    7. }

    (5,691 observations deleted)

    site 1

    N(mv <= 2.25) = 25

    N(mv > 2.25) = 25

    obs = 50

    N(runs) = 29

    z = .86

    Prob>|z| = .39

    (5,741 observations deleted)

    site 2

    no observations

    r(2000);


    I’m wondering why the restore command seems not to work.

    Any ideas?

  • #2
    Before other questions can be answered, the sorting here is puzzling. Normally with runtest there is some order that is important. But is it that of id within site?

    Assuming it is, I don't see that you need preserve and restore at all. Although runtest doesn't support if it does support in.

    Necessarily I haven't tested this code. I have assumed that there is nothing to test unless there are 2 or more observations, but even 2 is an absurd lower limit, I guess.

    Code:
    sort site id
    gen long obs = _n 
    
    forvalues i = 1/5894 {
        quietly count if site == `i' 
        if r(N) > 1 { 
            display "{title:site `i'}"
            su obs if site == `i', meanonly 
            runtest mv in `r(min)'/`r(max)' 
            di 
        }
    }

    Comment


    • #3
      Also check out statsby

      Comment


      • #4
        Thanks for the help. I'm a bit puzzled about using in instead of if. I had tried
        if site==100 runtest mv
        which gave an error message.
        With in the data are sorted by site and mv,so how would in pull out a particular site and do runtest for just the sequence of values of mv (identified by id) within that site?

        Comment


        • #5
          That's not going to help here.

          First, runtest doesn't support an if qualifier; that is presumably because any gaps in the series make a nonsense of the test.

          Second, an if command is not appropriate here. See

          FAQ . . . . . . . . . . . . . . . . . . . . if command versus if qualifier
          . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . J. Wernow
          4/05 I have an if or while command in my program that
          only seems to evaluate the first observation.
          What's going on?
          http://www.stata.com/support/faqs/programming/
          if-command-versus-if-qualifier/

          for a somewhat backward explanation that

          Code:
          if site==100 runtest mv
          will always be read as

          Code:
          if site[1]==100 runtest mv
          which seems unlikely for your data. In any case, the runtest run would be for the entire dataset, which is not at all what you want.


          That said, the syntax looks legal to me but you don't show the error message, which we do ask of you. (FAQ Advice #12)

          Comment


          • #6
            Sorry, the simple if command does work -but it runs the test on the whole lot instead of selecting. Many thanks for your specimen code, which I was able to get to work (with suitable adaptation), and which I will study as a model.

            Comment

            Working...
            X