Statistical tests
February 19, 2024
A random variable \(X\) is a measurable function \(X: \Omega \rightarrow E\) from a sample space \(\Omega\) (a set of possible outcomes) to a measurable space \(E\).
\[ P(X \in S) = P(\{\omega \in \Omega \mid X(\omega) \in S\}) \]
Experiment | Random variable |
---|---|
Toss two dice | \(Y =\) sum of the numbers |
Toss a coin 25 times | \(Y =\) number of heads in 25 tosses |
Apply different amounts of fertilizer | \(Y =\) yield per acre |
Can communicate / can not communicate | \(Y =\) contribution to public good |
Reflects a notion of magnitude, that is, if the values it can take are numbers. A quantitative variable represents thus a measure and is numerical.
Note: For all measurements, we usually stop at a standard level of granularity
also referred as categorical variables or factors are variables that are not numerical and which values fits into categories.
In other words, a qualitative variable is a variable which takes as its values modalities, categories or even levels, in contrast to quantitative variables which measure a quantity on each individual.
Note: that a qualitative variable with exactly 2 levels is also referred as a binary or dichotomous variable.
On the other hand, a qualitative ordinal variable is a qualitative variable with an order implied in the levels. For instance, if the severity of road accidents has been measured on a scale such as light, moderate and fatal accidents, this variable is a qualitative ordinal variable because there is a clear order in the levels.
Another good example is health, which can take values such as poor, reasonable, good, or excellent. Again, there is a clear order in these levels so health is in this case a qualitative ordinal variable.
\(H_{A}: p > p_{0}\): Reject \(H_{0}\) if \(B \geq b_{\alpha; n, p_{0}}\).
\(H_{A}: p < p_{0}\): Reject \(H_{0}\) if \(B \leq c_{\alpha; n, p_{0}}\).
\(H_{A}: p \neq p_{0}\): Reject \(H_{0}\) if \(B \geq b_{\alpha_{1}; n, p_{0}}\) or \(B \leq c_{\alpha_{2}; n, p_{0}}\), where \(\alpha_{1}+\alpha_{2}=\alpha\).
Due to the discreteness of \(B\), tests cannot be conducted for all \(\alpha\) values.
Use the binom.test
function in R for performing a binomial test.
x <- 4
n <- 20
binom.test(x, n, p = 0.5,
alternative = c("two.sided", "less", "greater"),
conf.level = 0.95)
Exact binomial test
data: x and n
number of successes = 4, number of trials = 20, p-value = 0.01182
alternative hypothesis: true probability of success is not equal to 0.5
95 percent confidence interval:
0.057334 0.436614
sample estimates:
probability of success
0.2
alternative
: Specifies the alternative hypothesis.conf.level
: Confidence level of the interval.# Sample data: Observed frequencies
observed <- matrix(c(10, 20, 30, 40), nrow = 2)
# Perform the Chi-squared test
test.result <- chisq.test(observed)
# Print the test results
print(test.result)
Pearson's Chi-squared test with Yates' continuity correction
data: observed
X-squared = 0.44643, df = 1, p-value = 0.504
# Sample data: Contingency table
observed <- matrix(c(8, 2, 1, 9), nrow = 2, byrow = TRUE)
# Perform Fisher Exact Test
test.result <- fisher.test(observed)
# Display the test results
print(test.result)
Fisher's Exact Test for Count Data
data: observed
p-value = 0.005477
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
2.057999 1740.081669
sample estimates:
odds ratio
27.32632
# Sample data
group1 <- c(7, 8, 9, 5, 6)
group2 <- c(1, 2, 3, 4, 5)
# Perform the Wilcoxon Mann-Whitney test
test.result <- wilcox.test(group1, group2)
# Display the test results
print(test.result)
Wilcoxon rank sum test with continuity correction
data: group1 and group2
W = 24.5, p-value = 0.01597
alternative hypothesis: true location shift is not equal to 0
Output includes the test statistic (W) and the p-value. A low p-value indicates a significant difference between the group medians.
Interpretation: The test helps assess the effect of an intervention or treatment by comparing measurements taken before and after.
# Sample paired data
before <- c(120, 101, 130, 108, 143)
after <- c(115, 107, 132, 101, 138)
# Perform the Wilcoxon signed-rank test
test.result <- wilcox.test(before, after, paired = TRUE)
# Display the test results
print(test.result)
Wilcoxon signed rank test with continuity correction
data: before and after
V = 10, p-value = 0.5879
alternative hypothesis: true location shift is not equal to 0
The wilcox.test
function is used with paired = TRUE
for paired data.
Output includes the test statistic (V) and the p-value.
A low p-value suggests a significant difference in the median values of the two groups.
# Sample data: Here, 'data' is a data frame with ranked data
# and 'group' is the grouping factor
library(tidyverse)
library(ggpubr)
library(rstatix)
data("selfesteem", package = "datarium")
selfesteem <- selfesteem %>%
gather(key = "time", value = "score", t1, t2, t3) %>%
convert_as_factor(id, time)
# Perform the Friedman test
res.fried <- selfesteem %>% friedman_test(score ~ time |id)
res.fried
# A tibble: 1 × 6
.y. n statistic df p method
* <chr> <int> <dbl> <dbl> <dbl> <chr>
1 score 10 18.2 2 0.000112 Friedman test
# Perform Pearson's correlation test
x <- c(1, 2, 3, 4, 5)
y <- c(2, 3, 4, 5, 6)
cor.test(x, y, method = "pearson")
Pearson's product-moment correlation
data: x and y
t = 82191237, df = 3, p-value < 2.2e-16
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
1 1
sample estimates:
cor
1
Cohen's d
d estimate: -0.3872983 (small)
95 percent confidence interval:
lower upper
-1.859353 1.084756