Announcement

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

  • problem with simulate command and rclass

    Dear all,

    I'm having a problem with simulate command. I have written an r-class program. When I run the program and use "return list", I get the estimates I need. However, when I use simulate to store those estimates, simulate returns an error and it does not store those values. I have tried using other estimates and it works fine but when trying to extract pval and ub it simply does not work. To try to solve the problem, I tried to extract the result, store it in a local, then generate a variable with that value and then use the r(mean) value of that variable - however this does not work. This is really odd and spent hours trying to figure out why it does not work without success.

    You can find below the code. Any help will be very much appreciated.


    Code:
    ssc install survsim
    cap prog drop sim_model
    prog define sim_model, rclass
        clear
        set obs 300
        gen trt = rbinomial(1,0.5)
        survsim time fail, lambdas(0.07) distribution(exponential) covariates(trt -.28768207) maxtime(48) 
        stset time, fail(fail)
        gen cured = rbinomial(1,0.20)
        replace time=48 if cured==1
        replace fail=0 if cured==1
        drop cured
        stset time, fail(fail)
    
        fmm: (pointmass fail) (streg trt, distribution(exponential))
        mat a=r(table_m2)
        local pval=el(a,4,1)
        gen pval=`pval'
        sum pval
        return scalar pval=r(mean)
        local pval=r(mean)
            
        local ubln=el(a,6,1)
        local ub=exp(`ubln')
        gen ub=`ub'
        sum ub
        return scalar ub=r(mean)
        local ub=r(mean)    
        ********************
        gen reject_ub=.
        replace reject_ub=1 if `ub'<1
        replace reject_ub=0 if reject_ub==.
        sum reject_ub    
        return scalar reject_ub=r(mean)
    
        gen reject_pvalue=.
        replace reject_pvalue=1 if `pval'<0.05
        replace reject_pvalue=0 if reject_pvalue==.
        sum reject_pvalue
        return scalar reject_pvalue=r(mean)
    
    end
    
    sim_model
    return list
    
    simulate ub=r(ub) pval=r(pval) reject_pvalue=r(reject_pvalue) reject_ub=r(reject_ub), reps(5): sim_model

  • #2
    I do not see anything wrong on first look.

    Try -set trace on- and report exactly what error message -simulate- returns.

    You can also try whether it works for only one returned result, e.g., just ub.

    Comment


    • #3
      I don't know why, but the code works well only when I turn on the "trace" option as below.

      Code:
      simulate ub=r(ub) pval=r(pval) reject_pvalue=r(reject_pvalue) reject_ub=r(reject_ub), trace reps(5): sim_model

      Comment


      • #4
        Originally posted by Joro Kolev View Post
        I do not see anything wrong on first look.

        Try -set trace on- and report exactly what error message -simulate- returns.

        You can also try whether it works for only one returned result, e.g., just ub.
        I used set trace on and there is nothing wrong. Simulate does not return an error

        Comment


        • #5
          Originally posted by Fei Wang View Post
          I don't know why, but the code works well only when I turn on the "trace" option as below.

          Code:
          simulate ub=r(ub) pval=r(pval) reject_pvalue=r(reject_pvalue) reject_ub=r(reject_ub), trace reps(5): sim_model
          Thank you Fei Wang. This solved the problem! I wonder what could be the problem here?

          Comment


          • #6
            Originally posted by Andrew Xavier View Post

            Thank you Fei Wang. This solved the problem! I wonder what could be the problem here?
            Sorry Andrew, I don't know why. Turning on and off "trace" is not supposed to affect the essence of the code. Hope some experts can decipher.

            Comment


            • #7
              Originally posted by Fei Wang View Post

              Sorry Andrew, I don't know why. Turning on and off "trace" is not supposed to affect the essence of the code. Hope some experts can decipher.
              Let's see if someone can decipher it

              Comment


              • #8
                This does not sound regular. Ask Stata technical support, and report what they said.

                Comment


                • #9
                  Thanks Joro Kolev,

                  I emailed them about this query and they will look at my query next week. I will then report what they said here

                  Comment


                  • #10
                    I received a reply from STATA:

                    You need to get the values from -r(table)-, which is the documented result. Results from the non-documented -r(table_m2)- are not posted when the command is called quietly.

                    i have done this and the simulate command works fine

                    Thanks

                    Comment

                    Working...
                    X