Reputation: 35
I'm trying to see if the class 'means' of four separate groups (pKa recorded at pH 6.1, 6.7, 7.3 and 8.1) are significantly different to the individual data points I obtained for each group (pKa recorded at pH 6.1, 6.7, 7.3 and 8.1)
Here is a sample of my data set
#Sample of my data (called Exp2)
structure(list(
pKa = c(6.946, 7.1, 6.625, 7.528, 7.102, 6.743,6.936, 6.579, 6.672, 7.27),
pH = c("pH_6.1", "pH_6.7", "pH_7.3", "pH_8.1", "pH_6.1", "pH_6.7", "pH_7.3", "pH_8.1", "pH_6.1", "pH_6.7"),
id = c("XAU", "XAU", "XAU", "XAU", "MyData", "MyData", "MyData","MyData", "PQ", "PQ")),
row.names = c(NA, 10L), class = "data.frame")
Here I have transformed the data frame in order to run several one sample t-tests of the class means for each group (different buffer pHs of 6.1, 6.7, 7.3 and 8.1) respectively. However I wondering if there is a more effective/ better way to carry out my analysis.
#Obtaining values to compare class means to
tapply(Exp2MyData$pKa, Exp2MyData$pH, mean)
#pH_6.1 pH_6.7 pH_7.3 pH_8.1
#7.102 6.743 6.936 6.579
#pH = 8.1
Buff_8.1 <- subset(Exp2,pH=="pH_8.1")
m8.1 <- Buff_8.1[,1]
#pH = 7.3
Buff_7.3 <- subset(Exp2,pH=="pH_7.3")
m7.3 <- Buff_7.3[,1]
#pH =
Buff_6.7 <- subset(Exp2,pH=="pH_6.7")
m6.7 <- Buff_6.7[,1]
#pH = 6.1
Buff_6.1 <- subset(Exp2,pH=="pH_6.1")
m6.1 <- Buff_6.1[,1]
###### Checking normality of individual data sets
shapiro.test(m6.1)
shapiro.test(m6.7)
shapiro.test(m7.3)
shapiro.test(m8.1)
# All not significantly different from normal distribution
####### Applying t.test
#t.test
t.test(m8.1, mu= 6.579) # p-value = 5.025e-11
t.test(m7.3, mu= 6.936) #p-value = 0.00564
t.test(m6.7, mu= 6.743) # p-value = 0.0005285
t.test(m6.1, mu= 7.102) # p-value = 3.85e-06
# All significantly different
Upvotes: 1
Views: 67
Reputation: 21938
Special thanks to Mr. Yuriy Saraykin that helped me fix the bug in my codes. This is not at all an alternative solution I just wanted to find out what went wrong.
Exp2 <- structure(list(
pKa = c(6.946, 7.1, 6.625, 7.528, 7.102, 6.743,6.936, 6.579, 6.672, 7.27),
pH = c("pH_6.1", "pH_6.7", "pH_7.3", "pH_8.1", "pH_6.1", "pH_6.7", "pH_7.3", "pH_8.1", "pH_6.1", "pH_6.7"),
id = c("XAU", "XAU", "XAU", "XAU", "MyData", "MyData", "MyData","MyData", "PQ", "PQ")),
row.names = c(NA, 10L), class = "data.frame")
library(dplyr)
library(tidyr)
library(broom)
Exp2 %>%
select(-id) %>%
group_by(pH) %>%
nest(data = c(pKa)) %>%
ungroup() %>%
mutate(mu = c(6.579, 6.936, 6.743, 7.102),
ttest = map2(data, mu, ~t.test(.x, mu = .y)),
tidied = map(ttest, ~ tidy(.x))) %>%
unnest(tidied)
# A tibble: 4 x 12
pH data mu ttest estimate statistic p.value parameter conf.low conf.high method
<chr> <list> <dbl> <lis> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
1 pH_6.1 <tibble~ 6.58 <hte~ 6.91 2.61 0.121 2 6.37 7.45 One Samp~
2 pH_6.7 <tibble~ 6.94 <hte~ 7.04 0.655 0.580 2 6.37 7.71 One Samp~
3 pH_7.3 <tibble~ 6.74 <hte~ 6.78 0.241 0.849 1 4.80 8.76 One Samp~
4 pH_8.1 <tibble~ 7.10 <hte~ 7.05 -0.102 0.935 1 1.02 13.1 One Samp~
# ... with 1 more variable: alternative <chr>
Modified version of the code so that column id
is included:
Exp2 %>%
group_by(pH) %>%
nest(data = c(id, pKa)) %>%
ungroup() %>%
mutate(mu = c(6.579, 6.936, 6.743, 7.102),
ttest = map2(data, mu, ~t.test(.x$pKa, mu = .y)), #Just pay attention that we use $ to subset .x variable because pKa is not the only varibale nested in data list column and we also have id variable, but in the previous code I did not use $.
tidied = map(ttest, ~ tidy(.x))) %>%
unnest(c(data, tidied)) %>%
select(-ttest)
# A tibble: 10 x 12
pH id pKa mu estimate statistic p.value parameter conf.low conf.high method
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
1 pH_6.1 XAU 6.95 6.58 6.91 2.61 0.121 2 6.37 7.45 One Sample~
2 pH_6.1 MyDa~ 7.10 6.58 6.91 2.61 0.121 2 6.37 7.45 One Sample~
3 pH_6.1 PQ 6.67 6.58 6.91 2.61 0.121 2 6.37 7.45 One Sample~
4 pH_6.7 XAU 7.1 6.94 7.04 0.655 0.580 2 6.37 7.71 One Sample~
5 pH_6.7 MyDa~ 6.74 6.94 7.04 0.655 0.580 2 6.37 7.71 One Sample~
6 pH_6.7 PQ 7.27 6.94 7.04 0.655 0.580 2 6.37 7.71 One Sample~
7 pH_7.3 XAU 6.62 6.74 6.78 0.241 0.849 1 4.80 8.76 One Sample~
8 pH_7.3 MyDa~ 6.94 6.74 6.78 0.241 0.849 1 4.80 8.76 One Sample~
9 pH_8.1 XAU 7.53 7.10 7.05 -0.102 0.935 1 1.02 13.1 One Sample~
10 pH_8.1 MyDa~ 6.58 7.10 7.05 -0.102 0.935 1 1.02 13.1 One Sample~
# ... with 1 more variable: alternative <chr>
Upvotes: 1
Reputation: 8880
possible solution
df <- structure(list(
pKa = c(6.946, 7.1, 6.625, 7.528, 7.102, 6.743,6.936, 6.579, 6.672, 7.27),
pH = c("pH_6.1", "pH_6.7", "pH_7.3", "pH_8.1", "pH_6.1", "pH_6.7", "pH_7.3", "pH_8.1", "pH_6.1", "pH_6.7"),
id = c("XAU", "XAU", "XAU", "XAU", "MyData", "MyData", "MyData","MyData", "PQ", "PQ")),
row.names = c(NA, 10L), class = "data.frame")
library(tidyverse)
library(broom)
df %>%
group_nest(pH) %>%
transmute(
pH,
mu = c(6.579, 6.936, 6.743, 7.102),
pValue = map2_dbl(data, mu, ~t.test(x = .x$pKa, mu = .y)$p.value),
Mean = map_dbl(data, ~mean(.x$pKa)))
#> # A tibble: 4 x 4
#> pH mu pValue Mean
#> <chr> <dbl> <dbl> <dbl>
#> 1 pH_6.1 6.58 0.121 6.91
#> 2 pH_6.7 6.94 0.580 7.04
#> 3 pH_7.3 6.74 0.849 6.78
#> 4 pH_8.1 7.10 0.935 7.05
df %>%
group_nest(pH) %>%
transmute(
pH,
mu = c(6.579, 6.936, 6.743, 7.102),
pValue = map2(data, mu, ~t.test(x = .x$pKa, mu = .y) %>% glance(.))
) %>%
unnest(pValue)
#> # A tibble: 4 x 10
#> pH mu estimate statistic p.value parameter conf.low conf.high method
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
#> 1 pH_6.1 6.58 6.91 2.61 0.121 2 6.37 7.45 One Samp~
#> 2 pH_6.7 6.94 7.04 0.655 0.580 2 6.37 7.71 One Samp~
#> 3 pH_7.3 6.74 6.78 0.241 0.849 1 4.80 8.76 One Samp~
#> 4 pH_8.1 7.10 7.05 -0.102 0.935 1 1.02 13.1 One Samp~
#> # ... with 1 more variable: alternative <chr>
Created on 2021-04-05 by the reprex package (v2.0.0)
Upvotes: 2