8

我坚持在 Rstudio 中为多个类别执行 t.tests。我想得到每种产品类型的 t.test 的结果,比较线上和线下的价格。我有超过 800 种产品类型,这就是为什么不想为每个产品组手动操作的原因。

我有一个名为 data 的数据框(超过 200 万行),如下所示:

> Product_type   Price_Online   Price_Offline   
1   A            48             37
2   B            29             22
3   B            32             40
4   A            38             36
5   C            32             27
6   C            31             35
7   C            28             24
8   A            47             42
9   C            40             36

理想情况下,我希望 R 将 t.test 的结果写入另一个名为 product_types 的数据框:

    > Product_type   
    1   A           
    2   B            
    3   C          
    4   D          
    5   E         
    6   F            
    7   G            
    8   H            
    9   I            
   800 ...

变成:

> Product_type   t         df       p-value   interval    mean of difference            
    1   A           
    2   B            
    3   C          
    4   D          
    5   E         
    6   F            
    7   G            
    8   H            
    9   I            
   800 ...

如果我在不同的数据框中拥有所有产品类型,这就是公式:

t.test(Product_A$Price_Online, Product_A$Price_Offline, mu=0, alt="two.sided", paired = TRUE, conf.level = 0.99)

必须有一种更简单的方法来做到这一点。否则我需要制作 800+ 个数据帧,然后执行 t 检验 800 次。

我尝试了列表和 lapply 的东西,但到目前为止它不起作用。我还在多个列上尝试了 t-Test: https ://sebastiansauer.github.io/multiple-t-tests-with-dplyr/

但是,最后他仍然手动插入男性和女性(对我来说超过 800 个类别)。

4

2 回答 2

23

这样做的整洁方法是使用 dplyr 和 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))))

比我的基本 r 解决方案更具可读性,它会为您处理列名!

编辑do比使用(参见r4ds) 更惯用的方法是使用nest为每种产品类型创建嵌套数据框,然后使用mapfrom为每个嵌套数据框运行 t 检验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)
于 2017-03-05T15:34:37.497 回答
5

一种方法是使用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)[c(1:9)])

要将结果放入数据框中,您必须这样rbind做:

type.convert(as.data.frame(do.call(rbind, result)), as.is=TRUE)
#     statistic parameter   p.value             conf.int estimate null.value   stderr alternative        method
# A    2.267787         2 0.1514719  -20.25867, 32.25867        6          0 2.645751   two.sided Paired t-test
# B -0.06666667         1 0.9576214  -477.9256, 476.9256     -0.5          0      7.5   two.sided Paired t-test
# C    1.073154         3 0.3618456 -9.996192, 14.496192     2.25          0 2.096624   two.sided Paired t-test

或者,使用管道:

do.call(rbind, result) |> as.data.frame() |> type.convert(as.is=TRUE)

数据

data <- structure(list(Product_type = c("A", "B", "B", "A", "C", "C", 
"C", "A", "C"), Price_Online = c(48L, 29L, 32L, 38L, 32L, 31L, 
28L, 47L, 40L), Price_Offline = c(37L, 22L, 40L, 36L, 27L, 35L, 
24L, 42L, 36L)), class = "data.frame", row.names = c("1", "2", 
"3", "4", "5", "6", "7", "8", "9"))
于 2017-03-05T15:19:55.603 回答