Announcement

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

  • How to run multiple likelihood-ratio tests using loop function on Stata

    Hi everyone,

    I am new to Stata. I would really appreciate any help you would provide me.

    I ran 209 logistic regressions and stored their estimates (E1, E2, ..., E209).
    Of 209 models, the 1st one (M1) is the basic model and rest of 208 models have 208 different additional variables (Mn=m1+Xn, X1=0).
    Then, I tried to compare the fitness of every model (M2-M208) to M1 and ran multiple likelihood-ratio tests using loop function, but I could not complete.

    Here are the commands I used (I am sorry, but I changed the names of variables and estimates):

    // I ran 209 logistic regressions (M1, M2, ..., M209) and stored their estimates (E1, E2, ..., E209)
    logit m1+X1
    estimate store E1
    logit m1+X2
    estimate store E2
    ..........
    logit m1+X209
    estimate store E209
    //stored estimates are _est_E1, _est_E2, ...., _est_E209

    //Then, I tried to run multiple likelihood-ratio tests using loop functions
    . local var "E1-E209"
    . foreach x of local var {
    2. lrtest E1 "`x'"
    3. }

    //Then, I got the following error message:
    estimation result E1-E209 not found

    I could run lrtest individually, and found that lrtest recognized E1, E2, ..., E209, but local only recognized _est_E1, _est_E2, ..., _est_E209.
    How can I run the looped likelihood-ratio test in such a case?

    I also would like to get a matrix for p-value of each likelihood test to generate a scattered graph of these p-values.
    Thus, tried following commands and partially worked (I could generate the empty matrix)
    . unab y:_est_E2-_est_E209
    . local l:word count `y'
    . matrix p = J(`l',1,.)
    . matrix rownames p = `y'
    . matrix colnames p = "p"
    . matlist p
    My question is how I can incorporate these commands into loop commands to run multiple likelihood-ratio tests and generate a matrix for p-value of each likelihood test.

    Any help will be greatly appreciated.

    Thanks in advance,

    Yuki

  • #2
    There are a number of both conceptual and syntax errors in your code. Here's an example of how to do this using the built-in auto.dta.

    Code:
    sysuse auto, clear
    
    logit foreign mpg
    estimates store E1
    
    local j = 2
    foreach x of varlist headroom trunk weight {
        logit foreign mpg `x'
        estimates store E`j'
        local ++j
    }
    
    matrix p = J(4, 1, .)
    forvalues i = 2/4 {
        lrtest E1 E`i'
        matrix p[`i', 1] = r(p)
    }
    matrix list p
    That said, in Stata it is usually not helpful to store results in a matrix, unless you are planning to do some actual matrix algebra with that matrix. And it's hard for me to think of an application of matrix algebra to a matrix that contains p-values. Apparently you plan to do some kind of graph (I don't know what you mean by a "scattered graph."), which suggests that you might be better off saving these p-values in a new Stata data set.

    Comment


    • #3
      Hi Clyde,

      I really appreciate your quick response and useful information. The code you suggested me perfectly worked!
      And, now I understood my several mistakes .

      Apparently, I need more training for using this software with more experience.

      Thanks so much again,

      Yuki

      Comment

      Working...
      X