Parametric tests require us to make certain assumptions about the distribution of the data. In cases where the data violates these assumptions, parametric methods would not be appropriate for data analysis and hypothesis testing. These assumptions include: normality (data is normally distributed/symmetrical, data isn’t scewed with heavy outliers), homogeneity (equal variances between groups), linearity, independent data.
The Pearson correlation is used to measure the association between two variables that have a linear relationship. Unlike the Pearson correlation, the Spearman correlation isn’t limited to data with linear relationships. The Spearman correlation is a rank-based Pearson correlation and be used to assess the strength of a monotonic relationship between two variables. The Spearman correlation can be used in the case of ordinal data while the Pearson correlation cannot be applied to ordinal data.
x <- c(1,2,3,4,5); y <- c(2, 3, 1, 4, 5)
# Spearman
cor.test(x, y, method="spearman")
##
## Spearman's rank correlation rho
##
## data: x and y
## S = 6, p-value = 0.2333
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## 0.7
# Kendalls Tau
cor.test(x, y, method="kendall")
##
## Kendall's rank correlation tau
##
## data: x and y
## T = 8, p-value = 0.2333
## alternative hypothesis: true tau is not equal to 0
## sample estimates:
## tau
## 0.6
Given the distribution of this data, Spearman and Kendalls are both sufficient for measuring correlation between x and y. We can tell that the relationship between x and y is not linear by looking at the plot, therefore, it would not be appropriate to use the Pearson correlation.
before <- c(102 , 86, 127, 140, 80)
after <- c(124 ,110 ,123 ,134, 154)
diff.4 <- before-after
mu.diff.4 <- mean(diff.4)
signs <- expand.grid(rep(list(c(-1,1)),5))
n <- 5
perm.4 <- as.matrix(signs)%*%as.matrix(diff.4)/n
1-sum(perm.4 > mu.diff.4)/length(perm.4)
## [1] 0.125
Null Hypothesis: The means of the two groups are the same.
\(H_{0}\): \(μ_{b}\)=\(μ_{a}\)
Alternative Hypothesis: The means of the two groups are different.
\(H_{A}\): \(μ_{b}\)≠\(μ_{a}\)
P-value=0.125
Conclusion: At \(\alpha\)=0.05 and p=0.125, we fail to reject the null hypothesis that the means of the two groups are the same.
Assumptions: The distribution is the same for the “Before” data and “After” data under the null hypothesis.
The p-value from part A is exact because, in this case, we were able to calculate every single permutation. If the number of permutations is too large then we can randomly sample permutations in order to get an approximate p-value.
values <- c(124, -1, -114, 142, -77, 124, -28, 101, -99, 18, 26, 212)
binom.test(sum(values > 50), length(values), p=0.50, alternative = "greater")
##
## Exact binomial test
##
## data: sum(values > 50) and length(values)
## number of successes = 5, number of trials = 12, p-value = 0.8062
## alternative hypothesis: true probability of success is greater than 0.5
## 95 percent confidence interval:
## 0.1810248 1.0000000
## sample estimates:
## probability of success
## 0.4166667
# pval
binom.test(sum(values > 50), length(values), p=0.50, alternative = "greater")$'p.value'[1]
## [1] 0.8061523
Null & Alternative Hypothesis:
\(H_{0}\): Median=50
\(H_{A}\): Median>50
P-value: 0.8062
Conclusion: At \(\alpha\)=0.05 and p=0.8062, we fail to reject the null hypothesis that the population median is=50. We do not have sufficient evidence to suggest that the population median is > 50.
Assumptions: Data is independent and IID.
data(iris)
summary(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
## 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
## Median :5.800 Median :3.000 Median :4.350 Median :1.300
## Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
## 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
## Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
## Species
## setosa :50
## versicolor:50
## virginica :50
##
##
##
kruskal.test(Petal.Width~Species, data=iris)
##
## Kruskal-Wallis rank sum test
##
## data: Petal.Width by Species
## Kruskal-Wallis chi-squared = 131.19, df = 2, p-value < 2.2e-16
Null Hypothesis:
\(H_{0}\): \(\theta_{1}\)=\(\theta_{2}\)=\(\theta_{3}\)=\(\theta_{n}\) where \(\theta_{i}\) is median petal width
Alternative Hypothesis:
\(H_{A}\): Not all \(\theta_{i}\) are equal
P-value < 2.2e-16
Conclusion: At \(\alpha\)=0.05 and p<0.0001, we reject the null hypothesis and have sufficient evidence to conclude that there is a difference in the median petal widths for one or more species.
# Bonferroni
pairwise.wilcox.test(iris$Petal.Width,iris$Species,p.adj="bonf",paired = TRUE)
##
## Pairwise comparisons using Wilcoxon signed rank test
##
## data: iris$Petal.Width and iris$Species
##
## setosa versicolor
## versicolor 2.2e-09 -
## virginica 2.2e-09 3.3e-09
##
## P value adjustment method: bonferroni
After applying the Bonferroni correction, we find there are statistically significant pair-wise differences between all groups.
# Tukey HSD
library(MultNonParam)
tukey.kruskal.test(iris$Petal.Width,iris$Species)
## 1-2 1-3 2-3
## TRUE TRUE TRUE
Tukey’s HSD also reports statistically significant pair-wise differences between all groups.
# Least Significant Difference (LSD)
library(agricolae)
LSD <- LSD.test(aov(lm(Petal.Width~Species, data = iris)), "Species"); LSD
## $statistics
## MSerror Df Mean CV t.value LSD
## 0.04188163 147 1.199333 17.06365 1.976233 0.08088724
##
## $parameters
## test p.ajusted name.t ntr alpha
## Fisher-LSD none Species 3 0.05
##
## $means
## Petal.Width std r LCL UCL Min Max Q25 Q50
## setosa 0.246 0.1053856 50 0.1888041 0.3031959 0.1 0.6 0.2 0.2
## versicolor 1.326 0.1977527 50 1.2688041 1.3831959 1.0 1.8 1.2 1.3
## virginica 2.026 0.2746501 50 1.9688041 2.0831959 1.4 2.5 1.8 2.0
## Q75
## setosa 0.3
## versicolor 1.5
## virginica 2.3
##
## $comparison
## NULL
##
## $groups
## Petal.Width groups
## virginica 2.026 a
## versicolor 1.326 b
## setosa 0.246 c
##
## attr(,"class")
## [1] "group"
The LSD method also reports statistically significant pair-wise differences between all species groups.
dat.7<-data.frame(y=c(21.308064, 6.800487, 2.742283, 34.083525, 29.941135, 11.941155, 51.397883, 36.453405, 20.557651),
trt=factor(rep(1:3, 3)),
block=factor(rep(c(1,2,3), each=3)))
kruskal.test(dat.7$y~dat.7$trt)
##
## Kruskal-Wallis rank sum test
##
## data: dat.7$y by dat.7$trt
## Kruskal-Wallis chi-squared = 3.8222, df = 2, p-value = 0.1479
The Kruskal-Wallis test reports a p-value=0.1479 which is not significant at the \(\alpha\)=0.05 level. The Kurskal-Wallis test is not appropriate in this setting due to the blocking element of the data set.
friedman.test(dat.7$y, dat.7$trt, dat.7$block)
##
## Friedman rank sum test
##
## data: dat.7$y, dat.7$trt and dat.7$block
## Friedman chi-squared = 6, df = 2, p-value = 0.04979
The Friedman test reported a p-value=0.04979. Technically, this is significant at the \(\alpha\)=0.05 level but I think it’s important to note that this outcome is just barely within the significance range. Regardless, the Friedman test is still an appropriate statistical method for this case.
We can ignore the blocking effect in the model if the p-value reported by the Kruskal-Wallis test is significant.