t检验与点二列相关的异同

t检验、点二列相关
两者侧重点不同,前者重在检验两列数据的均值差异,即宏观上是否来自同一总体,后者关注两列数据的相关程度。
相同点在都是一个二分数据将一列等距或等比数据分类,且两者数值可以通过公式相互转换。

转换的Python代码如下:

import math
import numpy as np


# s1 = [10, 9, 8, 8, 8, 7]
# s2 = [5, 5, 4, 4]

# s1 = [67, 73, 74, 70, 70, 75, 73, 68, 69]
# s2 = [69, 63, 67, 64, 61, 66, 60, 63, 63]

s1 = [84, 84, 88, 90, 78, 92, 94, 96, 88, 90]
s2 = [82, 76, 60, 72, 74, 76, 80, 78, 76, 74]

xbar1 = np.mean(s1)
xbar2 = np.mean(s2)


# Calculate sum of squares(和方)
def calc_ss(s):
  return sum(map(lambda x: x * x, s)) - sum(s) ** 2 / len(s)


ss1 = calc_ss(s1)
ss2 = calc_ss(s2)

xsquare1 = ss1 / (len(s1) - 1)
xsquare2 = ss2 / (len(s2) - 1)

print('Xbar1:', xbar1)
print('Xbar2:', xbar2)

print('SS1:', ss1)
print('SS2:', ss2)

print('Xsquare1:', xsquare1)
print('Xsquare2:', xsquare2)

spsquare = (ss1 + ss2) / (len(s1) + len(s2) - 2)
# 总体方差
print('SPsquare', spsquare)

se = np.sqrt( spsquare / len(s1) + spsquare / len(s2) )

print('SE:', se)


tobs = (xbar1 - xbar2) / se

print('t_obs:', tobs)

print('---------------------')

s3 = s1 + s2

sx = np.sqrt( calc_ss(s3) / len(s3) )

print('sx:', sx)

rpb = ((xbar1 - xbar2) / sx) * np.sqrt( (len(s1) / len(s3)) * (len(s2) / len(s3)) )

print('r_pb:', rpb)
print('t_obs:', tobs)
print('Fmax:', max(xsquare1, xsquare2) / min(xsquare1, xsquare2))

print(rpb ** 2, 'is r_pb^2')
print(tobs ** 2 / (tobs ** 2 + len(s1) + len(s2) - 2), 'is t^2 / (t^2 + df)')

Output:

Xbar1: 88.4
Xbar2: 74.8
SS1: 254.39999999999418
SS2: 321.59999999999854
Xsquare1: 28.26666666666602
Xsquare2: 35.73333333333317
SPsquare 31.999999999999595
SE: 2.5298221281346875
t_obs: 5.375872022286282
---------------------
sx: 8.662563131083052
r_pb: 0.7849870641173408
t_obs: 5.375872022286282
Fmax: 1.2641509433962497
0.616204690831562 is r_pb^2
0.6162046908315598 is t^2 / (t^2 + df)

Tag: none

Leave a new comment