[This article was first published on Data Science Tutorials, and kindly contributed to R-bloggers], (You can report content on this page here) Want to share your content on R-Bloggers? Click here if you have a blog, or click here if you don’t have one.

How to post pairwise comparison in R? appeared first on Data Science Tutorials

What do you have to lose? See Data Science tutorial here Data Science Tutorial.

How to do pairwise comparisons in R One-way ANOVA is used to evaluate whether there is a statistically significant difference between the means of three or more independent groups.

The following null and alternative hypotheses are used in one-way ANOVA.

H0: All group means are equal. Ha: Not all group means are equal.

We reject the null hypothesis and conclude that all group means are not equal if the overall p-value of the ANOVA is less than a predetermined significance level (eg, =.05).

We can next perform post hoc pairwise comparisons to determine which group means differ.

How to compare variances in R – Data Science tutorial

Example: One-way ANOVA in R

Consider a teacher who is curious about whether using three different study methods affects students’ test results.

She randomly distributes ten students to each study method to test it, then she tracks their test results.

To conduct a one-way ANOVA in R and test for differences in mean test scores among the three groups, use the following code:

Let’s create a data frame

df <- data.frame(tech = rep(c("tech1", "tech2", "tech3"), each = 10), score = c(276, 377, 407, 581, 182, 112, 483, 484) , 185, 289, 81, 82, 183, 183, 183, 584, 187, 190, 192, 193, 77, 78, 177, 178, 179, 140, 178, 195, 145, 158) head (df) Tech Score 1 Tech1 176 2 Tech1 177 3 Tech1 107 4 Tech1 181 5 Tech1 182 6 Tech1 112

Now we can perform one-way ANOVA

model <- aov(score ~ tech, data = df)

View the output of ANOVA

Summary (model) df sum square mean square f value pr(>f) technique 2 184786 92393 6.159 0.00626 ** residual 27 405053 15002 — signif. Codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1” 1

We will reject the null hypothesis that the mean test score is the same for each study method because the overall p-value of the ANOVA (.00626) is less than =.05.

We can now perform posthoc pairwise comparisons to identify groups by different means.

Create new variables from existing variables in R – Data Science Tutorials

Tukey method

The Tukey posthoc method performs best when each group has the same sample size.

The built-in TukeyHSD() function in R can be used to implement the Tukey posthoc method:

Let’s use Tukey post-hoc analysis

TukeyHSD(model, conf.level=.95) Tukey multiple comparisons mean 95% family-wise confidence level Fit: aov(formula = score ~ tech, data = df) $technique diff lwr upr p adj tech2-tech1 – 131.8 – 267.6121 4.012102 0.0584488 tech3-tech1 -187.1 -322.9121 -51.287898 0.0055676 tech3-tech2 -55.3 -191.1121 80.512102 0.5773136

From the output, we can see that only those pairs with p-values (“p adj”) less than 0.05 are significantly different from each other.

How to create contingency table in R? – Data Science Tutorial

chef’s method

When group means are compared, the Scheffe technique yields the largest confidence intervals and is the most conservative posthoc pairwise comparison method.

To implement the Scheffe post-hoc approach in R, use the ScheffeTest() function from the DescTools package:

Libraries (DescTools)

Now Scheffe is ready to do the post-hoc method

ScheffeTest(model) after multiple comparisons of means: Scheffe Test 95% family-wise confidence level Tech3-tech1 -187.1 -328.971 -45.22895 0.0078 ** Tech3-tech2 -55.3 -197.171 86.57105 0.6064 — Significance. Codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1” 1

From the output we can see that only those pairs with a p-value (“p adj”) less than 0.05 are significantly different from each other.

How to do rounded corner bar plot in R? – Data Science Tutorial

Bonferroni method

The Bonferroni method is best to apply when you want to make a set of pre-planned pairwise comparisons.

To use the Bonferroni post-hoc procedure, we can use the R syntax shown below:

Let’s use Bonferroni post-hoc analysis

pairwise.t.test(df$score, df$technique, p.adj=’bonferroni’) Pairwise comparison using t-test with pooled SD data: df$score and df$technique tech1 tech2 tech2 0.0697 – tech3 0.0061 0.9650 P value adjustment method: Bonferroni the Holm method

You can also use the Holm technique when you want to make a set of pairwise comparisons planned in advance as it often has more power than the Bonferroni approach.

How to Create a Heatmap in R: The Quick and Easy Way – Data Science Tutorials

The Holm post-hoc approach can be used in R using the syntax shown below:

Holm post-hoc approach should be used.

pairwise.t.test(df$score, df$technique, p.adj=’holm’) Pairwise comparison using t test with pooled SD data: df$score and df$technique tech1 tech2 tech2 0.0465 – tech3 0.0061 0.3217 P value adjustment method: Holm

What is the best way to filter by line number in R? – Data Science Tutorial

How to post pairwise comparison in R? appeared first on Data Science Tutorials

Learn to become an expert in Data Science field with Data Science Tutorials.

related