Announcement

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

  • program in the loop for bootstrap

    Hi everybody,

    could you please help me what is wrong with this code. the aim is from a reshaped imputed data set run the mixed separetly for cost and qaly to extract icost and iqaly and then bootstrap them for each set of imputed data sets.

    mi rename total_C_over18 y1
    mi rename qaly_at18month y2
    mi rename total_C0_b x1
    mi rename eq5d_0_b x2
    mi reshape long y x , i(pid) j(type)
    label values type
    gen cost=type==1
    set cformat %9.2f
    mi query
    local M = r(M)
    set seed 123
    forvalues i = 1/`M' {
    cap program drop test
    program define test, rclass
    mixed y i.cost i.cost#i.group i.cost#c.x || site: || cid:group if cost == 1 & (_mi_m == `i' | (_mi_m == 0 & _mi_miss == 0)), nocons reml
    return scalar icost = _b[1.cost#1.group]

    mixed y i.cost i.cost#i.group i.cost#c.x || site: || cid:group if cost == 0 & (_mi_m == `i' | (_mi_m == 0 & _mi_miss == 0)), nocons reml
    return scalar iqaly = _b[1.cost#1.group]
    end
    bootstrap bootsdiff_cost=_b[1.cost#1.group] bootsdiff_qaly=_b[1.cost#1.group], reps(20) seed(12345) saving(file`i', replace) : test `i'
    }

    it doesnt work.

  • #2

    You have a program define ... end sequence inside your loop. That seems unlikely to work well. In any case, why are you repeatedly trying to re-write the program? It should be defined outside any loop.

    On top of that test is a built-in command. So Stata will always try to run that before it tries your own program. Hence use a different name.

    There may be other problems but it's hard to comment without

    1. A reproducible example. That's likely to be hard for you to provide, but it's still a problem for us.

    2. An explanation of in what sense the program doesn't work.

    3. A signal on where your script fails.

    #2 and #3 should be much easier for you than #1.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      You have a program define ... end sequence inside your loop. That seems unlikely to work well. In any case, why are you repeatedly trying to re-write the program? It should be defined outside any loop.

      On top of that test is a built-in command. So Stata will always try to run that before it tries your own program. Hence use a different name.

      There may be other problems but it's hard to comment without

      1. A reproducible example. That's likely to be hard for you to provide, but it's still a problem for us.

      2. An explanation of in what sense the program doesn't work.

      3. A signal on where your script fails.

      #2 and #3 should be much easier for you than #1.
      thank you so much NicK,

      I have an imputed dataset including 50 set. I need to extract the point of estimations which are coef of group( for cost=1 and qaly=0). then I need to do 1000 resampling to get 1000 points of icost and iqaly. as my dataset is imputed, I cant directly do it. I tried o do on each set and then combine the results together. i did the below code for joint model it works well. i want to try how can I run it separately for each cost and qaly simultaneously and save the result in a unique file.

      forvalues i = 1/`M' {
      bootstrap bootsdiff_cost=_b[y:1.cost#1.group] bootsdiff_qaly= _b[y:0b.cost#1.group], reps(20) cluster(pid) idcluster(new_pid) seed(12345) saving(file`i', replace) :mixed y i.cost i.cost#i.group i.cost#c.x || site: || cid:group || new_pid: if _mi_m == `i' | (_mi_m == 0 & _mi_miss == 0), nocons reml res(uns, t(cost)) nolog
      }

      how can do it?

      BW

      Comment


      • #4
        Sorry, but while #3 backs up and tell us concisely what you want to do, it answers absolutely none of the questions I asked in #2.

        Statalist works best when very well specified questions allow very specific answers.

        Someone else may be able to help more with your project.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          Sorry, but while #3 backs up and tell us concisely what you want to do, it answers absolutely none of the questions I asked in #2.

          Statalist works best when very well specified questions allow very specific answers.

          Someone else may be able to help more with your project.
          You are right.

          once I runt test manually , there is no reaction in stata.By running the whole command, I mean do program in the loop bring me back an error of break.

          I have modified the command as follow :

          cap program drop test
          program define test, rclass
          mi estimate: mixed y i.cost i.cost#i.group i.cost#c.x || site: || cid:group if cost==1, nocons reml res(ind, by(group))
          matrix b= e(b_mi)
          matrix list b
          scalar delta_cost = b[1,3]
          return scalar delta_cost = delta_cost
          mi estimate: mixed y i.cost i.cost#i.group i.cost#c.x || site: || cid:group if cost==0, nocons reml res(ind, by(group))
          matrix z= e(b_mi)
          matrix list z
          scalar delta_qaly = z[1,3]
          scalar delta_qaly = z[1,3]
          return scalar delta_qaly = delta_qaly
          end

          mi query
          local M = r(M)
          set seed 123
          forvalues i = 1/`M' {
          bootstrap bootsdiff_cost=delta_cost bootsdiff_qaly= delta_qaly, reps(20) seed(12345) saving(file`i', replace) :test if _mi_m == `i' | (_mi_m == 0 & _mi_miss == 0)
          }
          use file1,clear
          forvalues i = 2/`M' {
          append using file`i'
          erase file`i'.dta
          }
          erase file1.dta
          bstat *
          estat bootstrap, percentile



          I expect the loop use the program in each loop. in this version manually running the program bring me back " nothing to restore", I don't know it is correct or not.
          Last edited by Hiro farabi; 20 Nov 2024, 07:28.

          Comment


          • #6
            #2

            On top of that test is a built-in command. So Stata will always try to run that before it tries your own program. Hence use a different name.
            I can't interact helpfully when your posts just do not engage with what has already been said.

            Comment

            Working...
            X