Announcement

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

  • if else statement with local macros

    Hi Everyone,

    I would appreciate your help in solving my problem below:

    I have two Linear Mixed Models in the loop
    My aim is to write out the estimate of mean difference of either of the two models below where the AIC is smaller but all the mean difference written to text file are from the sub (sub model) even though there are cases where AIC of the full model is smaller.


    forvalues i = 1/`r(max)' {
    mixed Ct ib1.Time_point if group ==`i' ||_all:R.money || sample: , reml dfmethod(satte) stddeviations dftable(ci) emiter(25)
    estimates store full //This stores the first model

    mixed Ct ib1.Time_point if group ==`i' || sample: , reml dfmethod(satte) stddeviations dftable(ci) emiter(25)
    estimates store sub //This stores the second model

    estimates stats full sub


    I have set up a loop similar to the one below but all the mean difference written to text file are from the sub (sub model) even though there are cases where AIC of the full model is smaller.


    matrix icstat = r(S)

    local stat_AIC1 = icstat[1,5]
    local stat_AIC2 = icstat[2,5]

    if `stat_AIC1' < `stat_AIC2' {
    local `stat_AIC1' = icstat[1,5]
    *estimate restore full
    }
    else {
    local stat_AIC2 = icstat[2,5]
    *estimate restore sub
    }

    contrast i.Time_point, mcompare(sch)effects


    matrix difference = r(table) // difference between mean control(0 hr/time 1) being the base
    matrix pvalue = r(table) // p-value

    local Mean_Difference1 = difference[1,1]


    local pvalue1 = pvalue[4,1]


    forvalues j = 1/4 {

    Many thanks in anticipation of your support.

  • #2
    It is hard to read code not formatted as such. Please do read and act on FAQ Advice #12.

    Skimming through, I am confident that

    Code:
    local `stat_AIC1' = icstat[1,5]
    is not what you want: try

    Code:
    local stat_AIC1 = icstat[1,5]
    There may be other problems. Clearly we can't run this code without the data and are limited in reply to what looks wrong syntactically.

    Comment


    • #3
      Is it possible the code where you write the result to the text file has disappeared? Also, please enclode any stata code in code-brackets (the hashtag symbol in the layout bar), this makes it much more legible.

      Comment


      • #4
        Thanks Nick Cox and Jesse Wursten.
        I have tweaked my code to reflect what Nick suggested but the result is still the same. Note I am using Stata 14.2
        Please see below a sample of my entire code


        ( # file open Goning using "AA.txt", write append
        # file write Freeze "Sample" _tab "Contrast" _tab "Mean_difference" _tab "Mean_difference_pvalue"

        su group, meanonly
        forvalues i = 1/`r(max)' {
        mixed Ct ib1.Time_point if group ==`i' ||_all:R.money|| sample: , reml dfmethod(satte) stddeviations dftable(ci) emiter(25)
        estimates store full
        mixed Ct ib1.Time_point if group ==`i' || sample: , reml dfmethod(satte) stddeviations dftable(ci) emiter(25)
        estimates store sub

        estimates stats full sub

        matrix icstat = r(S)

        local stat_AIC1 = icstat[1,5]
        local stat_AIC2 = icstat[2,5]

        if `stat_AIC1' < `stat_AIC2' {
        local stat_AIC1 = icstat[1,5]

        }
        else {
        local stat_AIC2 = icstat[2,5]

        }

        matrix difference = r(table) // difference between mean control(0 hr/time 1) being the base
        matrix pvalue = r(table) // p-value

        local Mean_Difference1 = difference[1,1]
        local Mean_Difference2 = difference[1,2]
        local Mean_Difference3 = difference[1,3]
        local Mean_Difference4 = difference[1,4]

        local pvalue1 = pvalue[4,1]
        local pvalue2 = pvalue[4,2]
        local pvalue3 = pvalue[4,3]
        local pvalue4 = pvalue[4,4]

        forvalues j = 1/4 {

        file write Goning_n "`i'" _tab "`j'" _tab "`Mean_Difference`j''" _tab "`pvalue`j''"

        }
        }

        file close Goning

        Hope this helps you to help me!

        Thannks

        Comment


        • #5
          Hi all,

          Please I need your help with the questions I posed! I have not yet sorted the issue.

          Cheers,

          Comment


          • #6
            Ebenezer: You presented a big chunk of code that is incomplete and that we can't run and which is hard to read. You are still ignoring requests made in #2 and #3 about more legible display of code. No one here is obliged to answer anything and -- unfortunately -- there is little to say about questions that can't be understood.

            I can only suggest two strategies.

            1. Rewrite the question to be self-contained with data that you give and people can access and complete code that we can run and show the results you got and explain why they are wrong or puzzling or not what you want. That may be too difficult, but it's one way to make a question clearer.

            2. Cut down your script to code that runs without error and then build in complications step by step until something goes wrong. Then try to work out why.

            Further, I am just answering generically. I don't use mixed models and have no specific expertise there, so it's most unlikely that I will have any suggestions to follow.

            Sorry, but it's hard to give you better news.



            Comment


            • #7
              Code:
              ** Part 1
              local stat_AIC1 = icstat[1,5]
              local stat_AIC2 = icstat[2,5]
              
              ** Part 2
              if `stat_AIC1' < `stat_AIC2' {
              local stat_AIC1 = icstat[1,5]
              
              }
              else {
              local stat_AIC2 = icstat[2,5]
              
              }
              Like Nick Cox, I have no experience with mixed models or postestimation therein, so I don't know how to fix your issue. I can tell you that in the part of your code I quoted, the bit below "part 2" does nothing whatsoever. The locals stat_aic1/2 are already defined in part 1, hence defining one of them again in part 2 will not do anything?

              Comment

              Working...
              X