Announcement

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

  • Help with matrix and looping

    Dear all,

    I have a matrix consisting of two columns: one with the total number of patients and another with the number of cases. There are 60 rows, and I want to compare row 2 with row 1, then row 3 with row 2, and so on, using prtesti.
    I did the following:

    mkmat totalpacientes totalvedo , matrix(data)

    matrix list data

    data[60,2]

    totalpacie~s totalvedo

    r1 27424 587
    r2 33583 723
    r3 36663 784 ...

    forvalues i = 1/60 {
    local total`i' = data[`i',1]
    local casos`i' = data[`i',2]
    local total`i+1' = data[`i'+1,1]
    local casos`i+1' = data[`i'+1,2]

    di "Comparing month `i' with month `i+1'"
    prtesti total`i' casos`i' total`i+1' casos`i+1'

    }


    I got the error:
    'total1' found where integer expected

    Where is the mistake in my command?

    Thanks for your time

    T

  • #2
    Originally posted by Thyago Moraes View Post
    . . . I want to compare row 2 with row 1, then row 3 with row 2, and so on, using prtesti.
    All of that serial pairwise NHST sounds a little uh, but you don't need to put it into a matrix to do it.
    Code:
    version 18.0
    
    clear *
    
    input long totalpacientes int totalvedo
    27424 587
    33583 723
    36663 784
    end
    
    forvalues row = 2/`=_N' {
    
        local previous = `row' - 1
        local obs1 = totalpacientes[`previous']
        local p1 = totalvedo[`previous']
    
        local obs2 = totalpacientes[`row']
        local p2 = totalvedo[`row']
    
        prtesti `obs1' `p1' `obs2' `p2', count
    }
    
    exit
    You might be able to get at the same thing with an estimation command—something along the lines of
    Code:
    generate byte month = _n
    glm totalvedo i.month, family(binomial totalpacientes) link(identity)
    but with a time-series lag operator. Time series don't factor so much in my line of work and so I'm not very familiar with Stata commands relating to them, but it might be worth looking into that angle for its elegance compared to a loop with prtesti.

    By the way, you posted this in the Mata subforum.

    Comment


    • #3
      Dear Joseph, thank you very much for your time! It worked perfectly!

      all best

      T

      Comment

      Working...
      X