Announcement

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

  • 1 group found, 2 required r(420); Error

    Hi Everyone,

    I'm running a lot of ttests and I keep getting this error whenever there aren't enough observations to test. I'm wondering if there's something I can add to my code to tell stata to simply skip that test if there aren't enough observations. This is my code:

    foreach region in list {
    bysort ethnicity: ttest meanscore, by(survey)
    bysort personofcolor: ttest meanscore, by(survey)
    bysort gender: ttest meanscore, by(survey)
    bysort cgp: ttest meanscore, by(survey)
    bysort economicbackground: ttest meanscore, by(survey)
    }

    I have 52 regions and several options for each of the demographics I'm testing, but the demographic breakdowns aren't the same for every region. So for some regions, there are observations for both survey years, but for other regions, there aren't so my code stops and I get the "1 group found, 2 required r(420); Error" error.

    Is there an easy fix for this? I've thought about using the egen command to create a new variable that has a value of 1 for each combination of region ethnicity in the current survey that also appears in the previous survey and then use an if in my test command that is newvariable==1, but that would mean a lot of extra coding...

    Thanks in advance for your help!
    Eva
    Last edited by Eva Brown; 27 Apr 2018, 16:19.

  • #2
    Welcome to Statalist, Eva.

    It's been almost a day and there have been no responses to your post. There are several likely reasons.

    First, your code is unconvincing, because you loop over region, but within the loop make no use of region. That suggests the code you typed is not the code you ran, or else that you did not recognize that the code you ran was not taking account of the region. Neither is a good basis for giving advice.

    Second, there is no "easy" fix for this. Now, that normally doesn't stop Statalist members from providing code to do the task, but with the lack of clarity in the code you provided, whatever work was done would likely require revision when the intent of the loop over region was understood.

    Finally, I expect some members - including me - would discourage running such a large number of ttests. The significance level of the tests will be overstated: if you are testing for significance at the 5% level, you would expect 1 in 20 of your tests to be significant just by chance. (That's a very brief reminder of something that I'm certain you've heard in the course in which you learned about t-tests.) So I think there's a certain amount of reluctance to set you on a path that would lead you to erroneous conclusions.

    Having said that, let me show some code. This command
    Code:
    bysort ethnicity: ttest meanscore, by(survey)
    accomplishes, in effect the same thing as this code
    Code:
    levelsof ethnicity, local(vals)
    foreach eth of local vals {
        display as text "-> ethnicity = `eth'"      
        capture noisily ttest meanscore if ethnicity==`eth', by(survey)
    }
    where the latter code has the advantage that the loop runs each ttest separately, and the loop continues even after a ttest fails.

    Having shown that code, and having earlier encouraged you to not do what it does, I'll say that my intent is providing it is to demonstrate that Stata is capable of ignoring errors and continuing to run. I recommend you do something other than run what would be a minimum of 32 ttests (5 "by" variables with a minimum of 2 values each) for each of 52 regions. That's a lot of ttests to look at, and they seem unlikely to be helpful.
    Last edited by William Lisowski; 28 Apr 2018, 17:10.

    Comment


    • #3
      Not going to add content wise, William has covered that, but note that bysort has option rc0.

      Best
      Daniel

      Comment


      • #4
        Daniel is indeed correct; the rc0 option (see help bysort or help by for details) provides an easy fix for the problem of bysort stopping at the first failure. The other concerns raised in post #2 (failure of the code to incorporate the looping variable region; excessive numbers of tests) remain.

        Comment


        • #5
          William and Daniel, thank you for the guidance! William, you were correct - I should have included "region" in my loop. I'm new to coding in Stata, so apologies for the confusion. I do understand that this is a ridiculous amount of ttests to be running and that it leaves me susceptible to type 1 errors and will take this into consideration when deciding how to move forward.
          In any case, it's very helpful to have learned about the option to include "rc0".

          Comment


          • #6
            I have been following the resolution and would like to be helped with the following related problem;



            I would like to have a t test result with one command like the picture attached to this post. I am using the command

            t-test variable, by(treatment variable)

            If i add more than one variable, stata does not give any output but gives the message below;

            too many variables specified
            r(103);

            I would be very grateful if some can help me with a command that will give me an output with more than one variable.

            Thank you in anticipation profoundly.
            Attached Files

            Comment


            • #7
              note that there are a sizable number of user-written routines for producing these kinds of descriptive stats; some are at SSC, some at Stata Journal; use -search- to find and download; here are some names: table1, table1_mc, basetable, partchart, sumtable, baselinetable and summtab (and there may be others by now); many of these have been discussed on statalist so a search might help find more

              Comment


              • #8
                Thank you so much Mr Rich for the candid recommendation and guidance.

                Comment

                Working...
                X