Here we introduce various tests & intervals in R. The following are examples of some tests & intervals in R, with more details in the pages on them.

1 One Sample or One Variable Parametric Tests

This section has tests and intervals for one sample or one variable data. See the page on each test for more details.

Table of Tests and Intervals in R
One Sample or One Variable
Test Formula Example
One Sample T-tests \(t = \frac{\bar x - \mu_0}{s/ \sqrt n}\)
and Student’s t-distribution.
Is the mean equal to \(\mu_0\)?
data = c(1.6, -2.1, 2.2, -2.4, 1.1, 3.3)
t.test(data, alternative = "two.sided",
mu = 0, conf.level = 0.95)
One Proportion Tests
(with Z & Chi-squared)
\(z = \frac{\frac{x}{n} - p_0}{\sqrt \frac{p_0(1-p_0)}{n}}\)
\(\text{or} \quad \chi^2_1=\sum_{i=1}^{2}\frac{(O_i-E_i)^2}{E_i}\),
and standard normal distribution
or chi-squared distribution.
Is the proportion equal to \(p_0\)?
prop.test(x = 55, n = 100, p = 0.5,
alternative = "two.sided",
conf.level = 0.95,
correct = FALSE)
One Variance
Chi-squared
\(\chi^2 = \frac{(n-1)s^2}{\sigma_0^2}\)
and chi-squared distribution.
Is the variance equal to \(\sigma_0\)?
install.packages("DescTools")
library(DescTools)
data = c(10.8, 8.1, 5.8, 8.8, 6.1)
VarTest(data, sigma.squared = 4,
alternative = "two.sided",
conf.level = 0.95)
One-way ANOVA \(F = \frac{\tt{variance \; between \; groups}}{\tt{variance \; within \; groups}}\)
\(= \frac{\left[ \sum_{j=1}^G N_j(\bar y_{j\cdot}-\bar y_{\cdot \cdot})^2 \right] / (G-1)}{\left[ \sum_{j=1}^G \sum_{k=1}^{N_j} (y_{jk}-\bar y_{j\cdot})^2 \right]/ (N-G)}\)
and F-distribution.
Are the group means equal?
y = c(7.3, 5.5, 5.1, 1.3,
8.5, 4.9, 6.2,
5.7, 6.3, 8.2, 4.9, 6.4)
groups = c("A", "A", "A", "A",
"B", "B", "B",
"C", "C", "C", "C", "C")
anova(lm(y ~ groups))
Chi-squared
Goodness of Fit
\(\chi^2=\sum_{i}\frac{(O_{i}-E_{i})^2}{E_{i}}\)
and chi-squared distribution.
Does the observed frequency distribution
fit the expectation?

chisq.test(c(19, 16, 11, 6),
p = c(0.4, 0.3, 0.2, 0.1))
Exact Binomial The number of successes, \(x\),
and binomial distribution.
Is the proportion equal to \(p_0\)?
binom.test(x = 14, n = 25, p = 0.5,
alternative = "two.sided",
conf.level = 0.95)


2 Two Samples or Two Variables Parametric Tests

This section has tests and intervals for two samples or two variables data. See the page on each test for more details.

Table of Tests and Intervals in R
Two Samples or Two Variables
Test Formula Example
Welch’s Two Sample
T-tests
\(t = \frac{(\bar x - \bar y) - \mu_0}{\sqrt{\frac{s_x^2}{n_x} + \frac{s_y^2}{n_y}}}\)
and Student’s t-distribution.
Are the means equal, or difference equal to \(\mu_0\)?
data_x = c(5.2, 2.1, 4.0, 6.3, 7.2, 8.2)
data_y = c(1.4, 6.2, 3.4, 5.9,
4.9, 7.1, 5.5, 4.4)
t.test(data_x, data_y,
alternative = "two.sided",
mu = 0, conf.level = 0.95)
Pooled Two Sample
T-tests (Equal Variance)
\(t = \frac{(\bar x - \bar y) - \mu_0}{s_p \cdot \sqrt{\frac{1}{n_x}+\frac{1}{n_y}}}\),
where \(s_p = \sqrt{\frac{\left(n_x-1\right)s_{x}^2+\left(n_y-1\right)s_{y}^2}{n_x+n_y-2}}\)
and Student’s t-distribution.
Are the means equal, or difference equal to \(\mu_0\)?
data_x = c(14.0, 13.1, 16.8, 11.1,
9.6, 12.4, 13.6, 8.8)
data_y = c(16.3, 14.8, 6.9, 17.3, 7.3)
t.test(data_x, data_y, var.equal = TRUE,
alternative = "less",
mu = 0, conf.level = 0.95)
Paired Two Sample T-tests
(Matched Pairs)
\(t = \frac{\bar d - d_0}{s_d/ \sqrt{n}} = \frac{(\bar x - \bar y) - d_0}{s_d/ \sqrt{n}}\)
and Student’s t-distribution.
Is the mean of paired
x and y differences equal \(d_0\)?

data_x = c(8.0, 9.2, 9.1, 7.2, 9.8)
data_y = c(7.5, 5.1, 8.6, 12.7, 8.4)
t.test(data_x, data_y, paired = TRUE,
alternative = "two.sided",
mu = 0, conf.level = 0.95)
Two Proportions Tests
(with Z & Chi-squared)
\(z = \frac{(\frac{x_1}{n_1}-\frac{x_2}{n_2})}{\sqrt {\hat p(1-\hat p) \left(\frac{1}{n_1} + \frac{1}{n_2} \right)}}\),
where \(\hat p = \frac{x_1 + x_2}{n_1 + n_2}\)
\(\text{or} \quad \chi^2_1=\sum_{i,j=1}^{2}\frac{(O_{ij}-E_{ij})^2}{E_{ij}}\)
and standard normal distribution
or chi-squared distribution.
Are the proportions equal?
prop.test(c(x1 = 22, x2 = 33),
c(n1 = 50, n2 = 60),
alternative = "two.sided",
conf.level = 0.95, correct = FALSE)
Pearson’s Correlation
Coefficient
\(r = \frac{\sum ^n _{i=1}(x_i - \bar{x})(y_i - \bar{y})}{\sqrt{\sum ^n _{i=1}(x_i - \bar{x})^2} \sqrt{\sum ^n _{i=1}(y_i - \bar{y})^2}}\),
\(t = \frac{r}{\sqrt{\frac{1 - r^2}{n-2}}}\)
and Student’s t-distribution.
Is there a linear correlation
between x and y?

data_x = c(8.2, 11.9, 11.1, 10.6, 9.9)
data_y = c(9.5, 10.2, 8.1, 13.4, 8.8)
cor.test(data_x, data_y, method = "pearson",
alternative = "two.sided",
conf.level = 0.95)
Two Variances F-tests \(F = \frac{s^2_1/\sigma^2_1}{s^2_2/\sigma^2_2}\),
if \(\sigma^2_1 = \sigma^2_2\), \(F = \frac{s^2_1}{s^2_2}\)
and F-distribution.
Are the variances equal, or ratio equal to \(r_0\)?
data1 = c(13.0, 9.8, 10.4, 10.5, 10.3)
data2 = c(9.9, 12.3, 12.5, 7.7,
11.1, 9.0, 8.1, 12.3)
var.test(data1, data2, ratio = 1,
alternative = "two.sided",
conf.level = 0.95)
Two-way ANOVA \(F = \frac{\tt{mean \;square \;error \;of \;factor}}{\tt{mean \;square \;error \;of \;residuals}}\)
and F-distribution.
Are there effects from the factors?
y = c(5.2, 3.1, 5.0, 7.4, 2.8, 7.2,
2.0, 4.7, 4.8, 6.9, 7.0, 5.1,
8.5, 8.2, 6.9, 5.3, 5.1, 5.3)
factorA = c(rep("A1", 6),
rep("A2", 6),
rep("A3", 6))
factorB = c(rep("B1", 3), rep("B2", 3),
rep("B1", 3), rep("B2", 3),
rep("B1", 3), rep("B2", 3))
anova(lm(y ~ factorA + factorB))
# For interaction effect
anova(lm(y ~ factorA + factorB + factorA:factorB))
Chi-squared
Contingency Table
\(\chi^2=\sum_{ij}\frac{(O_{ij}-E_{ij})^2}{E_{ij}}\)
and chi-squared distribution.
Are the row and column variables independent?
Are the populations homogeneous?
data = rbind(TrtA = c(Y = 40, N = 45),
TrtB = c(Y = 50, N = 55))
chisq.test(data, correct = FALSE)
Fisher’s Exact
Contingency Table
The 2x2 cell values
and hypergeometric distribution.
Are the row and column variables independent?
new = c(off = 7, on = 12)
old = c(off = 12, on = 15)
fisher.test(rbind(new, old),
alternative = "two.sided",
conf.level = 0.95)


3 Non-parametric Tests

This section has non-parametric tests and intervals. See the page on each test for more details.

Table of Tests and Intervals in R
Non-parametric Tests
Test Formula Example
Sign Tests for
One Sample
\(S = \sum_{i=1}^n I_{(x_i-m_0)>0}\),
& binomial distribution.
Is the median equal to \(m_0\)?
install.packages("BSDA")
library(BSDA)
data = c(1.4, 0.6, -0.5, 1.7, -1.3, 0.6)
SIGN.test(data, alternative = "two.sided",
md = 0, conf.level = 0.95)
Sign Tests for
Paired Samples
\(S = \sum_{i=1}^n I_{([x_i-y_i]-m_0)>0}\),
& binomial distribution.
Is the median of paired x and y
differences equal to \(m_0\)?

install.packages("BSDA")
library(BSDA)
data_x = c(5.4, 8.1, 6.9, 6.4, 5.7)
data_y = c(7.7, 6.1, 9.4, 7.0, 5.7)
SIGN.test(data_x, data_y,
alternative = "two.sided",
md = 0, conf.level = 0.95)
Wilcoxon Rank-Sum
(Mann–Whitney U) Tests
\(R_i\) is the rank of \(x_i - m_0\),
\(W = \sum_{i=1}^{n_x} R_i - \frac{n_x(n_x+1)}{2}\),
\(z = \frac{W - \frac{n_x n_y}{2}}{\sqrt{\frac{n_x n_y}{12}\left((n + 1) - \frac{\sum_{k=1}^{T}(t_k^3-t_k)}{n(n-1)} \right)}}\)
and standard normal distribution.
Are the medians equal, or difference equal to \(m_0\)?
data_x = c(4.7, 4.1, 4.2, 3.1, 3.8)
data_y = c(4.5, 3.7, 5.1, 5.7,
3.9, 5.0, 4.6, 4.3)
wilcox.test(data_x, data_y,
mu = 0, alternative = "two.sided",
conf.int = TRUE, conf.level = 0.95)
Wilcoxon Signed-Rank
Tests for One Sample
\(R_i\) is the rank of \(|x_i - m_0|\),
\(V = \sum_{i=1}^N R_i \cdot I_{(x_i-m_0)>0}\),
with \(t_k\) as the number of tied values for
set \(k\) that are tied at a particular value,
\(z = \frac{V - \frac{n(n+1)}{4}}{\sqrt{\frac{n(n + 1)(2n + 1)}{24}-\frac{\sum_{k=1}^{T}(t_k^3-t_k)}{48}}}\)
and standard normal distribution.
Is the median equal to \(m_0\)?
data = c(0.3, 0.5, -1.1, -0.6, 1.3, 0.4)
wilcox.test(data, alternative = "two.sided",
mu = 0,
conf.int = TRUE, conf.level = 0.95)
Wilcoxon Signed-Rank
Tests for Paired Samples
\(R_i\) is the rank of \(|(x_i - y_i) - m_0|\),
\(V = \sum_{i=1}^N R_i \cdot I_{([x_i-y_i]-m_0)>0}\),
with \(t_k\) as the number of tied values for
set \(k\) that are tied at a particular value,
\(z = \frac{V - \frac{n(n+1)}{4}}{\sqrt{\frac{n(n + 1)(2n + 1)}{24}-\frac{\sum_{k=1}^{T}(t_k^3-t_k)}{48}}}\)
and standard normal distribution.
Is the median of paired x and y
differences equal to \(m_0\)?

data_x = c(4.1, 4.5, 3.8, 5.2, 2.6)
data_y = c(2.6, 3.3, 2.4, 4.8, 6.0)
wilcox.test(data_x, data_y,
alternative = "two.sided",
mu = 0, paired = TRUE,
conf.int = TRUE, conf.level = 0.95)
Spearman’s Rank
Correlation Coefficient
Tests
\(r(x_i)\) and \(r(y_i)\) are ranks of \(x_i\) and \(y_i\),
\(r_s = \frac{\sum ^n _{i=1}[r(x_i) - \bar r(x)][r(y_i) - \bar r(y)]}{\sqrt{\sum ^n _{i=1}[r(x_i) - \bar r(x)]^2} \sqrt{\sum ^n _{i=1}[r(y_i) - \bar r(y)]^2}}\),
\(t = \frac{r_s}{\sqrt{\frac{1 - r_s^2}{n-2}}}\)
and Student’s t-distribution.
Is there a rank correlation between x and y?
data_x = c(3.5, 2.4, 2.8, 2.1, 1.8)
data_y = c(6.9, 7.2, 8.2, 7.3, 6.8)
cor.test(data_x, data_y,
alternative = "two.sided",
method = "spearman")
Kruskal-Wallis Tests \(\bar r_{j\cdot}\) is the average rank for group \(j\),
\(t_i\) is the number of tied values for set \(i\)
that are tied at a particular value,
\(H = \frac{\frac{12}{N(N+1)} \sum_{j=1}^G n_j \bar r_{j\cdot}^2 - 3(N+1)}{1-\frac{\sum_{i=1}^T (t_i^3-t_i)}{N^3-N}}\)
and chi-squared distribution.
Are the group medians equal?
x = c(17.9, 20.3, 20.4, 20.8, 19.2,
17.8, 22.6, 19.2, 18.4, 19.4, 19.3,
22.1, 20.3, 21.4, 19.1)
groups = c("A", "A", "A", "A", "A",
"B", "B", "B", "B", "B", "B",
"C", "C", "C", "C")
kruskal.test(x ~ groups)
Kolmogorov-Smirnov Tests \(D_n= \sup_x |F_n(x)-F(x)|\)
\(D_{n,m}=\sup_x |F_{1,n}(x)-F_{2,m}(x)|\)
Is the sample from the specified distribution?
Are the samples from the same distribution?
sample1 = c(4.1, 5.9, 5.2, 3.9, 3.6, 7.1, 4.3)
sample2 = c(3.1, 5.5, 5.3, 3.6, 3.1, 4.9, 6.1)
ks.test(sample1, pnorm, mean = 5, sd = 1)
ks.test(sample1, sample2)
Tests for Normal Distribution Histogram
Quantile-Quantile plot
Shapiro-Wilk test
Kolmogorov-Smirnov test
Is the sample from a normal distribution?
sample = c(4.6, 3.8, 6.7, 5.0, 6.1, 2.4,
4.5, 4.3, 3.9, 6.5, 3.6, 5.3, 5.8, 5.2)
hist(sample)
qqnorm(sample); qqline(sample)
shapiro.test(sample)
ks.test(sample, pnorm,
mean(sample), sd(sample))


Copyright © 2020 - 2024. All Rights Reserved by Stats Codes