📅  最后修改于: 2023-12-03 15:11:41.354000             🧑  作者: Mango
本统计-功率计算器是一个简单易用的工具,可以用于计算样本均值、方差、标准差、协方差以及线性回归的相关指标。同时,这个工具还可以通过输入样本数据,计算出样本的功率和最小有效差异值。对于需要进行统计分析的数据,使用本工具可以快速、准确地得到相应的统计结果。
该程序包含以下功能:
该程序需要输入样本数据,以及相关的参数。具体的输入说明如下:
该程序会输出如下信息:
from statistics_power_calculator import StatisticsPowerCalculator
# 创建一个对象
spc = StatisticsPowerCalculator()
# 输入样本数据,以及其他参数
independent_var = [1, 2, 3, 4, 5]
dependent_var = [2, 3, 4, 5, 6]
spc.set_sample_data(independent_var, dependent_var)
spc.set_significance_level(0.05)
spc.set_effect_size(0.5)
spc.set_sample_size(5)
# 进行统计分析
spc.calculate_statistics()
# 输出统计结果
print(spc.get_statistics_result())
以下是该程序的核心代码实现:
class StatisticsPowerCalculator:
def __init__(self):
self.independent_var = None
self.dependent_var = None
self.significance_level = 0.05
self.effect_size = 0.5
self.sample_size = 5
self.result = {}
def set_sample_data(self, independent_var, dependent_var):
self.independent_var = independent_var
self.dependent_var = dependent_var
def set_significance_level(self, level):
self.significance_level = level
def set_effect_size(self, size):
self.effect_size = size
def set_sample_size(self, size):
self.sample_size = size
def calculate_statistics(self):
# 计算样本均值、方差和标准差
sample_mean = sum(self.dependent_var) / len(self.dependent_var)
sample_variance = sum((x - sample_mean) ** 2 for x in self.dependent_var) / (len(self.dependent_var) - 1)
sample_stddev = math.sqrt(sample_variance)
# 计算协方差和相关系数
covariance = sum((x - sample_mean) * (y - sum(self.independent_var) / len(self.independent_var))
for x, y in zip(self.dependent_var, self.independent_var)) / (len(self.independent_var) - 1)
correlation = covariance / (sample_stddev * (sum((x - sum(self.independent_var) / len(self.independent_var)) ** 2
for x in self.independent_var) / (len(self.independent_var) - 1)))
# 计算回归系数和截距
sum_x, sum_y, sum_xy, sum_x_squared = 0, 0, 0, 0
for x, y in zip(self.independent_var, self.dependent_var):
sum_x += x
sum_y += y
sum_xy += x * y
sum_x_squared += x ** 2
slope = (len(self.independent_var) * sum_xy - sum_x * sum_y) / (len(self.independent_var) * sum_x_squared - sum_x ** 2)
intercept = sum_y / len(self.dependent_var) - slope * sum_x / len(self.dependent_var)
# 计算样本功率和最小有效差异值
eff_stddev = self.effect_size * sample_stddev
eff_difference = self.significance_level * sample_stddev / math.sqrt(self.sample_size)
null_distribution = stats.norm(loc=0, scale=sample_stddev / math.sqrt(self.sample_size))
critical_value = null_distribution.ppf(1 - self.significance_level)
power = 1 - null_distribution.cdf(critical_value - eff_difference)
min_effect_size = eff_difference / eff_stddev
# 将结果存储到属性result中
self.result['sample_mean'] = sample_mean
self.result['sample_variance'] = sample_variance
self.result['sample_stddev'] = sample_stddev
self.result['covariance'] = covariance
self.result['correlation'] = correlation
self.result['slope'] = slope
self.result['intercept'] = intercept
self.result['power'] = power
self.result['min_effect_size'] = min_effect_size
def get_statistics_result(self):
return self.result