尝试计算p_value时收到错误消息

时间:2018-10-14 10:06:46

标签: python python-3.x p-value

获取一个“ TypeError:/:'generator'和'int'不受支持的操作数类型”,

问题是当我尝试计算p_value时,不确定我在做什么错。如果我的问题含糊不清,请原谅我

import numpy as np
import random
beer = [27, 19, 20, 20, 23, 17, 21, 24, 31, 26, 28, 20, 27, 19, 25, 31, 24, 28, 24, 29, 21, 21, 18, 27, 20]
water = [21, 19, 13, 22, 15, 22, 15, 22, 20, 12, 24, 24, 21, 19, 18, 16, 23, 20]

#running a permutation test
def permutation_test():
  combined = beer + water
  random.shuffle(combined)

#slice function to create 2 groups of the same length as the beer test group
  split = len(beer)
  group_one,group_two = combined[:split], combined[split:] #first25, last25
  return np.mean(group_one)-np.mean(group_two)

#monte carlo method to run the permutation test 100 000 times  
iterate = [permutation_test() for _ in range(100000)]

#calculating effect size, standard score
effect_size = np.median(beer) - np.median(water)
standard_score = (effect_size - np.mean(iterate))/np.std(iterate)

#calculating p-value to assess whether the observed effect size is an anomaly
p_value = np.mean(test >= effect_size for test in iterate)
print(standard_score, p_value)

1 个答案:

答案 0 :(得分:0)

您的列表理解表达式未正确定义:

使用它来解决问题:

 p_value = np.mean([(test >= effect_size) for test in iterate])