Perform multiple paired t-tests based on groups/categories

廉价感情. 提交于 2019-11-29 02:32:11

One way to do it is to use by:

result <- by(data, data$Product_type, 
    function(x) t.test(x$Price_Online, x$Price_offline, mu=0, alt="two.sided", paired = TRUE, conf.level = 0.99))

The only drawback is that by returns a list, and if you want your results in a dataframe, you have to convert it:

df <- data.frame(t(matrix(unlist(result), nrow = 10)))

You'll then have to add the product type and column names manually:

df$Product_type <- names(result)
names(df) <- names(result$A)
yeedle

The tidy way of doing it is using dplyr and broom:

library(dplyr)
library(broom)

df <- data %>% 
  group_by(Product_type) %>% 
  do(tidy(t.test(.$Price_Online, 
                 .$Price_Offline, 
                 mu = 0, 
                 alt = "two.sided", 
                 paired = TRUE, 
                 conf.level = 0.99))))

Much more readable than my base r solution, and it handles the column names for you!

EDIT A more idiomatic way to do it rather than using do (see r4ds) is to use nest to create nested dataframes for each product type, then run a t-test for each nested dataframe using map from purrr.

library(broom)
library(dplyr)
library(purrr)
library(tidyr)

t_test <- function(df, mu = 0, alt = "two.sided", paired = T, conf.level = .99) {
  tidy(t.test(df$Price_Offline, 
              df$Price_Online,
              mu = mu, 
              alt = alt,
              paired = paired,
              conf.level = conf.level))
}

d <- df %>%
  group_by(Product_type) %>%
  nest() %>%
  mutate(ttest = map(data, t_test)) %>%
  unnest(ttest, .drop = T)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!