术语及定义
1、重复测量定义
在重复测量研究中,样本中的个体要在同一个因变量下被测量多于一次,所有的处理条件都使用相同的被试。
2、与独立试验研究的区别
重复试验与独立试验的区别是,重复试验使用的是同一批样本,比较的是同一个样本在施加因变量影响后与处理前的变化。
示例及公式
表1 放松训练前后,在哮喘发作时,病人所需药物的剂量
病人 | 训练前的一周 | 训练后的一周 | D | D^2 |
---|---|---|---|---|
A | 9 | 3 | -6 | 36 |
B | 4 | 1 | -3 | 9 |
C | 5 | 0 | -5 | 25 |
D | 4 | 3 | -1 | 1 |
E | 7 | 2 | -5 | 25 |
陈述假设:
H
0
:
u
=
0
(症状上没有变化)
H0:u=0(症状上没有变化)
H0:u=0(症状上没有变化)
H 1 : u ≠ 0 (有变化) H1:u\neq0(有变化) H1:u=0(有变化)
公式:
t
=
M
−
u
s
t=\frac{M-u}{s}
t=sM−u
其中M为差值的平均值,在此例中:
M
=
(
−
6
−
3
−
5
−
1
−
5
)
5
=
−
4
M=\frac{(-6-3-5-1-5)}{5}=-4
M=5(−6−3−5−1−5)=−4
u总体平均数差,即为0。
s为估计标准误:
计算s的方式第一步是求出样本的方差:
s
2
=
S
S
n
−
1
=
∑
D
2
−
(
∑
D
)
2
n
n
−
1
=
16
5
−
1
=
4
(
S
S
为平方差和)
s^2=\frac{SS}{n-1}=\frac{\sum D^2-\frac{(\sum D)^2}{n}}{n-1}=\frac{16}{5-1}=4(SS为平方差和)
s2=n−1SS=n−1∑D2−n(∑D)2=5−116=4(SS为平方差和)
然后估计除标准误:
s
=
s
2
n
=
4
5
=
0.894
s=\sqrt{\frac{s^2}{n}}=\sqrt{\frac{4}{5}}=0.894
s=ns2=54=0.894
最终的t值为:
t
=
M
−
u
s
=
−
4
−
0
0.894
=
−
4.47
t=\frac{M-u}{s}=\frac{-4-0}{0.894}=-4.47
t=sM−u=0.894−4−0=−4.47
maven依赖
<!-- https://mvnrepository.com/artifact/net.sf.jsci/jsci -->
<dependency>
<groupId>net.sf.jsci</groupId>
<artifactId>jsci</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-math3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
实现代码
package com.math.statistics;
import org.apache.commons.math3.stat.descriptive.moment.StandardDeviation;
import JSci.maths.statistics.TDistribution;
/***
* 配对T检验
* @author miaoyibo
*
*/
public class PairedTTest {
private double[] x;
private double[] y;
private int freedom;
StandardDeviation standardDeviation =new StandardDeviation();
public PairedTTest(double[] x, double[] y) {
this.x = x;
this.y = y;
this.freedom=x.length-1;
}
public int getXSize() {
return x==null?0:x.length;
}
public int getYSize() {
return y==null?0:y.length;
}
/***
* 计算差值的平均值
* @return
*/
public double getMean() {
double sum=0;
for(int i=0;i<x.length;i++) {
sum=sum+(y[i]-x[i]);
}
return sum/x.length;
}
/***
* 计算方差
* @param x
* @return
*/
public double getStandard() {
double sum=0;
double pow=0;
for(int i=0;i<x.length;i++) {
double d=(y[i]-x[i]);
pow=pow+Math.pow(d,2);
sum=sum+d;
}
int n=x.length;
double ss=pow-Math.pow(sum,2)/n;
return ss;
}
public double calculateTvalue() {
double sp=getStandard()/freedom;
double sm=Math.sqrt(sp/x.length);
return (getMean()-0)/sm;
}
public int getDegreesOfFreedom() {
return freedom;
}
/***
* @return
*/
public double getPValue() {
double t=calculateTvalue();
TDistribution td=new TDistribution(freedom);
double cumulative = td.cumulative(t);
double p;
if(t>0) {
p=(1-cumulative)*2;
}else {
p=cumulative*2;
}
return p;
}
/***
* 效应大小
* @return
*/
public double getRValue() {
double t=calculateTvalue();
return Math.pow(t,2)/(Math.pow(t,2)+freedom);
}
/***
* 科恩d值
* @return
*/
public double getDValue() {
double m=getMean();
double s=getStandard()/freedom;
return Math.abs(m/Math.sqrt(s));
}
}
DEMO
package com.math.demo;
import com.math.statistics.PairedTTest;
public class PairedTTestDemo {
public static void main(String[] args) {
double[] x= {9,4,5,4,7};
double[] y= {3,1,0,3,2};
PairedTTest pt=new PairedTTest(x, y);
System.out.println(pt.calculateTvalue());//T分数
System.out.println(pt.getPValue());//P值
System.out.println(pt.getRValue());//效应大小
System.out.println(pt.getDValue());//科恩d值
}
}