Loading [MathJax]/jax/output/HTML-CSS/jax.js
Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Mathematics LibreTexts

1.6.9.1.4: Testing...

( \newcommand{\kernel}{\mathrm{null}\,}\)

The significance of difference between means for paired parametric data (t-test for paired data):

Code 1.6.9.1.4.1 (R):

data <- read.table("data/bugs.txt", h=TRUE)
t.test(data$WEIGHT, data$LENGTH, paired=TRUE)

... t-test for independent data:

Code 1.6.9.1.4.2 (R):

data <- read.table("data/bugs.txt", h=TRUE)
t.test(data$WEIGHT, data$LENGTH, paired=FALSE)

(Last example is for learning purpose only because our data is paired since every row corresponds with one animal. Also, "paired=FALSE" is the default for the t.test(), therefore one can skip it.)

Here is how to compare values of one character between two groups using formula interface:

Code 1.6.9.1.4.3 (R):

data <- read.table("data/bugs.txt", h=TRUE)
t.test(data$WEIGHT ~ data$SEX)

Formula was used because our weight/sex data is in the long form:

Code 1.6.9.1.4.4 (R):

data <- read.table("data/bugs.txt", h=TRUE)
data[, c("WEIGHT", "SEX")]

Convert weight/sex data into the short form and test:

Code 1.6.9.1.4.5 (R):

data <- read.table("data/bugs.txt", h=TRUE)
data3 <- unstack(data[, c("WEIGHT", "SEX")])
t.test(data3[[1]], data3[[2]])

(Note that test results are exactly the same. Only format was different.)

If the p-value is equal or less than 0.05, then the difference is statistically supported. R does not require you to check if the dispersion is the same.

Nonparametric Wilcoxon test for the differences:

Code 1.6.9.1.4.6 (R):

data <- read.table("data/bugs.txt", h=TRUE)
wilcox.test(data$WEIGHT, data$LENGTH, paired=TRUE)

One-way test for the differences between three and more groups (the simple variant of ANOVA, analysis of variation):

Code 1.6.9.1.4.7 (R):

data <- read.table("data/bugs.txt", h=TRUE)
wilcox.test(data$WEIGHT ~ data$SEX)

Which pair(s) are significantly different?

Code 1.6.9.1.4.8 (R):

data <- read.table("data/bugs.txt", h=TRUE)
pairwise.t.test(data$WEIGHT, data$COLOR, p.adj="bonferroni")

(We used Bonferroni correction for multiple comparisons.)

Nonparametric Kruskal-Wallis test for differences between three and more groups:

Code 1.6.9.1.4.9 (R):

data <- read.table("data/bugs.txt", h=TRUE)
kruskal.test(data$WEIGHT ~ data$COLOR)

Which pairs are significantly different in this nonparametric test?

Code 1.6.9.1.4.10 (R):

data <- read.table("data/bugs.txt", h=TRUE)
pairwise.wilcox.test(data$WEIGHT, data$COLOR)

The significance of the correspondence between categorical data (nonparametric Pearson chi-squared, or χ2 test):

Code 1.6.9.1.4.11 (R):

data <- read.table("data/bugs.txt", h=TRUE)
chisq.test(data$COLOR, data$SEX)

The significance of proportions (nonparametric):

Code 1.6.9.1.4.12 (R):

data <- read.table("data/bugs.txt", h=TRUE)
prop.test(sum(data$SEX), length(data$SEX), 0.5)

(Here we checked if this is true that the proportion of male is different from 50%.)

The significance of linear correlation between variables, parametric way (Pearson correlation test):

Code 1.6.9.1.4.13 (R):

data <- read.table("data/bugs.txt", h=TRUE)
cor.test(data$WEIGHT, data$LENGTH, method="pearson")

... and nonparametric way (Spearman’s correlation test):

Code 1.6.9.1.4.14 (R):

data <- read.table("data/bugs.txt", h=TRUE)
cor.test(data$WEIGHT, data$LENGTH, method="spearman")

The significance (and many more) of the linear model describing relation of one variable on another:

Code 1.6.9.1.4.15 (R):

data <- read.table("data/bugs.txt", h=TRUE)
summary(lm(data$LENGTH ~ data$SEX))

... and analysis of variation (ANOVA) based on the linear model:

Code 1.6.9.1.4.16 (R):

data <- read.table("data/bugs.txt", h=TRUE)
aov(lm(data$LENGTH ~ data$SEX))

This page titled 1.6.9.1.4: Testing... is shared under a Public Domain license and was authored, remixed, and/or curated by Alexey Shipunov.

Support Center

How can we help?