# analyzing difference between two groups by other numeric factor with only (0-5) values

I want to analyze the difference between two groups by other factor. the factor is numeric with distribution (0-5) only. using Stata® what would be the best test to estimate the significance of difference ?

I tried using t test but i got error massage that the factor need to have two categories only.

    . ttest group, by (y)

more than 2 groups found, only 2 allowed
r(420);

• You may want to rethink if your y variable is really numeric. The fact that you've called it a "factor" and "numeric" may be telling you something. It sounds like it may be a ordered categorical, or ordinal, variable. In that case, a t-test may not be the best approach. – Sal Mangiafico Nov 23 '18 at 10:36

I'm not sure what your what data you are using or what you mean by 'group' and 'factor'. So this is pure speculation.

Maybe you have 100 subjects in Group 1 and 100 subjects in Group 2. Everyone took a survey with possible responses $$y$$ that have values $$0, 1, \dots, 5.$$ Now you want to do a two-sample t test to see if responses differ between the two groups. In that case, the phrase would be that you want to test the significance of "$$y$$ by group." I think you might have reversed the roles of y and gp when you did the t test.

I am using R rather than Stata.

In Group 1 a table of my y-scores is as follows:

y      0  1  2  3  4  5
count  9 23 27 31  9  1


In Group 2 a similar table of y-values is:

y      0  1  2  3  4  5
count  0 10 28 29 25  8


My variable gp has 100 $$1$$'s and 100 $$2$$'s.

Then in R, the Welch (separate-variances) two-sample is as follows:

t.test(y ~ gp)

Welch Two Sample t-test

data:  y by gp
t = -5.0766, df = 197.74, p-value = 8.845e-07
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-1.1385362 -0.5014638
sample estimates:
mean in group 1 mean in group 2
2.11            2.93


And here is the pooled two-sample t test:

t.test(y ~ gp, var.eq=T)

Two Sample t-test

data:  y by gp
t = -5.0766, df = 198, p-value = 8.836e-07
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-1.1385337 -0.5014663
sample estimates:
mean in group 1 mean in group 2
2.11            2.93


If my guess is right, then try again in Stata to see if you get reasonable answers. If my guess is wrong, then maybe you can use what I have done in order to explain more clearly what you mean.

• If I guessed your mistake correctly, it is because a mistake I have seen many times before. – BruceET Nov 23 '18 at 2:18
• @SalMangiafico what test do you recommend ? – N00 Nov 23 '18 at 17:05
• @SalMangiafico, indeed the Y variable represent an answer to question of how many. i appreciate any more advice. – N00 Nov 23 '18 at 18:23
• This began as a question about how to do a t test in Stata. Now the (uninformed) discussion seems to have centered on whether to do a t test at all. Not having seen the dist'n of your scores from the subjects or how many subjects you have, I can't say whether something else would be better than a t test. It is not obvious that MWW or other methods would be better. And it is not obvious whether the alternatives would be worse. That discussion would be facilitated if you could show your data, or at least give more details about it. // The t test is fine for the simulated data I showed. – BruceET Nov 24 '18 at 5:18