Announcement

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

  • Looping over observation

    Hi,
    I want to run one-sample test of proportion using following data and save p-val for each obs.
    prtesti #obs1 #p1 #p2

    I tried the following program. The program is working for fist values only. Can someone tell me where is the problem with my program?
    Thanks.

    Nizam

    gen pval=.
    local N = _N
    forvalues i = 1/`N' {
    prtesti `=obs1' `=p1' `=p2'
    local p= 2*(normprob(-abs(r(z))))
    replace pval=`p'
    }
    obs1 p1 p2 pval
    1273314 0.5256 0.5150
    1631438 0.5278 0.5150
    1686524 0.5214 0.5150
    1980880 0.5298 0.5120
    8187141 0.5158 0.5175
    5386442 0.5193 0.5175
    4701894 0.4823 0.5173
    4232052 0.5311 0.5170

  • #2
    Code:
    gen pval=.
    local N = _N
    forvalues i = 1/`N' {
    prtesti `=obs1[`i']' `=p1[`i']' `=p2[`i']'
    local p= 2*(normprob(-abs(r(z))))
    replace pval=`p' in `i'
    }
    Your prtesti command

    1. really doesn't know or care that it is inside a loop.

    2. has the same form through the loop. Stata's rule is that the first observation is used in such circumstances when you feed a variable to a command expecting constants.

    Note that the replace statement must specify where you want to put the result; otherwise all values are changed..
    Last edited by Nick Cox; 06 Feb 2019, 12:02.

    Comment


    • #3
      Thanks Nick

      Comment


      • #4
        See also https://www.stata.com/support/faqs/d...iate-commands/

        Comment

        Working...
        X