ANOVA With Python And SQL. Understanding why and how to do... | by Nurullah Sirca | Towards Data Science

The analysis of variance (ANOVA) is utilized for analyzing the variance which involves assessing the difference between the two variables. It is an effective statistical tool for analyzing financial or customer data which is helpful for monitoring the planning process and managing the projects successfully. One-way ANOVA should be used when you have collected data about one categorical independent variable and one quantitative dependent variable. The independent variable should have at least three levels. Here, we will fetch a clinical trial dataset from SQL with pyodbc, run ANOVA on Python and interpret the results.
A little backstory before we dive in! Before ANOVA, people were using multiple t-tests to compare if there is a difference between variables. As the world progressed, the data become vaster and the number of groups increased. Doing multiple t-tests were not going to be feasible, therefore ANOVA was born. Note that, use a t-test instead If you only want to compare two groups
Check sample sizes, we need an equal number of observations in each group.
Calculate Mean Square for each group.
Calculate Mean Square error.
Calculate F-value by dividing the difference between groups to the difference within groups.
Using the F-distribution Table for deciding on the Null Hypothesis.
The Shapiro-Wilk test can be used to check the normal distribution of residuals. Null hypothesis being the data is drawn from the normal distribution.Bartlett’s test to check the homogeneity of variances. Null hypothesis being the samples from populations having equal variances. Data isn’t drawn from a normal distribution? Try the Levene test instead.
The Shapiro-Wilk test can be used to check the normal distribution of residuals. Null hypothesis being the data is drawn from the normal distribution.
Bartlett’s test to check the homogeneity of variances. Null hypothesis being the samples from populations having equal variances. Data isn’t drawn from a normal distribution? Try the Levene test instead.
The data we will use is a clinical trials data. It shows the memory performance of patients under three different drugs. To get the data from the SQL server, we will use the Python package pyodbc.
After entering the required credentials, we get the data we need by executing:
cursor.execute("SELECT Drug,(Mem_Score_After - Mem_Score_Before) as Diff FROM trials WHERE Drug IN ('A','S','T')")
After fetching the data, we can transfer it to pandas for further analysis. If we describe and visualize the target variable values by drugs we get:
Note: As they are seen in both boxplot and description of drugs, the memory performance of patients under Drug A is the best clearly.
First of all, the hypotheses need to be defined.
Null Hypothesis:Mean memory performances for different drugs is the same.
Alternate Hypothesis:Mean memory performances for different drugs isn’t same(It might be the case that mean for two drugs are the same, but not all)
Let’s now run a one way ANOVA test to see if the groups are indeed different or not. The python package stats is used for this part of the analysis.
stats.f_oneway(df[‘Diff’][df[‘Drug’] == ‘A’],  df[‘Diff’][df[‘Drug’] == ‘S’], df[‘Diff’][df[‘Drug’] == ‘T’])
The P-value obtained from ANOVA analysis is significant (P<0.05),
and therefore, we conclude that there are significant differences among treatments. The results show an F statistic of 22. If F value is bigger than F-critical, which is in our case, we reject the null hypothesis. If F value is smaller than F-critical, we accept the null hypothesis.
Does this make sense? Let’s visualize again.
We can see that for two groups, the difference between them is lower than the difference within them. For the third group, shown in blue, the distribution seems quite different than the other two. Therefore our eyes also tell us that the drug treatment shown in blue is different than the other two.
We have done a basic one way ANOVA to see if any of the drugs improved memory performance. After getting a significant result and an F statistic bigger than the critical value of F, we have concluded that the drugs do not have the same effect. We have seen from the histogram that one of the drugs had much higher performance than the other two. This is a simple dataset, if it was more complex we would use the Tukey HSD test to see which groups are different.
One way ANOVA is a great way to see if there is a difference between groups, let it be groups of customers, trials or financial operations related experiments. I am sure you can find a way to apply them to your work if they haven’t been already.
I welcome feedback, please let me know if you have questions. You can reach me out on LinkedIn.