-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
mamtawardhani
wants to merge
1
commit into
Codecademy:main
Choose a base branch
from
mamtawardhani:t-tests-statsmodel
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
content/python/concepts/statsmodels/terms/t-tests/t-tests.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
``` | ||
|
||
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. | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.