Chapter 07-Basic statistics(Part4 t-tests&&nonparametric tests of group difference)
2013-08-23 22:38
357 查看
一. t-tests
这一部分我们使用分布在MASS包中的UScrime数据集。它是关于美国47个州在1960年时,关于惩罚制度对犯罪率的影响。
Prob:监禁(坐牢)的概率;
U1:14到24岁的城市那你的失业率;
U2:35到39岁的城市男子的失业率;
So:an indicator variable for Southern states
1. 独立的t-test(independent t-test)
t.test(y~x,data)
t.tset(y1,y2)
例01:
注意:可以摒弃南方的州和非南方的州有相同的犯罪率,因为p<0.01。
2.依赖的t-test
t.test(y1,y2,paired=TRUE)
·y1和y2是两个有依赖关系的组的数值向量。
例02:
二. nonparametric tests of group difference
1. 比较两组
如果两组是独立的,应该使用Wilcoxon rank sum去评估自变量是否是来自相同概率分布的样本。
wilcox.test(y~x,data)
wilcox.test(y1,y2)
例03:
例04:
2.比较多于两组
Kruskal-Wallis test:
kruskal.test(y~A,data)
·A:a grouping variable with two or more levels, if just two levels, equivalent to Mann-Whitney;
·y:a numeric outcome variable;
Friedman test:
friedman.test(y~A|B,data)
·B: a blocking variable that identifies matched observations.
npmc包中的npmc()函数:期待输入两列的数据,分别叫var(the dependent variable)和class(the grouping variable).
这一部分我们使用分布在MASS包中的UScrime数据集。它是关于美国47个州在1960年时,关于惩罚制度对犯罪率的影响。
Prob:监禁(坐牢)的概率;
U1:14到24岁的城市那你的失业率;
U2:35到39岁的城市男子的失业率;
So:an indicator variable for Southern states
1. 独立的t-test(independent t-test)
t.test(y~x,data)
t.tset(y1,y2)
例01:
> library(MASS) > t.test(Prob~So,data=UScrime) Welch Two Sample t-test data: Prob by So t = -3.8954, df = 24.925, p-value = 0.0006506 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: -0.03852569 -0.01187439 sample estimates: mean in group 0 mean in group 1 0.03851265 0.06371269
注意:可以摒弃南方的州和非南方的州有相同的犯罪率,因为p<0.01。
2.依赖的t-test
t.test(y1,y2,paired=TRUE)
·y1和y2是两个有依赖关系的组的数值向量。
例02:
> library(MASS) > sapply(UScrime[c("U1","U2")],function(x)(c(mean=mean(x),sd=sd(x)))) U1 U2 mean 95.46809 33.97872 sd 18.02878 8.44545 > with(UScrime,t.test(U1,U2,paired=TRUE)) Paired t-test data: U1 and U2 t = 32.4066, df = 46, p-value < 2.2e-16 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: 57.67003 65.30870 sample estimates: mean of the differences 61.48936
二. nonparametric tests of group difference
1. 比较两组
如果两组是独立的,应该使用Wilcoxon rank sum去评估自变量是否是来自相同概率分布的样本。
wilcox.test(y~x,data)
wilcox.test(y1,y2)
例03:
> with(UScrime,by(Prob,So,median)) So: 0 [1] 0.038201 -------------------------------------------------------- So: 1 [1] 0.055552 > wilcox.test(Prob~So,data=UScrime) Wilcoxon rank sum test data: Prob by So W = 81, p-value = 8.488e-05 alternative hypothesis: true location shift is not equal to 0
例04:
> sapply(UScrime[c("U1","U2")],median) U1 U2 92 34 > with(UScrime,wilcox.test(U1,U2,paired=TRUE)) Wilcoxon signed rank test with continuity correction data: U1 and U2 V = 1128, p-value = 2.464e-09 alternative hypothesis: true location shift is not equal to 0
2.比较多于两组
Kruskal-Wallis test:
kruskal.test(y~A,data)
·A:a grouping variable with two or more levels, if just two levels, equivalent to Mann-Whitney;
·y:a numeric outcome variable;
Friedman test:
friedman.test(y~A|B,data)
·B: a blocking variable that identifies matched observations.
npmc包中的npmc()函数:期待输入两列的数据,分别叫var(the dependent variable)和class(the grouping variable).
相关文章推荐
- Selected solutions to exercise of "The C Programming Language" 2e (Part 7, End of Chapter 1)
- Inline Assembly(for GNU assembler)-the difference of AT&T & intel
- Making & Using Objects(Chapter 2 of Thinking in C++)
- error C2352: 'CWnd::MessageBoxW' : illegal call of non-static member function
- [Err] 1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated colum
- Function Overloading & Default Arguments(Chapter 7 of Thinking in C++)
- References & the Copy-Constructor(Chapter 11 of Thinking in C++)
- Can I get rid of the "This page contains both secure and nonsecure items" warning?
- 【ccs13】Accepted papper list & part of download
- C++ difference of keywords 'typename' and 'class' in templates
- Difference of Getting DOM ElementsM Between IE6 & FF2
- Selected solutions to exercise of "The C Programming Language" 2e (Part 1)
- filegroup reference and partitioning scheme' is not supported in this version of sql server.
- error: invalid initialization of non-const reference of type ‘*&’ from a temporary of type
- Box2D: 错误variable length array of non-POD element type 'b2Vec2'
- [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c
- Intel-x86-System-Programming-Guide, Part 1,Chapter 2.1 OVERVIEW OF THE SYSTEM-LEVEL ARCHITECTURE
- mysql:Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column
- About the difference of href='javascript:void(0)' and href=‘#’
- ORA-15063: ASM discovered an insufficient number of disks for diskgroup "ASM,KEL"