# Chapter 12: Hypothesis Testing

## 12.3 ANOVA (Analysis of Variance)

### 12.3.1 What is ANOVA?

ANOVA, which stands for Analysis of Variance, is a statistical method used to compare the means of three or more independent (unrelated) groups. It is often used when we want to determine if there are any significant differences between the means of these groups. While the t-test is used to compare two means, ANOVA is a more suitable option when we want to compare more than two means. This is because it allows us to test for differences between several groups at once.

As mentioned earlier, the null hypothesis in an ANOVA test is that all group means are equal. However, the alternative hypothesis is that at least one group mean is different from the others. This means that ANOVA is a powerful tool for detecting differences between groups, which makes it a valuable tool for researchers who are trying to understand the effects of different variables on a particular outcome.

Furthermore, ANOVA allows us to examine the variance within and between groups, which provides additional insights into the data. By understanding the sources of variance, we can better understand the factors that are contributing to the differences between groups. This can help us identify potential areas for improvement, and can also help us develop more effective strategies for addressing these differences.

In summary, ANOVA is a powerful statistical method that allows us to compare the means of multiple groups at once. By examining the variance within and between groups, we can gain a better understanding of the factors that are contributing to differences between groups. This can provide valuable insights that can help us develop more effective strategies for addressing these differences and improving outcomes.

### 12.3.2 Why Use ANOVA?

When deciding whether to use ANOVA or multiple t-tests, it is important to consider several factors. One key advantage of ANOVA is that it provides a single, consistent test for analyzing multiple groups. This can be especially helpful when dealing with large datasets or with many different groups.

In addition, using ANOVA can help to mitigate the risk of a Type I error, which can occur when running multiple t-tests. As we discussed in the previous section, the more t-tests you run, the higher the risk of making a Type I error. By analyzing all the groups simultaneously, ANOVA can help to reduce this risk.

However, it's important to note that ANOVA may not always be the best choice for every situation. For example, if you have a small number of groups with clear differences between them, it may be more appropriate to use individual t-tests to analyze the results.

ANOVA assumes that the variances of the groups being compared are equal. If this assumption is not met, the results of the ANOVA test may not be accurate. Therefore, it's important to carefully consider the specific characteristics of your dataset before deciding which statistical test to use.

### 12.3.3 One-way ANOVA

The Analysis of Variance (ANOVA) is a statistical tool that is commonly used to test if there are any significant differences among the means of three or more independent (unrelated) groups. In this regard, the simplest form of ANOVA is One-way ANOVA, which is used to compare means across different groups.

The hypothesis for one-way ANOVA is as follows:

- Null Hypothesis (H_0): The means of the different groups are the same, and any observed differences are due to chance alone.
- Alternative Hypothesis (H_a): At least one group mean is different from the others, and the observed differences are not due to chance.

It should be noted that ANOVA is a robust statistical tool that can be used to test the significance of differences across groups while controlling for other variables. Additionally, ANOVA can be extended to more complex designs, including factorial ANOVA and repeated measures ANOVA, among others. Overall, ANOVA is an essential tool in statistical analysis, which can help researchers draw meaningful conclusions from their data.

### 13.3.4 Example: One-way ANOVA in Python

Let's consider a simple example. Suppose we have test scores from students in three different classes: A, B, and C, and we want to know if one class outperforms the others.

Here's how you could conduct a One-way ANOVA in Python using the `scipy.stats`

library:

`import scipy.stats as stats`

import numpy as np

# Generating some example data

class_a = np.random.normal(70, 10, 30)

class_b = np.random.normal(75, 10, 30)

class_c = np.random.normal(80, 10, 30)

# Perform one-way ANOVA

F, p = stats.f_oneway(class_a, class_b, class_c)

# Interpret results

alpha = 0.05 # Significance level

print(f'F-statistic: {F}, p-value: {p}')

if p < alpha:

print('One or more groups significantly differ from each other.')

else:

print('There is no significant difference between the groups.')

In this example, a low p-value indicates that we should reject the null hypothesis, and that at least one of the class means significantly differs from the others.

There is much more to explore in the world of ANOVA, including advanced topics such as two-way ANOVA, repeated measures ANOVA, and more. Let's dive in!

### 12.3.5 Two-way ANOVA

One-way ANOVA is used to test differences between groups that are categorized on one dimension, while two-way ANOVA is used when dealing with groups that are categorized on two independent variables. For instance, let's say you are analyzing the test scores of students in a school.

Using two-way ANOVA, you can examine how each factor (grade level and subject) impacts the test scores and determine if there is any interaction between the two factors. Furthermore, you can use this analysis to identify any trends or patterns that may emerge in the data and to draw more detailed conclusions about the variables at play.

Here's a quick Python example using the `statsmodels`

library for a two-way ANOVA:

`import statsmodels.api as sm`

from statsmodels.formula.api import ols

import pandas as pd

# Example data: test scores categorized by grade and subject

data = {

'Score': [89, 90, 92, 88, 85, 76, 81, 77, 82, 90, 92, 91, 93, 88, 85],

'Grade': ['9th', '9th', '9th', '9th', '9th', '10th', '10th', '10th', '10th', '10th', '11th', '11th', '11th', '11th', '11th'],

'Subject': ['Math', 'Science', 'English', 'History', 'Art', 'Math', 'Science', 'English', 'History', 'Art', 'Math', 'Science', 'English', 'History', 'Art']

}

df = pd.DataFrame(data)

# Fit the model

model = ols('Score ~ C(Grade) + C(Subject) + C(Grade):C(Subject)', data=df).fit()

# Perform the ANOVA

anova_table = sm.stats.anova_lm(model, typ=2)

print(anova_table)

In this example, `Score`

is our dependent variable, while `Grade`

and `Subject`

are our independent variables. We're interested in finding out how these variables and their interaction affect the test score.

### 12.3.6 Repeated Measures ANOVA

If you're dealing with repeated measures over time or some other form of related groups, repeated measures ANOVA might be a useful statistical technique for your analysis. This allows you to compare the same group at different time points or under different conditions.

For example, if you were measuring a group of patients' heart rates before, during, and after exercise, you could use repeated measures ANOVA to determine if there are statistically significant changes at each time point. In addition, this method can also help you to identify any potential interactions between the time points and other factors that you may have measured, such as age, gender, or medication usage.

By accounting for these variables, you can gain a deeper understanding of the underlying effects of the intervention you are studying. Furthermore, repeated measures ANOVA can be useful in situations where you have missing data, as it can help you to impute the missing values and still obtain valid results. Overall, repeated measures ANOVA is a powerful tool for analyzing longitudinal data and can provide valuable insights into the changes that occur over time in your study population.

Sometimes the same subjects are used for each treatment (i.e., repeated measures), like in a longitudinal study. In these cases, the variance within the groups is not a good estimator of the variance for the errors, so we use Repeated Measures ANOVA.

In Python, you can use the

class from the **AnovaRM**

library:**statsmodels**

`import statsmodels.api as sm`

import pandas as pd

# Sample data: Patient's heart rate measured at different times

data = {

'Patient': ['1', '1', '1', '2', '2', '2', '3', '3', '3'],

'Time': ['Before', 'During', 'After', 'Before', 'During', 'After', 'Before', 'During', 'After'],

'HeartRate': [70, 80, 75, 72, 85, 78, 68, 79, 76]

}

df = pd.DataFrame(data)

# Perform Repeated Measures ANOVA

anovarm = sm.stats.AnovaRM(df, 'HeartRate', 'Patient', within=['Time'])

fit = anovarm.fit()

print(fit.summary())

### 12.3.7 Assumptions of ANOVA

As you know, ANOVA (analysis of variance) is a statistical test that is widely used in research to compare means across two or more groups. However, it is important to note that ANOVA comes with its own set of assumptions that must be met in order for it to be accurate.

One of the main assumptions is that the data is normally distributed. This means that the data should form a bell-shaped curve when plotted on a graph. Another important assumption is homogeneity of variances, which means that the variance within each group should be roughly equal. Finally, ANOVA assumes that the observations are independent of one another.

If any of these assumptions are violated, it may be necessary to transform the data or use non-parametric tests instead. Transforming the data involves applying a mathematical function to the values in order to change the shape of the distribution. Non-parametric tests, on the other hand, do not make any assumptions about the underlying distribution of the data, but they may have less power to detect differences between groups.

Example:

`from scipy import stats`

# Test for normality

_, p_value_norm = stats.shapiro(df['HeartRate'])

# Test for homoscedasticity

_, p_value_levene = stats.levene(

df['HeartRate'][df['Time'] == 'Before'],

df['HeartRate'][df['Time'] == 'During'],

df['HeartRate'][df['Time'] == 'After']

)

print("Shapiro-Wilk p-value:", p_value_norm)

print("Levene p-value:", p_value_levene)

It is important to remember that in statistics, the devil is often in the assumptions. Therefore, it is crucial to be aware of the assumptions you are making and how to validate them in order to draw valid conclusions. You may want to consider conducting sensitivity analyses to test the robustness of your findings to different assumptions.

Additionally, it may be helpful to examine the distribution of your data and check for outliers, which can greatly impact the results of your analysis. By taking these steps, you can ensure that your conclusions are based on sound statistical principles.

Now! Let's dive into some practical exercises to cement your understanding of hypothesis testing and ANOVA. These exercises will not only help you grasp the theoretical underpinnings but also give you a hands-on experience in Python programming.

## 12.3 ANOVA (Analysis of Variance)

### 12.3.1 What is ANOVA?

ANOVA, which stands for Analysis of Variance, is a statistical method used to compare the means of three or more independent (unrelated) groups. It is often used when we want to determine if there are any significant differences between the means of these groups. While the t-test is used to compare two means, ANOVA is a more suitable option when we want to compare more than two means. This is because it allows us to test for differences between several groups at once.

As mentioned earlier, the null hypothesis in an ANOVA test is that all group means are equal. However, the alternative hypothesis is that at least one group mean is different from the others. This means that ANOVA is a powerful tool for detecting differences between groups, which makes it a valuable tool for researchers who are trying to understand the effects of different variables on a particular outcome.

Furthermore, ANOVA allows us to examine the variance within and between groups, which provides additional insights into the data. By understanding the sources of variance, we can better understand the factors that are contributing to the differences between groups. This can help us identify potential areas for improvement, and can also help us develop more effective strategies for addressing these differences.

In summary, ANOVA is a powerful statistical method that allows us to compare the means of multiple groups at once. By examining the variance within and between groups, we can gain a better understanding of the factors that are contributing to differences between groups. This can provide valuable insights that can help us develop more effective strategies for addressing these differences and improving outcomes.

### 12.3.2 Why Use ANOVA?

When deciding whether to use ANOVA or multiple t-tests, it is important to consider several factors. One key advantage of ANOVA is that it provides a single, consistent test for analyzing multiple groups. This can be especially helpful when dealing with large datasets or with many different groups.

In addition, using ANOVA can help to mitigate the risk of a Type I error, which can occur when running multiple t-tests. As we discussed in the previous section, the more t-tests you run, the higher the risk of making a Type I error. By analyzing all the groups simultaneously, ANOVA can help to reduce this risk.

However, it's important to note that ANOVA may not always be the best choice for every situation. For example, if you have a small number of groups with clear differences between them, it may be more appropriate to use individual t-tests to analyze the results.

ANOVA assumes that the variances of the groups being compared are equal. If this assumption is not met, the results of the ANOVA test may not be accurate. Therefore, it's important to carefully consider the specific characteristics of your dataset before deciding which statistical test to use.

### 12.3.3 One-way ANOVA

The Analysis of Variance (ANOVA) is a statistical tool that is commonly used to test if there are any significant differences among the means of three or more independent (unrelated) groups. In this regard, the simplest form of ANOVA is One-way ANOVA, which is used to compare means across different groups.

The hypothesis for one-way ANOVA is as follows:

- Null Hypothesis (H_0): The means of the different groups are the same, and any observed differences are due to chance alone.
- Alternative Hypothesis (H_a): At least one group mean is different from the others, and the observed differences are not due to chance.

It should be noted that ANOVA is a robust statistical tool that can be used to test the significance of differences across groups while controlling for other variables. Additionally, ANOVA can be extended to more complex designs, including factorial ANOVA and repeated measures ANOVA, among others. Overall, ANOVA is an essential tool in statistical analysis, which can help researchers draw meaningful conclusions from their data.

### 13.3.4 Example: One-way ANOVA in Python

Let's consider a simple example. Suppose we have test scores from students in three different classes: A, B, and C, and we want to know if one class outperforms the others.

Here's how you could conduct a One-way ANOVA in Python using the `scipy.stats`

library:

`import scipy.stats as stats`

import numpy as np

# Generating some example data

class_a = np.random.normal(70, 10, 30)

class_b = np.random.normal(75, 10, 30)

class_c = np.random.normal(80, 10, 30)

# Perform one-way ANOVA

F, p = stats.f_oneway(class_a, class_b, class_c)

# Interpret results

alpha = 0.05 # Significance level

print(f'F-statistic: {F}, p-value: {p}')

if p < alpha:

print('One or more groups significantly differ from each other.')

else:

print('There is no significant difference between the groups.')

In this example, a low p-value indicates that we should reject the null hypothesis, and that at least one of the class means significantly differs from the others.

There is much more to explore in the world of ANOVA, including advanced topics such as two-way ANOVA, repeated measures ANOVA, and more. Let's dive in!

### 12.3.5 Two-way ANOVA

One-way ANOVA is used to test differences between groups that are categorized on one dimension, while two-way ANOVA is used when dealing with groups that are categorized on two independent variables. For instance, let's say you are analyzing the test scores of students in a school.

Using two-way ANOVA, you can examine how each factor (grade level and subject) impacts the test scores and determine if there is any interaction between the two factors. Furthermore, you can use this analysis to identify any trends or patterns that may emerge in the data and to draw more detailed conclusions about the variables at play.

Here's a quick Python example using the `statsmodels`

library for a two-way ANOVA:

`import statsmodels.api as sm`

from statsmodels.formula.api import ols

import pandas as pd

# Example data: test scores categorized by grade and subject

data = {

'Score': [89, 90, 92, 88, 85, 76, 81, 77, 82, 90, 92, 91, 93, 88, 85],

'Grade': ['9th', '9th', '9th', '9th', '9th', '10th', '10th', '10th', '10th', '10th', '11th', '11th', '11th', '11th', '11th'],

'Subject': ['Math', 'Science', 'English', 'History', 'Art', 'Math', 'Science', 'English', 'History', 'Art', 'Math', 'Science', 'English', 'History', 'Art']

}

df = pd.DataFrame(data)

# Fit the model

model = ols('Score ~ C(Grade) + C(Subject) + C(Grade):C(Subject)', data=df).fit()

# Perform the ANOVA

anova_table = sm.stats.anova_lm(model, typ=2)

print(anova_table)

In this example, `Score`

is our dependent variable, while `Grade`

and `Subject`

are our independent variables. We're interested in finding out how these variables and their interaction affect the test score.

### 12.3.6 Repeated Measures ANOVA

If you're dealing with repeated measures over time or some other form of related groups, repeated measures ANOVA might be a useful statistical technique for your analysis. This allows you to compare the same group at different time points or under different conditions.

For example, if you were measuring a group of patients' heart rates before, during, and after exercise, you could use repeated measures ANOVA to determine if there are statistically significant changes at each time point. In addition, this method can also help you to identify any potential interactions between the time points and other factors that you may have measured, such as age, gender, or medication usage.

By accounting for these variables, you can gain a deeper understanding of the underlying effects of the intervention you are studying. Furthermore, repeated measures ANOVA can be useful in situations where you have missing data, as it can help you to impute the missing values and still obtain valid results. Overall, repeated measures ANOVA is a powerful tool for analyzing longitudinal data and can provide valuable insights into the changes that occur over time in your study population.

Sometimes the same subjects are used for each treatment (i.e., repeated measures), like in a longitudinal study. In these cases, the variance within the groups is not a good estimator of the variance for the errors, so we use Repeated Measures ANOVA.

In Python, you can use the

class from the **AnovaRM**

library:**statsmodels**

`import statsmodels.api as sm`

import pandas as pd

# Sample data: Patient's heart rate measured at different times

data = {

'Patient': ['1', '1', '1', '2', '2', '2', '3', '3', '3'],

'Time': ['Before', 'During', 'After', 'Before', 'During', 'After', 'Before', 'During', 'After'],

'HeartRate': [70, 80, 75, 72, 85, 78, 68, 79, 76]

}

df = pd.DataFrame(data)

# Perform Repeated Measures ANOVA

anovarm = sm.stats.AnovaRM(df, 'HeartRate', 'Patient', within=['Time'])

fit = anovarm.fit()

print(fit.summary())

### 12.3.7 Assumptions of ANOVA

As you know, ANOVA (analysis of variance) is a statistical test that is widely used in research to compare means across two or more groups. However, it is important to note that ANOVA comes with its own set of assumptions that must be met in order for it to be accurate.

One of the main assumptions is that the data is normally distributed. This means that the data should form a bell-shaped curve when plotted on a graph. Another important assumption is homogeneity of variances, which means that the variance within each group should be roughly equal. Finally, ANOVA assumes that the observations are independent of one another.

If any of these assumptions are violated, it may be necessary to transform the data or use non-parametric tests instead. Transforming the data involves applying a mathematical function to the values in order to change the shape of the distribution. Non-parametric tests, on the other hand, do not make any assumptions about the underlying distribution of the data, but they may have less power to detect differences between groups.

Example:

`from scipy import stats`

# Test for normality

_, p_value_norm = stats.shapiro(df['HeartRate'])

# Test for homoscedasticity

_, p_value_levene = stats.levene(

df['HeartRate'][df['Time'] == 'Before'],

df['HeartRate'][df['Time'] == 'During'],

df['HeartRate'][df['Time'] == 'After']

)

print("Shapiro-Wilk p-value:", p_value_norm)

print("Levene p-value:", p_value_levene)

It is important to remember that in statistics, the devil is often in the assumptions. Therefore, it is crucial to be aware of the assumptions you are making and how to validate them in order to draw valid conclusions. You may want to consider conducting sensitivity analyses to test the robustness of your findings to different assumptions.

Additionally, it may be helpful to examine the distribution of your data and check for outliers, which can greatly impact the results of your analysis. By taking these steps, you can ensure that your conclusions are based on sound statistical principles.

Now! Let's dive into some practical exercises to cement your understanding of hypothesis testing and ANOVA. These exercises will not only help you grasp the theoretical underpinnings but also give you a hands-on experience in Python programming.

## 12.3 ANOVA (Analysis of Variance)

### 12.3.1 What is ANOVA?

ANOVA, which stands for Analysis of Variance, is a statistical method used to compare the means of three or more independent (unrelated) groups. It is often used when we want to determine if there are any significant differences between the means of these groups. While the t-test is used to compare two means, ANOVA is a more suitable option when we want to compare more than two means. This is because it allows us to test for differences between several groups at once.

As mentioned earlier, the null hypothesis in an ANOVA test is that all group means are equal. However, the alternative hypothesis is that at least one group mean is different from the others. This means that ANOVA is a powerful tool for detecting differences between groups, which makes it a valuable tool for researchers who are trying to understand the effects of different variables on a particular outcome.

Furthermore, ANOVA allows us to examine the variance within and between groups, which provides additional insights into the data. By understanding the sources of variance, we can better understand the factors that are contributing to the differences between groups. This can help us identify potential areas for improvement, and can also help us develop more effective strategies for addressing these differences.

In summary, ANOVA is a powerful statistical method that allows us to compare the means of multiple groups at once. By examining the variance within and between groups, we can gain a better understanding of the factors that are contributing to differences between groups. This can provide valuable insights that can help us develop more effective strategies for addressing these differences and improving outcomes.

### 12.3.2 Why Use ANOVA?

When deciding whether to use ANOVA or multiple t-tests, it is important to consider several factors. One key advantage of ANOVA is that it provides a single, consistent test for analyzing multiple groups. This can be especially helpful when dealing with large datasets or with many different groups.

In addition, using ANOVA can help to mitigate the risk of a Type I error, which can occur when running multiple t-tests. As we discussed in the previous section, the more t-tests you run, the higher the risk of making a Type I error. By analyzing all the groups simultaneously, ANOVA can help to reduce this risk.

However, it's important to note that ANOVA may not always be the best choice for every situation. For example, if you have a small number of groups with clear differences between them, it may be more appropriate to use individual t-tests to analyze the results.

ANOVA assumes that the variances of the groups being compared are equal. If this assumption is not met, the results of the ANOVA test may not be accurate. Therefore, it's important to carefully consider the specific characteristics of your dataset before deciding which statistical test to use.

### 12.3.3 One-way ANOVA

The Analysis of Variance (ANOVA) is a statistical tool that is commonly used to test if there are any significant differences among the means of three or more independent (unrelated) groups. In this regard, the simplest form of ANOVA is One-way ANOVA, which is used to compare means across different groups.

The hypothesis for one-way ANOVA is as follows:

- Null Hypothesis (H_0): The means of the different groups are the same, and any observed differences are due to chance alone.
- Alternative Hypothesis (H_a): At least one group mean is different from the others, and the observed differences are not due to chance.

It should be noted that ANOVA is a robust statistical tool that can be used to test the significance of differences across groups while controlling for other variables. Additionally, ANOVA can be extended to more complex designs, including factorial ANOVA and repeated measures ANOVA, among others. Overall, ANOVA is an essential tool in statistical analysis, which can help researchers draw meaningful conclusions from their data.

### 13.3.4 Example: One-way ANOVA in Python

Let's consider a simple example. Suppose we have test scores from students in three different classes: A, B, and C, and we want to know if one class outperforms the others.

Here's how you could conduct a One-way ANOVA in Python using the `scipy.stats`

library:

`import scipy.stats as stats`

import numpy as np

# Generating some example data

class_a = np.random.normal(70, 10, 30)

class_b = np.random.normal(75, 10, 30)

class_c = np.random.normal(80, 10, 30)

# Perform one-way ANOVA

F, p = stats.f_oneway(class_a, class_b, class_c)

# Interpret results

alpha = 0.05 # Significance level

print(f'F-statistic: {F}, p-value: {p}')

if p < alpha:

print('One or more groups significantly differ from each other.')

else:

print('There is no significant difference between the groups.')

In this example, a low p-value indicates that we should reject the null hypothesis, and that at least one of the class means significantly differs from the others.

There is much more to explore in the world of ANOVA, including advanced topics such as two-way ANOVA, repeated measures ANOVA, and more. Let's dive in!

### 12.3.5 Two-way ANOVA

One-way ANOVA is used to test differences between groups that are categorized on one dimension, while two-way ANOVA is used when dealing with groups that are categorized on two independent variables. For instance, let's say you are analyzing the test scores of students in a school.

Using two-way ANOVA, you can examine how each factor (grade level and subject) impacts the test scores and determine if there is any interaction between the two factors. Furthermore, you can use this analysis to identify any trends or patterns that may emerge in the data and to draw more detailed conclusions about the variables at play.

Here's a quick Python example using the `statsmodels`

library for a two-way ANOVA:

`import statsmodels.api as sm`

from statsmodels.formula.api import ols

import pandas as pd

# Example data: test scores categorized by grade and subject

data = {

'Score': [89, 90, 92, 88, 85, 76, 81, 77, 82, 90, 92, 91, 93, 88, 85],

'Grade': ['9th', '9th', '9th', '9th', '9th', '10th', '10th', '10th', '10th', '10th', '11th', '11th', '11th', '11th', '11th'],

'Subject': ['Math', 'Science', 'English', 'History', 'Art', 'Math', 'Science', 'English', 'History', 'Art', 'Math', 'Science', 'English', 'History', 'Art']

}

df = pd.DataFrame(data)

# Fit the model

model = ols('Score ~ C(Grade) + C(Subject) + C(Grade):C(Subject)', data=df).fit()

# Perform the ANOVA

anova_table = sm.stats.anova_lm(model, typ=2)

print(anova_table)

In this example, `Score`

is our dependent variable, while `Grade`

and `Subject`

are our independent variables. We're interested in finding out how these variables and their interaction affect the test score.

### 12.3.6 Repeated Measures ANOVA

If you're dealing with repeated measures over time or some other form of related groups, repeated measures ANOVA might be a useful statistical technique for your analysis. This allows you to compare the same group at different time points or under different conditions.

For example, if you were measuring a group of patients' heart rates before, during, and after exercise, you could use repeated measures ANOVA to determine if there are statistically significant changes at each time point. In addition, this method can also help you to identify any potential interactions between the time points and other factors that you may have measured, such as age, gender, or medication usage.

By accounting for these variables, you can gain a deeper understanding of the underlying effects of the intervention you are studying. Furthermore, repeated measures ANOVA can be useful in situations where you have missing data, as it can help you to impute the missing values and still obtain valid results. Overall, repeated measures ANOVA is a powerful tool for analyzing longitudinal data and can provide valuable insights into the changes that occur over time in your study population.

Sometimes the same subjects are used for each treatment (i.e., repeated measures), like in a longitudinal study. In these cases, the variance within the groups is not a good estimator of the variance for the errors, so we use Repeated Measures ANOVA.

In Python, you can use the

class from the **AnovaRM**

library:**statsmodels**

`import statsmodels.api as sm`

import pandas as pd

# Sample data: Patient's heart rate measured at different times

data = {

'Patient': ['1', '1', '1', '2', '2', '2', '3', '3', '3'],

'Time': ['Before', 'During', 'After', 'Before', 'During', 'After', 'Before', 'During', 'After'],

'HeartRate': [70, 80, 75, 72, 85, 78, 68, 79, 76]

}

df = pd.DataFrame(data)

# Perform Repeated Measures ANOVA

anovarm = sm.stats.AnovaRM(df, 'HeartRate', 'Patient', within=['Time'])

fit = anovarm.fit()

print(fit.summary())

### 12.3.7 Assumptions of ANOVA

As you know, ANOVA (analysis of variance) is a statistical test that is widely used in research to compare means across two or more groups. However, it is important to note that ANOVA comes with its own set of assumptions that must be met in order for it to be accurate.

One of the main assumptions is that the data is normally distributed. This means that the data should form a bell-shaped curve when plotted on a graph. Another important assumption is homogeneity of variances, which means that the variance within each group should be roughly equal. Finally, ANOVA assumes that the observations are independent of one another.

If any of these assumptions are violated, it may be necessary to transform the data or use non-parametric tests instead. Transforming the data involves applying a mathematical function to the values in order to change the shape of the distribution. Non-parametric tests, on the other hand, do not make any assumptions about the underlying distribution of the data, but they may have less power to detect differences between groups.

Example:

`from scipy import stats`

# Test for normality

_, p_value_norm = stats.shapiro(df['HeartRate'])

# Test for homoscedasticity

_, p_value_levene = stats.levene(

df['HeartRate'][df['Time'] == 'Before'],

df['HeartRate'][df['Time'] == 'During'],

df['HeartRate'][df['Time'] == 'After']

)

print("Shapiro-Wilk p-value:", p_value_norm)

print("Levene p-value:", p_value_levene)

It is important to remember that in statistics, the devil is often in the assumptions. Therefore, it is crucial to be aware of the assumptions you are making and how to validate them in order to draw valid conclusions. You may want to consider conducting sensitivity analyses to test the robustness of your findings to different assumptions.

Additionally, it may be helpful to examine the distribution of your data and check for outliers, which can greatly impact the results of your analysis. By taking these steps, you can ensure that your conclusions are based on sound statistical principles.

Now! Let's dive into some practical exercises to cement your understanding of hypothesis testing and ANOVA. These exercises will not only help you grasp the theoretical underpinnings but also give you a hands-on experience in Python programming.

## 12.3 ANOVA (Analysis of Variance)

### 12.3.1 What is ANOVA?

### 12.3.2 Why Use ANOVA?

### 12.3.3 One-way ANOVA

The hypothesis for one-way ANOVA is as follows:

### 13.3.4 Example: One-way ANOVA in Python

Here's how you could conduct a One-way ANOVA in Python using the `scipy.stats`

library:

`import scipy.stats as stats`

import numpy as np

# Generating some example data

class_a = np.random.normal(70, 10, 30)

class_b = np.random.normal(75, 10, 30)

class_c = np.random.normal(80, 10, 30)

# Perform one-way ANOVA

F, p = stats.f_oneway(class_a, class_b, class_c)

# Interpret results

alpha = 0.05 # Significance level

print(f'F-statistic: {F}, p-value: {p}')

if p < alpha:

print('One or more groups significantly differ from each other.')

else:

print('There is no significant difference between the groups.')

### 12.3.5 Two-way ANOVA

Here's a quick Python example using the `statsmodels`

library for a two-way ANOVA:

`import statsmodels.api as sm`

from statsmodels.formula.api import ols

import pandas as pd

# Example data: test scores categorized by grade and subject

data = {

'Score': [89, 90, 92, 88, 85, 76, 81, 77, 82, 90, 92, 91, 93, 88, 85],

'Grade': ['9th', '9th', '9th', '9th', '9th', '10th', '10th', '10th', '10th', '10th', '11th', '11th', '11th', '11th', '11th'],

'Subject': ['Math', 'Science', 'English', 'History', 'Art', 'Math', 'Science', 'English', 'History', 'Art', 'Math', 'Science', 'English', 'History', 'Art']

}

df = pd.DataFrame(data)

# Fit the model

model = ols('Score ~ C(Grade) + C(Subject) + C(Grade):C(Subject)', data=df).fit()

# Perform the ANOVA

anova_table = sm.stats.anova_lm(model, typ=2)

print(anova_table)

`Score`

is our dependent variable, while `Grade`

and `Subject`

are our independent variables. We're interested in finding out how these variables and their interaction affect the test score.

### 12.3.6 Repeated Measures ANOVA

In Python, you can use the

class from the **AnovaRM**

library:**statsmodels**

`import statsmodels.api as sm`

import pandas as pd

# Sample data: Patient's heart rate measured at different times

data = {

'Patient': ['1', '1', '1', '2', '2', '2', '3', '3', '3'],

'Time': ['Before', 'During', 'After', 'Before', 'During', 'After', 'Before', 'During', 'After'],

'HeartRate': [70, 80, 75, 72, 85, 78, 68, 79, 76]

}

df = pd.DataFrame(data)

# Perform Repeated Measures ANOVA

anovarm = sm.stats.AnovaRM(df, 'HeartRate', 'Patient', within=['Time'])

fit = anovarm.fit()

print(fit.summary())

### 12.3.7 Assumptions of ANOVA

Example:

`from scipy import stats`

# Test for normality

_, p_value_norm = stats.shapiro(df['HeartRate'])

# Test for homoscedasticity

_, p_value_levene = stats.levene(

df['HeartRate'][df['Time'] == 'Before'],

df['HeartRate'][df['Time'] == 'During'],

df['HeartRate'][df['Time'] == 'After']

)

print("Shapiro-Wilk p-value:", p_value_norm)

print("Levene p-value:", p_value_levene)