Pairwise Comparison of Proportion with R

Pairwise comparison of proportions is a statistical method used to compare the proportions of success or the presence of a certain characteristic between multiple groups. In R, several packages and functions are available to perform these comparisons, providing a robust toolkit for statistical analysis in various fields, such as medical research, psychology, marketing, and more. This article will guide you through the process of conducting pairwise comparisons of proportions using R, including the setup, execution, and interpretation of the results.

Understanding Pairwise Comparison of Proportions

Pairwise comparison of proportions involves comparing the proportions of a binary outcome (e.g., success/failure, yes/no) between pairs of groups to determine if there are statistically significant differences between them. This method is often used when you have more than two groups and want to compare each possible pair of groups.

Hypothesis Testing

In pairwise comparison of proportions, the null hypothesis (H0) typically states that the proportions are equal between the two groups being compared. The alternative hypothesis (H1) states that the proportions are different. Statistical tests are used to determine whether the observed differences between proportions are statistically significant.

Steps to Conduct Pairwise Comparison in R

Now we will discuss Pairwise Comparison of proportion with R step by step.

1. Data Preparation

First, you need to prepare your data. Let’s consider an example dataset where we have responses from different groups on whether they achieved a particular outcome (1 for success, 0 for failure).

R
# Example dataset
data <- data.frame(
  group = rep(c("Group1", "Group2", "Group3"), each = 100),
  outcome = c(rbinom(100, 1, 0.5), rbinom(100, 1, 0.6), rbinom(100, 1, 0.4))
)
# Check the structure of the data
str(data)

Output:

'data.frame':    300 obs. of  2 variables:
$ group : chr "Group1" "Group1" "Group1" "Group1" ...
$ outcome: int 1 0 1 1 0 0 0 0 0 1 ...

2. Pairwise Comparison Using the prop.test Function

The prop.test function in R can be used for comparing proportions. However, for pairwise comparisons, we need to run this function multiple times for each pair of groups.

R
# Function to perform pairwise comparison
pairwise_prop_test <- function(data, group_var, outcome_var) {
  # Get unique groups
  groups <- unique(data[[group_var]])
  # Initialize a list to store results
  results <- list()
  
  # Loop through each pair of groups
  for (i in 1:(length(groups) - 1)) {
    for (j in (i + 1):length(groups)) {
      group1 <- data[data[[group_var]] == groups[i], ]
      group2 <- data[data[[group_var]] == groups[j], ]
      
      # Number of successes and trials for each group
      successes <- c(sum(group1[[outcome_var]]), sum(group2[[outcome_var]]))
      trials <- c(nrow(group1), nrow(group2))
      
      # Perform proportion test
      test <- prop.test(successes, trials)
      
      # Store results
      results[[paste(groups[i], "vs", groups[j])]] <- test
    }
  }
  
  return(results)
}

# Perform pairwise proportion tests
results <- pairwise_prop_test(data, "group", "outcome")

# Display the results
results

Output:

$`Group1 vs Group2`

2-sample test for equality of proportions with continuity correction

data: successes out of trials
X-squared = 1.3279, df = 1, p-value = 0.2492
alternative hypothesis: two.sided
95 percent confidence interval:
-0.23549292 0.05549292
sample estimates:
prop 1 prop 2
0.55 0.64


$`Group1 vs Group3`

2-sample test for equality of proportions with continuity correction

data: successes out of trials
X-squared = 5.1452, df = 1, p-value = 0.02331
alternative hypothesis: two.sided
95 percent confidence interval:
0.02377193 0.31622807
sample estimates:
prop 1 prop 2
0.55 0.38


$`Group2 vs Group3`

2-sample test for equality of proportions with continuity correction

data: successes out of trials
X-squared = 12.505, df = 1, p-value = 0.0004059
alternative hypothesis: two.sided
95 percent confidence interval:
0.1162046 0.4037954
sample estimates:
prop 1 prop 2
0.64 0.38
  • Group 1 vs Group 2: No significant difference in proportions (p-value = 0.2492).
  • Group 1 vs Group 3: Significant difference in proportions, with Group 1 having a higher success rate (p-value = 0.02331).
  • Group 2 vs Group 3: Highly significant difference in proportions, with Group 2 having a higher success rate (p-value = 0.0004059).

3. Adjusting for Multiple Comparisons

When conducting multiple pairwise comparisons, the risk of Type I error (false positives) increases. To adjust for this, methods such as the Bonferroni correction or the Holm method can be used.

R
# Applying Bonferroni correction
p_values <- sapply(results, function(x) x$p.value)
adjusted_p_values <- p.adjust(p_values, method = "bonferroni")

# Display adjusted p-values
adjusted_p_values

Output:

Group1 vs Group2 Group1 vs Group3 Group2 vs Group3 
0.747516832 0.069931711 0.001217592
  • Group 1 vs Group 2: No significant difference (p-value = 0.7475).
  • Group 1 vs Group 3: No significant difference, but close to significance (p-value = 0.0699).
  • Group 2 vs Group 3: Highly significant difference (p-value = 0.0012).

4. Interpreting the Results

After performing the tests and adjusting the p-values, the next step is to interpret the results. If the adjusted p-value for a pair of groups is less than the significance level (e.g., 0.05), we reject the null hypothesis and conclude that there is a significant difference in proportions between those groups.

R
# Setting significance level
alpha <- 0.05
# Checking significant results
significant_results <- which(adjusted_p_values < alpha)
# Display significant comparisons
names(significant_results)

Output:

[1] "Group2 vs Group3"

In this process, we:

  1. Set the significance level.
  2. Identified which adjusted p-values were below this threshold.
  3. Retrieved and displayed the names of the significant comparisons.

The only significant comparison at the 0.05 significance level is between Group 2 and Group 3. This indicates that the adjusted p-value for the comparison “Group2 vs Group3” is less than 0.05, showing a statistically significant difference in proportions between these two groups.

Conclusion

Pairwise comparison of proportions in R is a powerful method for statistical analysis in studies involving multiple groups. By following the steps outlined above, you can effectively conduct these comparisons, adjust for multiple testing, and interpret the results. R provides robust functions and packages that make this process straightforward, ensuring that your analysis is both accurate and comprehensive. Whether you are in medical research, marketing, or any other field requiring proportion comparison, this method will be invaluable in drawing meaningful conclusions from your data.



Contact Us