目录
- T检验程序
- 方差分析
- 回归分析
T检验程序
import numpy as np
from scipy import stats# 准备数据
group_A = [10, 12, 11]
group_B = [15, 16, 14]# A vs B
t_stat, p_value = stats.ttest_ind(group_A, group_B)
print(f"A vs B: t-statistic = {t_stat}, p-value = {p_value}")
结果
F-Statistic: 24.0
P-Value: 0.008049893100837719
There is a significant difference between the groups.
方差分析
# 使用scipy.stats的f_oneway函数进行单因素方差分析
f_statistic, p_value = stats.f_oneway(group_A, group_B)# 输出结果
print(f"F-Statistic: {f_statistic}")
print(f"P-Value: {p_value}")# 判断显著性
alpha = 0.05
if p_value < alpha:print("There is a significant difference between the groups.")
else:print("There is no significant difference between the groups.")
运行结果
F-Statistic: 24.0
P-Value: 0.008049893100837719
There is a significant difference between the groups.
回归分析
import pandas as pd
import statsmodels.api as sm
import statsmodels.formula.api as smf# 创建数据
data = {'Group': ['A', 'A', 'A', 'B', 'B', 'B'],'Value': [10, 12, 11, 15, 16, 14]
}
df = pd.DataFrame(data)# 添加哑变量
df = pd.get_dummies(df, columns=['Group'], drop_first=True)# 回归模型
model = smf.ols('Value ~ Group_B', data=df).fit()# 输出回归结果
print(model.summary())# 提取方差分析表
anova_table = sm.stats.anova_lm(model, typ=2)
print("\nANOVA Table:")
print(anova_table)
运行结果
OLS Regression Results
==============================================================================
Dep. Variable: Value R-squared: 0.857
Model: OLS Adj. R-squared: 0.821
Method: Least Squares F-statistic: 24.00
Date: Mon, 20 Jan 2025 Prob (F-statistic): 0.00805
Time: 11:27:46 Log-Likelihood: -7.2972
No. Observations: 6 AIC: 18.59
Df Residuals: 4 BIC: 18.18
Df Model: 1
Covariance Type: nonrobust
===================================================================================coef std err t P>|t| [0.025 0.975]
-----------------------------------------------------------------------------------
Intercept 11.0000 0.577 19.053 0.000 9.397 12.603
Group_B[T.True] 4.0000 0.816 4.899 0.008 1.733 6.267
==============================================================================
Omnibus: nan Durbin-Watson: 2.500
Prob(Omnibus): nan Jarque-Bera (JB): 0.562
Skew: 0.000 Prob(JB): 0.755
Kurtosis: 1.500 Cond. No. 2.62
==============================================================================Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.ANOVA Table:sum_sq df F PR(>F)
Group_B 24.0 1.0 24.0 0.00805
Residual 4.0 4.0 NaN NaN