import pandas as pd
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
# Load the data from 'CM_cost.csv' into a DataFrame
data = pd.read_csv('CM_cost.csv')
# Calculate the difference between post-engagement and pre-engagement costs
data['cost_diff'] = data['cost_post'] - data['cost_pre']
# Summary statistics for pre-engagement costs
print('Summary statistics for pre-engagement costs (cost_pre):')
print(data['cost_pre'].describe())
# Summary statistics for post-engagement costs
print('\nSummary statistics for post-engagement costs (cost_post):')
print(data['cost_post'].describe())
# Summary statistics for cost differences
print('\nSummary statistics for cost differences (cost_post - cost_pre):')
print(data['cost_diff'].describe())
# Histograms to visualize the distributions
plt.figure(figsize=(12, 5))
plt.subplot(1, 3, 1)
plt.hist(data['cost_pre'], bins=20, color='blue', alpha=0.7)
plt.title('Pre-engagement Costs')
plt.subplot(1, 3, 2)
plt.hist(data['cost_post'], bins=20, color='green', alpha=0.7)
plt.title('Post-engagement Costs')
plt.subplot(1, 3, 3)
plt.hist(data['cost_diff'], bins=20, color='red', alpha=0.7)
plt.title('Cost Differences')
plt.tight_layout()
plt.show()
# Test for normality of the cost differences using Shapiro-Wilk test
stat, p_value_normality = stats.shapiro(data['cost_diff'])
print('\nShapiro-Wilk test for normality of cost differences:')
print(f'Statistic={stat:.3f}, p-value={p_value_normality:.3f}')
# Decide which statistical test to use based on the normality test result
alpha = 0.05 # Significance level
if p_value_normality > alpha:
print('\nCost differences are approximately normally distributed.')
# Perform a paired t-test (one-tailed test)
t_stat, p_value_ttest = stats.ttest_rel(data['cost_pre'], data['cost_post'], alternative='greater')
print('Paired t-test (one-tailed):')
print(f't-statistic={t_stat:.3f}, p-value={p_value_ttest:.3f}')
if p_value_ttest < alpha:
print('Conclusion: There is a statistically significant reduction in medical costs after the program.')
else:
print('Conclusion: No statistically significant reduction detected.')
else:
print('\nCost differences are not normally distributed.')
# Perform a Wilcoxon signed-rank test (one-tailed test)
stat_wilcoxon, p_value_wilcoxon = stats.wilcoxon(data['cost_pre'], data['cost_post'], alternative='greater')
print('Wilcoxon signed-rank test (one-tailed):')
print(f'Statistic={stat_wilcoxon:.3f}, p-value={p_value_wilcoxon:.3f}')
if p_value_wilcoxon < alpha:
print('Conclusion: There is a statistically significant reduction in medical costs after the program.')
else:
print('Conclusion: No statistically significant reduction detected.')
Explanation:
-
Data Loading and Preparation:
- We load the dataset
CM_cost.csv into a pandas DataFrame.
- We calculate the difference between post-engagement and pre-engagement costs (
cost_diff), which is essential for paired comparisons.
-
Descriptive Statistics and Visualization:
- We output summary statistics (
count, mean, std, min, 25%, 50%, 75%, max) for cost_pre, cost_post, and cost_diff to understand the basic characteristics of the data.
- We create histograms for
cost_pre, cost_post, and cost_diff to visualize their distributions. This helps in assessing normality visually.
-
Normality Testing:
- We perform the Shapiro-Wilk test on
cost_diff to statistically assess whether the differences are normally distributed.
- Null hypothesis (H0): The data is normally distributed.
- Alternative hypothesis (H1): The data is not normally distributed.
- If the p-value from the Shapiro-Wilk test is greater than 0.05, we fail to reject H0 and proceed assuming normality.
-
Statistical Testing:
- If the data is normally distributed:
- We perform a paired t-test using
stats.ttest_rel with the alternative='greater' parameter for a one-tailed test.
- Null hypothesis (H0): The mean post-engagement cost is not less than the mean pre-engagement cost.
- Alternative hypothesis (H1): The mean post-engagement cost is less than the mean pre-engagement cost.
- We check if the p-value is less than the significance level (0.05). If it is, we conclude that there is a statistically significant reduction in costs.
- If the data is not normally distributed:
- We perform a Wilcoxon signed-rank test using
stats.wilcoxon with the alternative='greater' parameter for a one-tailed test.
- The hypotheses are similar to the t-test but suited for non-parametric data.
- We interpret the p-value in the same way.
-
Conclusion:
- Based on the p-value from the appropriate test, we draw a conclusion about whether the medical costs have significantly reduced after the program.
- A p-value less than the significance level indicates that we can reject the null hypothesis in favor of the alternative hypothesis.
How to Interpret the Results:
-
P-value Interpretation:
- p-value < 0.05: Significant evidence to conclude that medical costs have reduced after the program.
- p-value ≥ 0.05: Insufficient evidence to conclude that there is a reduction in medical costs.
-
Test Selection:
- Paired t-test: Used when the cost differences are normally distributed.
- Wilcoxon signed-rank test: Used when the cost differences are not normally distributed.
By following this approach, we ensure that the statistical test chosen is appropriate for the data distribution, which leads to valid and reliable conclusions about the effectiveness of the program in reducing medical costs.