1

I would like to conduct a two-sample independent t-test and my grouping variable, "group" is factored as '0' and '1'. When calling the t.test(), it calculates the difference as mean for group 0 - mean for group 1, giving me a negative difference and negative confidence intervals.

How can I reverse the order of comparison so that R estimates mean for group 1 - mean for group 0? I know it compares alphabetically, so I assume it compares numbers in increasing value. I already tried fct_rev() from the forcats package to set group '1' as the reference group, but that did not change the order in which t.test compared and still gave negative outputs. This is important as it is a 1 sided test so I have expectations on the sign of the difference.

Thanks!

EDIT: I'd like to keep the names as '0' and '1' because they need to be coded as such for subsequent analyses to work

Cassandra
  • 137
  • 1
  • 9

2 Answers2

0

You can use the levels argument when constructing the factor. It is possible to revert the levels (see code below) and it is also possible to specify an arbitrary order:

# two samples with different mean
x <- c(rnorm(10, 5, 1), rnorm(10, 8, 1))

# the grouping variable
f1 <- factor(rep(0:1, each=10))

# the grouping variable with reverse order of levels
f2 <- factor(f1, levels=rev(levels(f1)))

t.test(x ~ f1)

t.test(x ~ f2)
tpetzoldt
  • 5,338
  • 2
  • 12
  • 29
  • Thanks! I was aiming for this with `fct_rev()` (which didn't work initially) but your proof made me try it from another way. Previously, I tried to reverse the levels before a series of subsets and reshaping, but now I moved the reverse levels further down, a version of this: `shapeddf <- df %>% gather (....) %>% gather(...) %>% mutate( group = fct_rev(as.factor(group)) )` made the t-tests compare in the order I wanted – Cassandra Nov 11 '21 at 22:15
0

For some reason I cannot comment, can you not simply recode your variables and rerun it so they run in the correct order? e.g.:

mydata <- read.csv(yourdataset.csv, na.strings = "")
#make a new "recoded group" variable
mydata$group.recoded <- mydata$group
mydata <- within(mydata, group.recoded[group.variable==group0] <- "group2")

  • Thanks for the suggestion. I have edited my post to reflect that I want to keep the names, but I've since found a fix – Cassandra Nov 11 '21 at 22:19