2

I have a data set where the samples are grouped by row. This is because I'm working with different replicates. Here is an example of my data format, where the Sample 1 is in the first row with his 3 replicates values (-5.11, -6.64, -2.44)

R1       R2      R3
-5.11   -6.64   -2.44
-4.77   -6.64    2.49
-6.64   -4.01    3.07
-6.64    0.66   -3.65
-3.06    2.7    -6.64
 3.26    3.23    1.64
 2.34    3.28    3.25
 3.14    0.79    3.18
 2.98    3.12   -6.64
-6.64   -1.85   -3.86

What I would like to do is an ANOVA analysis row by row using the 3 replicates and have a new column with the p-value. This is how it would look like:

R1       R2      R3     p-value
-5.11   -6.64   -2.44   0.91    (1º anova)
-4.77   -6.64    2.49   0.006   (2º anova)
-6.64   -4.01    3.07   0.05    (3º anova)
-6.64    0.66   -3.65   0.0001
-3.06    2.7    -6.64   0.0006
 3.26    3.23    1.64   0.60
 2.34    3.28    3.25    ... 
 3.14    0.79    3.18    ...
 2.98    3.12   -6.64    ... 
-6.64   -1.85   -3.86    ...

I tried to do this with Excel, but the problem with this is that Excel creates a summary table, not just the result. For few results this's OK, I copy the p-value and copy in the fourth column, but in this case I have 13000 rows...

Here you are a reproducible example:

R1 = c(-5.11,-4.77,-6.64,-6.64,-3.06,3.26,2.34)
R2 = c(-6.64,-6.64,-4.01,0.66,2.7,3.23,3.28)
R3 = c(-2.44,2.49,3.07,-3.65,-6.64,1.64,3.25)

mydata = data.frame(cbind(R1,R2,R3))

I tried to use R in order to do this analysis, but I didn't find the way to do it row by row. The only option I found was by column.

Thank you in advance.

LAP
  • 6,605
  • 2
  • 15
  • 28
Enrique
  • 842
  • 1
  • 9
  • 21
  • If I understand correct, you want to do an ANOVA on three points? If that's true, this may be awfully little information to put into a model. What is the dependent variable? – Roman Luštrik May 31 '18 at 08:36
  • Exactly Roman Luštrik . For example, First ANOVA with the data (-5.11,-6.64 and -2.44), second ANOVA (-4.77, -6.64, 2.49) etc. I know that maybe is awfully little information, but sometimes we have more than 3 replicates and this case, my boss wants to do something with that p-values. – Enrique May 31 '18 at 08:41

1 Answers1

2

Taking the advice found here, we use stack to produce a dataframe with one value and one indicator variable (ind), then perform the aov:

R1 = c(-5.11,-4.77,-6.64,-6.64,-3.06,3.26,2.34)
R2 = c(-6.64,-6.64,-4.01,0.66,2.7,3.23,3.28)
R3 = c(-2.44,2.49,3.07,-3.65,-6.64,1.64,3.25)

mydata = data.frame(cbind(R1,R2,R3))

mat <- t(mydata)
rownames(mat) <- NULL
colnames(mat) <- letters[seq_len(ncol(mat))]
df <- stack(as.data.frame(mat))

> head(df)
  values ind
1  -5.11   a
2  -6.64   a
3  -2.44   a
4  -4.77   b
5  -6.64   b
6   2.49   b

aov(values ~ ind, data = df)

Call:
   aov(formula = values ~ ind, data = df)

Terms:
                     ind Residuals
Sum of Squares  164.5202  179.6335
Deg. of Freedom        6        14

Residual standard error: 3.582033
Estimated effects may be unbalanced

If we need more information, we can also use anova(lm(...)):

test <- anova(lm(values ~ ind, data = df))
summary(test)

       Df         Sum Sq         Mean Sq         F value          Pr(>F)      
 Min.   : 6   Min.   :164.5   Min.   :12.83   Min.   :2.137   Min.   :0.1134  
 1st Qu.: 8   1st Qu.:168.3   1st Qu.:16.48   1st Qu.:2.137   1st Qu.:0.1134  
 Median :10   Median :172.1   Median :20.13   Median :2.137   Median :0.1134  
 Mean   :10   Mean   :172.1   Mean   :20.13   Mean   :2.137   Mean   :0.1134  
 3rd Qu.:12   3rd Qu.:175.9   3rd Qu.:23.77   3rd Qu.:2.137   3rd Qu.:0.1134  
 Max.   :14   Max.   :179.6   Max.   :27.42   Max.   :2.137   Max.   :0.1134  
                                              NA's   :1       NA's   :1  

Edit: The ANOVA won't give you single p-values, but lm will:

summary(lm(values ~ ind, data = df))

Call:
lm(formula = values ~ ind, data = df)

Residuals:
   Min     1Q Median     3Q    Max 
-4.307 -1.797 -0.440  0.550  5.597 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)  
(Intercept)   -4.730      2.068  -2.287   0.0383 *
indb           1.757      2.925   0.601   0.5577  
indc           2.203      2.925   0.753   0.4637  
indd           1.520      2.925   0.520   0.6114  
inde           2.397      2.925   0.819   0.4263  
indf           7.440      2.925   2.544   0.0234 *
indg           7.687      2.925   2.628   0.0199 *
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 3.582 on 14 degrees of freedom
Multiple R-squared:  0.478, Adjusted R-squared:  0.2543 
F-statistic: 2.137 on 6 and 14 DF,  p-value: 0.1134
LAP
  • 6,605
  • 2
  • 15
  • 28
  • This is a big step. But how can I take the p-value for "A ind" values? – Enrique May 31 '18 at 09:00
  • Yes! That's what I want. Thanks bro – Enrique May 31 '18 at 09:13
  • 1
    @Enrique make sure you report sample size. Reporting confidence interval would probably be better (in concert with sample size). E.g. see Wasserstein, R. L., & Lazar, N. A. (2016). The ASA’s Statement on p -Values: Context, Process, and Purpose. The American Statistician, 70(2), 129–133. https://doi.org/10.1080/00031305.2016.1154108 – Roman Luštrik Jun 01 '18 at 07:50