Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Comparing Two Groups: t-tests | Section
Applying Statistical Methods
Avsnitt 1. Kapitel 6
single

single

bookComparing Two Groups: t-tests

Svep för att visa menyn

When you want to determine if two groups differ significantly in their means, t-tests are a fundamental tool. There are two primary types of t-tests: the independent t-test (also known as the two-sample t-test) and the paired t-test.

The independent t-test is used when you are comparing the means of two unrelated groups, such as test scores from students in two different classes. The paired t-test is used when the two groups are related or matched in some way, such as measurements taken from the same individuals before and after an intervention.

Before performing a t-test, you should check several assumptions:

  • The data should be approximately normally distributed in each group;
  • The data should be measured at the interval or ratio level;
  • For the independent t-test, the variances of the two groups should be equal (homogeneity of variance).

In Python, you can use functions from the scipy.stats module to perform these tests. The ttest_ind function performs the independent t-test, and ttest_rel is used for the paired t-test. When interpreting the results, pay close attention to the p-value: if it is less than your chosen significance level (commonly 0.05), you can conclude that there is a statistically significant difference between the group means.

A low p-value suggests that the observed difference is unlikely to have occurred by random chance, while a high p-value indicates insufficient evidence to claim a difference.

12345678910111213141516
import numpy as np from scipy.stats import ttest_ind, ttest_rel # Independent t-test example group_a = np.array([23, 21, 19, 24, 20]) group_b = np.array([30, 29, 33, 35, 31]) t_stat_ind, p_value_ind = ttest_ind(group_a, group_b, equal_var=True) print("Independent t-test statistic:", t_stat_ind) print("Independent t-test p-value:", p_value_ind) # Paired t-test example before = np.array([5.1, 5.5, 6.0, 5.8, 5.3]) after = np.array([5.7, 6.0, 6.5, 6.2, 5.9]) t_stat_rel, p_value_rel = ttest_rel(before, after) print("Paired t-test statistic:", t_stat_rel) print("Paired t-test p-value:", p_value_rel)
copy
Uppgift

Svep för att börja koda

Given two pairs of datasets, determine if there is a significant difference between their means utilizing the appropriate t-test from scipy.stats in the global scope.

  • Scenario 1: the arrays data_a and data_b represent independent samples. Utilize the ttest_ind function and assign the resulting p-value to the variable p_val_ind.
  • Scenario 2: the arrays data_before and data_after represent paired (dependent) samples. Utilize the ttest_rel function and assign the resulting p-value to the variable p_val_paired.

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 6
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

some-alt