10
$\begingroup$

I have two groups of data that have different sample sizes and in order to be able to analyze both sets they must have the same variance. I was told I should use Bartlett's to test the homogeneity of variance, but when I try to run the test in R it says that the two groups must have the same sample size.

  1. Does Bartlett's test require the groups to have the same sample size?

  2. How was my labmate able to analyze a similar dataset (two groups, different sample sizes) using Bartlett's?

  3. What other test could I use that would show the two groups have similar variances?

$\endgroup$

3 Answers 3

13
$\begingroup$

I don't know what code you used, but tests do not require equal sample sizes. You can use Levene's test to check for heteroscedasticity. In R, you can use ?leveneTest in the car package:

set.seed(9719)                       # this makes the example exactly reproducible
g1 = rnorm( 50, mean=2, sd=2)        # here I generate data w/ different variances
g2 = rnorm(100, mean=3, sd=3)        #   & different sample sizes
my.data = stack(list(g1=g1, g2=g2))  # getting the data into 'stacked' format

library(car)                         # this package houses the function
leveneTest(values~ind, my.data)      # here I test for heteroscedasticity:
# Levene's Test for Homogeneity of Variance (center = median)
#        Df F value   Pr(>F)   
# group   1  8.4889 0.004128 **
#       148                    
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Levene's test is just a $t$-test ($F$-test) on transformed data. (I discuss tests for heteroscedasticity here: Why Levene test of equality of variances rather than F ratio?) What having unequal sample sizes will do is cause you to have less power to detect a difference. To understand this more fully, it may help to read my answer here: How should one interpret the comparison of means from different sample sizes? Note however, that running a test of your assumptions and then choosing a primary test is not generally recommended (see, e.g., here: A principled method for choosing between t-test or non-parametric e.g. Wilcoxon in small samples). If you are worried that there may be heteroscedasticity, you might do best to simply use a test that won't be susceptible to it, such as the Welch $t$-test, or even the Mann-Whitney $U$-test (which doesn't even require normality). Some information about alternative strategies can be gathered from my answer here: Alternatives to one-way ANOVA for heteroskedastic data.

$\endgroup$
3
$\begingroup$

If you're trying to test regression/anova/t-test assumptions, the advice of multiple papers is you're better off not testing the assumption as a basis for choosing a a procedure to apply (e.g. for choosing between an equal variance t-test and a Welch-t-test or between ANOVA and a Welch-Satterthwaite type adjusted ANOVA).

If you can't make the assumption a priori and your original sample sizes are not equal (or at least very close to equal) you should simply not use a procedure that assumes equal variances (in effect, always assume your heteroskedasticity test would reject, without looking).

$\endgroup$
5
  • $\begingroup$ I wasn't very specific in my question about what I needed it for, but you're right. Basically I have a huge dataset and it's split into two groups, measurements and estimates. I want to be able to analyze (not in a statistical sense) the dataset and maximize the number of samples I can use for the analysis by saying that the estimates are similar to the measurements. I could do the analysis separately for the two groups, but that would be much less powerful and the results would be confusing. I was able to determine that both groups have similar variance so I was able to pool the data. $\endgroup$ Nov 28, 2014 at 20:48
  • $\begingroup$ Often it's possible to simply assume they have different variance and still do a single, approximate analysis; after all that's what the Welch-adjusted versions of t-tests and ANOVA do. It's not clear to me that there's ever a big power loss in doing so; the point of the papers that say 'don't test' is there isn't. But maybe there's something about your analysis I'm missing. $\endgroup$
    – Glen_b
    Nov 29, 2014 at 0:16
  • $\begingroup$ The analysis is fairly complicated... I'm not using a typical statistical analysis like ANOVA or variants, instead I am in need of as many samples as possible in order to get an accurate estimate of various population parameters. I'm a fisheries biologist and, in short, the more samples I can use in my analysis the better. $\endgroup$ Nov 29, 2014 at 23:39
  • $\begingroup$ I'm not sure there's enough information here to give a much better response. $\endgroup$
    – Glen_b
    Nov 30, 2014 at 0:11
  • 1
    $\begingroup$ Could you provide additional information about the papers you mentioned, regarding the advice of not testing the assumption as a basis for choosing a procedure to apply? Thank you $\endgroup$
    – omega
    Nov 9, 2021 at 10:41
0
$\begingroup$

In R:

install.packages('lawstat')

require(lawstat)

levene.test(x ~ y, your_data)

Should work this way even if you have unequal sample size

$\endgroup$
1
  • $\begingroup$ Although implementation is often mixed with substantive content in questions, we are supposed to be a site for providing information about statistics, machine learning, etc., not code. It can be good to provide code as well, but please elaborate your substantive answer in text for people who don't read this language well enough to recognize & extract the answer from the code. $\endgroup$ Jun 9, 2022 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.