R Code For Class 13 Cw March 11 Codec1 C411 704 665c2 ✓ Solved

Write R code for performing statistical analyses including testing hypotheses and calculating confidence intervals.

Your code should cover the following functions and methods:

  • Calculating the mean ratios of two sets of data.
  • Conducting a permutation test.
  • Using the Wilcoxon test for comparing treated and untreated groups.
  • Calculating confidence intervals for paired and independent samples.

Paper For Above Instructions

The objective of this paper is to provide R code implementations for various statistical analyses, aiming to compute mean ratios, conduct permutation tests, utilize the Wilcoxon test, and derive confidence intervals for different datasets. Each section will be accompanied by code explanations that help understand the statistical concepts being employed.

1. Mean Ratios of Two Data Sets

We start by defining two vectors of scores representing two methods, traditional and new, to analyze mean differences.

c1 <- c(4.11, 7.04, 6.65)

c2 <- c(5.23, 5.29, 5.31)

ratio <- mean(c1) / mean(c2)

print(paste("Mean ratio:", ratio))

This code calculates the mean ratio of two samples, providing insights into the comparative performance of the two methods.

2. Permutation Test

The permutation test is a non-parametric method used to determine whether two samples come from the same distribution. It involves the following steps:

A <- c(1, 2, 4)

B <- c(3, 5, 6, 7, 8, 9, 10)

R <- 9999 # Number of permutations

all <- c(A, B) # Combined samples

k <- 1:length(all) # Indices

reps <- numeric(R) # Storage for permuted test statistics

ts <- mean(A) - mean(B) # Observed test statistic

for (i in 1:R) {

m <- sample(k, size=length(A), replace=FALSE) # Randomly sample indices

a1 <- all[m] # Assigned permutation of sample A

b1 <- all[-m] # Remaining indices for sample B

reps[i] <- mean(a1) - mean(b1) # Calculate the test statistic for permutation

}

pvalue <- mean(c(ts, reps) >= ts) # Calculate approximate p-value

print(paste("P-value from permutation test:", pvalue))

The code above generates a pool of permutations, enabling the calculation of a p-value to test hypotheses regarding the means of two groups.

3. Wilcoxon Test for Treated vs Untreated Groups

The Wilcoxon test is applicable for comparing two paired groups. Here we demonstrate its implementation:

treated <- c(15, 21, 32)

untreated <- c(26, 45, 39, 52, 60, 70, 82)

treatedDF <- data.frame(ages=c(treated, untreated),

status=as.factor(c(rep("Treated", length(treated)), rep("Untreated", length(untreated)))))

library(coin)

wilcox_result <- wilcox_test(ages ~ status, data=treatedDF, distribution="exact", alternative="two.sided")

print(wilcox_result)

This R code applies the Wilcoxon rank-sum test, allowing researchers to test for differences in central tendency between treated and untreated groups while accommodating tied ranks.

4. Confidence Intervals for Paired Data

Calculating confidence intervals for paired data requires sorting the differences between pairs. This is demonstrated below:

anx_data <- read.table("anorexia.txt", header=TRUE)

x <- anx_data$group1 # First group's values

y <- anx_data$group2 # Second group's values

d <- sort(x - y) # Differences

n <- length(d)

walsh <- outer(d, d, "+") / 2 # Walsh averages

walsh <- sort(walsh[lower.tri(walsh, diag=TRUE)])

nW <- length(walsh)

k <- qsignrank(0.05/2, n) # Adjust alpha for confidence level

cat("Achieved confidence level:", 1 - 2 * psignrank(k-1, n), "\n")

confidence_interval <- c(walsh[k], walsh[nW + 1 - k])

print(paste("Confidence Interval:", confidence_interval))

Here, confidence intervals for paired differences are computed, providing a range within which the true central tendency likely lies.

5. Confidence Intervals for Independent Samples

Lastly, we will derive confidence intervals for independent samples:

granite <- c(33.63, 39.86, 69.32, 42.13, 58.36, 74.11)

basalt <- c(26.15, 18.56, 17.55, 9.84, 28.29, 34.15)

m <- length(granite)

n <- length(basalt)

diffs <- sort(as.vector(outer(granite, basalt, "-")))

alpha <- 0.05 # Significance level

k <- qwilcox(alpha/2, m, n)

if (k == 0) k <- k + 1

cat("Achieved confidence level:", 1 - 2 * pwilcox(k - 1, m, n), "\n")

confidence_interval <- c(diffs[k], diffs[length(diffs) + 1 - k])

print(paste("Confidence Interval for independent samples:", confidence_interval))

This code completes our statistical analysis for independent samples while providing a confidence interval for the difference of means.

Conclusion

The presented R code snippets provide a comprehensive example of statistical techniques applicable in research. By utilizing functions specific to hypothesis testing and confidence interval calculation, researchers can derive meaningful insights from their data.

References

  • Dalgaard, P. (2008). Introductory Statistics with R. Springer.
  • Wilcox, R. R. (2012). Introduction to Robust Estimation and Hypothesis Testing. Academic Press.
  • Hollander, M., & Wolfe, D. A. (1999). Nonparametric Statistical Methods. Wiley-Interscience.
  • Buchanan, R. A., & Schreiber, J. (2015). Statistical Methods for the Analysis of Experimental Data. Wiley.
  • R Core Team. (2021). R: A Language and Environment for Statistical Computing. R Foundation for Statistical Computing.
  • Ritz, C., & Spiess, A. N. (2008). Package 'glmnet'.
  • Gibbons, J. D., & Chakraborti, S. (2011). Nonparametric Statistical Inference. Chapman and Hall/CRC.
  • Chung, M. K., & Wang, P. (2017). Applied Statistical Inference: A Problem-Based Approach. Springer.
  • Higgins, J. P. T., & Thompson, S. G. (2002). Quantifying heterogeneity in a meta-analysis. Statistics in Medicine.
  • Field, A. (2013). Discovering Statistics Using R. SAGE Publications.