Announcement

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

  • Foreach paired ttest loop

    Hi there,

    I'm having trouble running the foreach command using paired ttest.

    I have tried something like this, but it did not work:

    foreach var of varlist ZLracylcarn*{
    foreach i in 1 2{
    ttest ZLracylcarn`i'*==ZLracylcarn`i+1'*
    }

    My variables at time point one are named:
    ZLracylcarn_1_C0_
    ZLracylcarn_1_C10_
    and so on....

    And my variables at time point two are named:
    ZLracylcarn_2_C0_
    ZLracylcarn_2_C10_
    and so on....

    Also, how can I export these results to excel?

    Any help is very welcome.
    Many thanks
    Nathalie

  • #2
    I don't find your question clear. "something like this": always show your exact code, not a paraphrase. "didn't work": please do read https://www.statalist.org/forums/help#stata on why this is not a good idea.

    There are at least three specific problems here.

    The first is that you want one loop not two.

    The second is that local macro evaluations don't work in the way you ask.

    The third is your outer loop is over a bunch of variables but you want to take them two at a time.

    It seems that you want to compare the results at time 1 and time 2 and at time 2 and time 3. I am guessing there.

    Code:
    forval t = 1/2 {
        local T = `t'+ 1
        ttest ZLracylcarn_`t'_C0_ = ZLracylcarn_`T'_C0_
        ttest ZLracylcarn_`t'_C10_ = ZLracylcarn_`T'_C10_
    }
    That said, I can't comment definitively on whether this analysis is well-founded, but two t-tests sharing one variable are not independent.

    I guess by "excel" you mean "MS Excel". I don't know anything much about export to Excel and I can't see that you're telling anyone what results you want exported or in what form.


    Comment


    • #3
      Many thanks Nick for your help, really appreciate it. I'm new in the forum and will be more clear in my future posts.
      I just want to compare the results at time 1 and time 2 for many different metabolites.
      The problem I have with the code you sent is that the variables have an identical name until `t' (for time 1) or `T' (for time 2) and after that they differ. For example, _C0_ and _C10_ represent different metabolites. I tried to include a * after `t' and `T', but it says the syntax was invalid

      Code:
      forval t = 1/2 {
      local T = `t'+ 1
      ttest ZLracylcarn_`t'* = ZLracylcarn_`T'*
      }


      Do you have any other suggestion?
      Many thanks
      Nathalie

      Comment


      • #4
        Please show us all the variable names concerned. We can't guess at syntax otherwise, as the only information here seems to be that they differ from what you have told us.

        Show the results of

        Code:
        describe ZLracylcarn*

        Comment

        Working...
        X