Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Term Entry] Python statsmodels: t-tests #5994

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions content/python/concepts/statsmodels/terms/t-tests/t-tests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
---
Title: 't-tests'
Description: 'Tests whether the means of one or two groups are significantly different under specified assumptions.'
Subjects:
- 'Machine Learning'
- 'Data Science'
Tags:
- 'Statistics'
- 'Properties'
- 'Models'
- 'Data'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
---

The **t-test** in Statsmodels is a statistical method to determine whether the means of one or two groups differ significantly. It is commonly used to compare the mean of a sample to a specified value, the means of two independent groups, or the means of paired data.

## Syntax

```pseudo
statsmodels.stats.weightstats.ttest_ind(x1, x2, alternative='two-sided', usevar='pooled', weights=(None, None))
```

- `x1`, `x2`: Arrays containing sample data for the groups being compared.
- `alternative`: The hypothesis to test. Options include:
- `'two-sided'`: Default, tests for any difference.
- `'larger'`: Tests if the mean of `x1` is greater than `x2`.
- `'smaller'`: Tests if the mean of `x1` is less than `x2`.
- `usevar`: Assumptions about variance. Options include:
- `'pooled'`: Default, assumes equal variance.
- `'unequal'`: Does not assume equal variance.
- `weights`: A tuple specifying weights for `x1` and `x2`, used in weighted t-tests.

## Example

In this example, a one-sample t-test is performed to determine whether the mean of a sample dataset is significantly different from _10_:

```py
import numpy as np
from statsmodels.stats.weightstats import ttest_ind

# Sample data
data = np.array([9.5, 10.1, 9.8, 10.2, 9.9, 10.0, 9.7])

# Hypothesized population mean
population_mean = 10

# Perform t-test
t_stat, p_value, df = ttest_ind(
data,
np.full(len(data), population_mean),
alternative='two-sided'
)

# Output results
print(f"t-statistic: {t_stat}")
print(f"P-value: {p_value}")
print(f"Degrees of freedom: {df}")

# Interpretation
alpha = 0.05
if p_value < alpha:
print("Reject the null hypothesis: The sample mean is significantly different from 10.")
else:
print("Fail to reject the null hypothesis: No significant difference from 10.")
Comment on lines +40 to +66
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @mamtawardhani, can you please check if this code is correct? The description says the code is trying to perform a one-sample test, but ttest_ind is used for independent two-sample tests.

```

The code above generates the following ouput:

```shell
t-statistic: -1.2545000963743562
P-value: 0.23354509216171446
Degrees of freedom: 12.0
Fail to reject the null hypothesis: No significant difference from 10.
```
Loading