2

Let's say I have a store and I want to know which people buy the most. I decide to divide my customers between young people (under 40) and old people (above 40). I have a data frame and in each of its row there is the individual name, id and the information if he bought anything (1) or no (0). How can I know if the difference between the number of people who bought is significant? Is it by using a t-test? And what would be the input here?

AFAIK, the t-test compare two means, so I can't see how would that make sense for a binary variable.

Dumb ML
  • 197
  • 6
  • See [this Q&A](https://stats.stackexchange.com/questions/90893/why-use-a-z-test-rather-than-a-t-test-with-proportional-data?noredirect=1&lq=1) for a discussion of z vs t test for proportions. – BruceET May 10 '20 at 18:12

1 Answers1

2

You could look at $2 \times 2$ table with columns Y and O for age groups and rows P and N for whether a purchase was made.

          Y     O     Tot
 P       35    43      88
 N       98    82     180
 Tot    143   125     268     

To see if the proportions of sales for Y and O groups are significantly different, you have your choice of several kinds of tests. The following ones are shown here:

  • If you have lots of data you could do a chi-squared test.

  • With fewer observations, you might want to use Fisher's exact test. (If you get an error message from a chi-squared test, saying that you don't have enough data, then use Fisher's exact test.)

  • A test of two proportions of purchases.

Here is how to run the various tests in R. Most of the tests can use a matrix of the data as follows:

MAT = matrix(c(35, 53, 98, 82), byrow=T, nrow=2)
MAT
     [,1] [,2]
[1,]   35   53
[2,]   98   82

Chi-squared tests. Here are two slightly different versions of essentially the same test.

chisq.test(MAT)

        Pearson's Chi-squared test 
        with Yates' continuity correction

data:  MAT
X-squared = 4.5194, df = 1, p-value = 0.03351

I don't believe it is good idea to do the Yates correction for samples as large as this. The correction can make the chi-squared statistic smaller, and thus the P-value larger.

Without correction, the test is as follows:

chisq.test(MAT, cor=F)

         Pearson's Chi-squared test

data:  MAT
X-squared = 5.0894, df = 1, p-value = 0.02407

Fisher's exact test. This test uses the row and column totals and (and one of the four individual counts) along with a hypergeometric distribution. Before modern computation, Fisher's test was used mainly for small counts because of difficulties computing cumulative hypergeometric probabilities. Typically, as here, the P-value for Fisher's exact test is between the P-values for the chi-squared test, with and without the Yates correction.

fisher.test(MAT)

       Fisher's Exact Test for Count Data

data:  MAT
p-value = 0.02729     
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
 0.3177046 0.9568553
sample estimates
odds ratio 
 0.5537864 

Test of two proportions. As implemented in R, prop.test is a test to see if two proportions $\hat p_y = 35/143 = 0.245$ and $\hat p_o = 53/135 = 0.424$ are consistent with each other or significantly different.

This test, comparing proportions of people in each age group who made purchases, may be easier to explain in a report for non-statisticians.

Notice that the two numbers of purchases and the two group sizes are used for the data in this test. [As of 5/10/20, instructions in the R documentation for prop.test give information about the use of a matrix that is incompatible with R version 3.4.4.]

prop.test(c(35,53), c(143,135))

    2-sample test for equality of proportions 
    with continuity correction

data:  c(35, 53) out of c(143, 135) 
X-squared = 6.3486, df = 1, p-value = 0.01175
alternative hypothesis: two.sided
 95 percent confidence interval:
  -0.26344067 -0.03223402
sample estimates:
   prop 1    prop 2 
0.2447552 0.3925926 

Without the continuity correction, the P-value is a little smaller:

prop.test(c(35,53), c(143,135), cor=F)$p.val
[1] 0.008081579

For comparison, here is a test of two proportions from Minitab statistical software, which uses a normal approximation, seemingly with no continuity correction. [Computations agree with the formula given in the NIST link near the beginning of this Answer.]

Test and CI for Two Proportions 

Sample   X    N  Sample p
1       35  143  0.244755
2       53  135  0.392593

Difference = p (1) - p (2)
Estimate for difference:  -0.147837
95% CI for difference:  (-0.256240, -0.0394342)
Test for difference = 0 (vs ≠ 0):  
    Z = -2.67  P-Value = 0.008

Note: I am just showing you tests in R that are appropriate for your data. I am not advocating that you should do them all. For your own data, you should decide which one test to do. The P-values of these tests will generally be pretty much the same (here, significant at levels roughly from 3% to 1%). Even so, it would be cheating to do them all and pick the one with the smallest P-value.

BruceET
  • 47,896
  • 2
  • 28
  • 76