T-test

Evaluating the means of one or two populations.

  • One sample t-test: evaluate whether a single group differs from a known value

    R: t.test | MATLAB: ttest | Python: ttest_1samp

    1
    2
    3
    4
    t.test(x, m,
    alternative = c("two.sided", "less", "greater"),
    var.equal = FALSE)
    # m is mean that you want to test
    1
    2
    [h,p] = ttest(x,m, 'Tail','right/left/both')
    % m is mean that you want to test
    1
    2
    3
    4
    5
    from scipy import stats
    stats.ttest_1samp(rvs, popmean=3,
    alternative={‘two-sided’, ‘less’, ‘greater’},
    nan_policy={‘propagate’, ‘omit’, ‘raise’})
    # popmean is mean that you want to test
  • Independent two sample t-test: evaluate whether two groups differ from each other

    R: t.test | MATLAB: ttest2 | Python: ttest_ind

    1
    2
    3
    t.test(x, y,
    alternative = c("two.sided", "less", "greater"),
    paired = FALSE, var.equal = FALSE)
    1
    h = ttest2(x,y, 'Tail','right/left/both')
    1
    2
    3
    4
    from scipy import stats
    stats.ttest_ind(rvs1, rvs2, equal_var=False,
    alternative={‘two-sided’, ‘less’, ‘greater’},
    nan_policy={‘propagate’, ‘omit’, ‘raise’})
  • Paired sample t-test: evaluate whether there is a significant difference in paired measurements

    R: t.test | MATLAB: ttest | Python: ttest_rel

    1
    2
    3
    t.test(x, y,
    alternative = c("two.sided", "less", "greater"),
    paired = FALSE)
    1
    [h,p] = ttest(x,y, 'Tail','right/left/both')
    1
    2
    3
    4
    from scipy import stats
    stats.ttest_rel(rvs1, rvs3,
    alternative={‘two-sided’, ‘less’, ‘greater’},
    nan_policy={‘propagate’, ‘omit’, ‘raise’})

ANOVA

  • One-way ANOVA

    To investigate whether different levels of a control variable have a significant effect on the observed variable.

    1
      
    1

    1

  • Two-way ANOVA

  • Repeated Measures ANOVA

Normality Test

Type: Shapiro–Wilk test | One-sample Kolmogorov-Smirnov test

Aim: To assess whether samples came from a normally distributed population.

R: shapiro.test | MATLAB: kstest | Python: normaltest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
res_aov <- aov(flipper_length_mm ~ species, data = dat)
shapiro.test(res_aov$residuals)

# check signal visually
par(mfrow = c(1, 2)) # combine plots

# histogram
hist(res_aov$residuals)

# QQ-plot
library(car)
qqPlot(res_aov$residuals,
id = FALSE # id = FALSE to remove point identification
)
1
[h,p] = kstest(x)
1
2
3
4
from scipy import stats
res = stats.normaltest(x)
res.statistic
res.pvalue

Equal Variance (Levene’s Test)

To assess the equality of variances for a variable calculated for two or more groups.

R: leveneTest | MATLAB: vartestn | Python: levene

1
2
3
# Levene's test
library(car)
leveneTest(flipper_length_mm ~ species, data = dat)
1
2
3
4
5
6
7
8
9
10
load examgrades
% Test the null hypothesis that the variances are equal across the five columns of data in the students’ exam grades matrix, grades.
vartestn(grades)

load carsmall
% Test the null hypothesis that the variances in miles per gallon (MPG) are equal across different model years.
vartestn(MPG,Model_Year)

% using Levene’s Test
p = vartestn(MPG,Model_Year,'TestType','LeveneAbsolute')
1
2
3
4
from scipy import stats
res = stats.levene(small_dose, medium_dose, large_dose)
res.statistic
res.pvalue

Chi-sq test

Fisher test

Wilcoxon test

  • Wilcoxon signed-rank test

  • Wilcoxon Rank Sum test

Kruskal test

Proportion test