I'm conducting a hypothesis test for the difference between two groups. The file dat1 contains all observations of measure for the first group; dat2 contains all observations of measure for the second group. Measures of both groups are normally distributed, so a parametric test is appropriate. The code for a student's t-test would look like this:

    ttest1 <- t.test(x = dat1$measure, y = dat2$measure, alternative 
                     = "two.sided", var.equal = TRUE)

But a Levene test indicates that the groups have differing variances, so the t-statistic needs to be adjusted. So the code looks like this:

    ttest2 <- t.test(x = dat1$measure, y = dat2$measure, alternative 
                     = "two.sided", var.equal = FALSE)

But I need to weight each observation. I can produce a weighted student's t-test using the wtd.t.test() function in the weights package:

    ttest3 <- wtd.t.test(x = dat1$measure, y = dat2$measure, 
                         weight = dat1$weight, weighty = 
         dat2$weight, alternative = "two.tailed")

But there is no var.equal argument that would allow me to use the Welch adjustment to correct for concerns about different variances. I have not been able to find another package that facilitates such a test.

Is there a package that allows for this? If not, I need to figure out how to write a function that has this feature.

  • 2
    $\begingroup$ Please explain what you mean by 'weight each observation'. In the Welch version of the tests (default setting var.eq=F), it is not relevant whether $\sigma_1^2$ and $\sigma_2^2$ are the same. If you want to test for equality of variances anyway you can still use an F-test (normal data) or Levene's test. $\endgroup$
    – BruceET
    Commented Jan 28, 2021 at 2:45
  • $\begingroup$ @BruceET - I mention in the post that I used a Levene test and determined the two groups have different variances. This violates the assumption of homogeneity in variance necessary for a student's t-test, and is why I plan to use the Welch test. Explaining what sample weights are may be out of scope, but it's a very standard procedure in survey research (thus the wtd.t.test() function in the popular weights package, which I use in my post). See documentation for ?weights::wtd.t.test() or this link: aapor.org/Education-Resources/For-Researchers/Poll-Survey-FAQ/… $\endgroup$
    – JmQ
    Commented Jan 28, 2021 at 2:52
  • 1
    $\begingroup$ Where you able to find a solution for this? I am currently running through the exact same problem, but I have 5 groups to compare. I searched everywhere on the internet and nothing. $\endgroup$
    – mimi
    Commented Oct 8, 2021 at 16:50
  • $\begingroup$ Was there enough data to match the two groups so that the distributions would be similar? $\endgroup$
    – user405514
    Commented Jan 23 at 21:16

1 Answer 1


The weights::wtd.t.test function does (try to) do the Welch t-test. It has

        vx <- wtd.var(x, weight, na.rm=TRUE)
        vy <- wtd.var(y, weighty, na.rm=TRUE)
        sxy <- sqrt((vx/n)+(vy/n2))


        df <- (((vx/n)+(vy/n2))^2)/((((vx/n)^2)/(n-1))+((vy/n2)^2/(n2-1)))

in the code

I don't think that's the correct thing to do, since the degrees of freedom calculation doesn't take weights (and any other features of sampling) into account. More generally, the denominator of the t-test in the code is an estimate of the denominator in a population version rather than an estimate of the standard error of the numerator. It may well be ok for something like Poisson sampling though.

In other options, the survey::svyttest function does a proper survey-weighted comparison of means.


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.