2

I am trying to compare the tensile strength of different material weights of material in R. The tensile data is as follows:

tensile <- read.table(text="   Weight Strength Replicate
1      15        7         1
2      15        7         2
3      15       15         3
4      15       11         4
5      15        9         5
6      20       12         1
7      20       17         2
8      20       12         3
9      20       18         4
10     20       18         5
11     25       14         1
12     25       18         2
13     25       18         3
14     25       19         4
15     25       19         5
16     30       19         1
17     30       25         2
18     30       22         3
19     30       19         4
20     30       23         5
21     35        7         1
22     35       10         2
23     35       11         3
24     35       15         4
25     35       11         5", header=TRUE)

The variable Weight should be regarded as a factor (explanatory/independent variable) for the purpose of this analysis:

tensile$Weight <- factor(tensile$Weight)

I first fitted a one-way ANOVA model to my data:

tensile.aov <- aov(Strength ~ Weight, data = tensile)

According to the ANOVA, there appears to be a difference in the different weights with respect to the response (strength). So I then decided to do pairwise comparisons using the LSD (Least Significant Difference):

LSD.aov(tensile.aov)

However, this LSD function was provided through a separate file, so I'm unfortunately unable to share the code here.

I calculated the LSD for my data and got the following table:

enter image description here

Note that, according to the raw p-values, the pairwise comparisons between the 35 and 15 and 25 and 20 weights are the only ones that are not significantly different from each other at the alpha = 0.05 significance level; the other pairwise comparisons are significantly different. I want to create a letter summary to illustrate this, where groups only have the same letter if they are not significantly different from each other, and groups which do not have the same letter are significantly different from each other:

enter image description here

How can I go about creating such a table in R?

I'm also totally open to a 'manual' solution. By this, I mean manually creating a table using vectors and such. I'm new to R, so I don't have a good grasp on even the most basic aspects.

The Pointer
  • 2,226
  • 7
  • 22
  • 50
  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Apr 06 '18 at 15:54
  • @MrFlick My apologies. I will edit with more now. – The Pointer Apr 06 '18 at 15:54
  • @MrFlick I added as much as I could, but one of the functions requires an an external r source file, so I was unable to provide it. Is that ok? – The Pointer Apr 06 '18 at 16:05

2 Answers2

1

The multcompView package can turn p-values into letters, but in this case, the emmeans package can do both the comparison and the letters.

library(emmeans)
em <- emmeans(tensile.aov, ~Weight)

summary(pairs(em, adjust="none"), infer=TRUE)
#>  contrast estimate      SE df    lower.CL   upper.CL t.ratio p.value
#>  15 - 20      -5.6 1.79555 20  -9.3454518 -1.8545482  -3.119  0.0054
#>  15 - 25      -7.8 1.79555 20 -11.5454518 -4.0545482  -4.344  0.0003
#>  15 - 30     -11.8 1.79555 20 -15.5454518 -8.0545482  -6.572  <.0001
#>  15 - 35      -1.0 1.79555 20  -4.7454518  2.7454518  -0.557  0.5838
#>  20 - 25      -2.2 1.79555 20  -5.9454518  1.5454518  -1.225  0.2347
#>  20 - 30      -6.2 1.79555 20  -9.9454518 -2.4545482  -3.453  0.0025
#>  20 - 35       4.6 1.79555 20   0.8545482  8.3454518   2.562  0.0186
#>  25 - 30      -4.0 1.79555 20  -7.7454518 -0.2545482  -2.228  0.0375
#>  25 - 35       6.8 1.79555 20   3.0545482 10.5454518   3.787  0.0012
#>  30 - 35      10.8 1.79555 20   7.0545482 14.5454518   6.015  <.0001
#>
#> Confidence level used: 0.95

cld(em, adjust="none")
#>  Weight emmean       SE df  lower.CL upper.CL .group
#>  15        9.8 1.269646 20  7.151566 12.44843  1    
#>  35       10.8 1.269646 20  8.151566 13.44843  1    
#>  20       15.4 1.269646 20 12.751566 18.04843   2   
#>  25       17.6 1.269646 20 14.951566 20.24843   2   
#>  30       21.6 1.269646 20 18.951566 24.24843    3  
#> 
#> Confidence level used: 0.95 
#> significance level used: alpha = 0.05
Aaron left Stack Overflow
  • 36,704
  • 7
  • 77
  • 142
0

I managed to do it as follows:

Weight = c(15, 20, 25, 30, 35)
mean = c(9.8, 15.4, 17.6, 21.6, 10.8)
letters = c("a", "b", "b", "", "a")
LSDletterSummary <- data.frame(Weight, mean, letters)
LSDletterSummary

If anyone has a better way to go about it, feel free to share.

The Pointer
  • 2,226
  • 7
  • 22
  • 50