0

The data looks as follows:

> data <- read.csv("data.csv")
> head(data)
  ï..class.1    rev.1 class.2    rev.2
1          7 136.9900    1318  31.9900
2       1223  24.0984    1001   0.0000
3       1318  61.9900    6851 104.2655
4       1014  39.9800    1318  29.9800
5          7  32.9800    7     52.9900
6        291 107.6674     797  31.2741

I want to perform a significance test to compare the means of rev.1 and rev.2 ONLY where groups class.1=class.2. So for example, I am trying to get all the "7" classes compared, then all the 1318 classes compared. I tried doing this with ANOVA and TukeyHSD but am having issues only comparing the groups that I want. Any guidance would be greatly appreciated!

zx8754
  • 52,746
  • 12
  • 114
  • 209
Sharkbait
  • 1
  • 1
  • 1
    Welcome to StackOverflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – Sotos Dec 28 '17 at 13:56
  • Now I see you asked the same question twice: https://stackoverflow.com/questions/47999190/test-for-significance-of-specific-multiple-groups-in-r. Please do not do that the next time. – www Dec 28 '17 at 16:27

2 Answers2

0

Looks like the row data is not aligned. If so, then the data could be split into two groups and stacked on top of each other. Column names would be: class, class_id, rev. From there you can filter for the class_id of interest and then proceed with your analysis.

library(dplyr)
library(tidyr)

# create some data
rev.1 <- rnorm(100, 200,50)
rev.2 <- rnorm(100, 180,35)

class.1 <- seq.int(from = 1000, by = 10, length.out = 100)
class.2 <- seq.int(from = 1000, by = 20, length.out = 100)

df <- tibble(class.1 = class.1, rev.1 = rev.1, class.2 = class.2, rev.2 = rev.2)


# split the data and stack
group_1 <- df %>%
  select(class.1, rev.1) %>%
  gather(key = class,
         value = class_id,
         -rev.1) %>%
  rename(rev = rev.1)

group_2 <- df %>%
  select(class.2, rev.2) %>%
  gather(key = class,
         value = class_id,
         -rev.2) %>%
  rename(rev = rev.2)

df_stacked <- rbind(group_1, group_2)

# filter for the class_id of interest
df_filtered <- df_stacked %>%
  filter(class_id == 1020)
AlphaDrivers
  • 136
  • 4
0

If you want to compare means from two groups, it seems to me that t-test is a good choice. Here is an option using . First I created an example data frame called dat.

# Load package
library(tidyverse)

# Set seed
set.seed(12345)

# Create example data frame
dat <- expand.grid(class1 = 1:5, class2 = 1:5) %>%
  slice(rep(1:n(), 5)) %>%
  mutate(rev1 = rnorm(n()), rev2 = rnorm(n())) %>%
  mutate(rev2 = sample(rev2, size = n(), replace = TRUE))
# View the head of data frame
dat
# # A tibble: 125 x 4
#    class1 class2   rev1    rev2
#     <int>  <int>  <dbl>   <dbl>
#  1      1      1  0.586  0.548 
#  2      2      1  0.709  0.868 
#  3      3      1 -0.109  0.0784
#  4      4      1 -0.453 -0.567 
#  5      5      1  0.606 -0.0767
#  6      1      2 -1.82   0.167 
#  7      2      2  0.630  2.66  
#  8      3      2 -0.276  0.831 
#  9      4      2 -0.284 -1.70  
# 10      5      2 -0.919 -2.13  
# # ... with 115 more rows

After that, I filtered the data frame when class1 == class2, group the data by class1, and then conduct t-test using the do function. Finally,map_dbl can get the p.value of each t.test to a new data frame.

dat2 <- dat %>%
  filter(class1 == class2) %>%
  group_by(class1) %>%
  do(data_frame(class = .$class1[1],
                TTest = list(t.test(.$rev1, .$rev2)))) %>%
  mutate(PValue = map_dbl(TTest, "p.value"))
dat2
# # A tibble: 5 x 4
# # Groups: class1 [5]
#   class1 class TTest       PValue
#    <int> <int> <list>       <dbl>
# 1      1     1 <S3: htest> 0.700 
# 2      2     2 <S3: htest> 0.381 
# 3      3     3 <S3: htest> 0.859 
# 4      4     4 <S3: htest> 0.0580
# 5      5     5 <S3: htest> 0.206 

If you want to access the test result of a particular class, you can do the following.

# Get the result of the first class
dat2$TTest[dat2$class == 1]
# [[1]]
# 
# Welch Two Sample t-test
# 
# data:  .$rev1 and .$rev2
# t = 0.40118, df = 7.3956, p-value = 0.6996
# alternative hypothesis: true difference in means is not equal to 0
# 95 percent confidence interval:
#   -0.9379329  1.3262368
# sample estimates:
# mean of x mean of y 
# 0.6033533 0.4092013 

Here is another option, we can also split the data frame to a list and apply the t-test through the list.

# Split the data frame and conduct T-test
dat_list <- dat %>%
  filter(class1 == class2) %>%
  split(.$class1) %>%
  map(~t.test(.$rev1, .$rev2))

# Get the result of the first class
dat_list$`1`

# Welch Two Sample t-test
# 
# data:  .$rev1 and .$rev2
# t = 0.40118, df = 7.3956, p-value = 0.6996
# alternative hypothesis: true difference in means is not equal to 0
# 95 percent confidence interval:
#   -0.9379329  1.3262368
# sample estimates:
# mean of x mean of y 
# 0.6033533 0.4092013 
www
  • 38,575
  • 12
  • 48
  • 84