我是靠谱客的博主 体贴大船,最近开发中收集的这篇文章主要介绍频数表和列联表的使用,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

数据使用的是Kock&Edward(1988),一项风湿性关节炎新疗法的双盲临床试验的结果。

其中Treatment为治疗情况,表示为两种:Treated(用药治疗)、Placebo(安慰剂治疗),improved为改善情况,包括了无改善、一定程度改善和显著改善。

相关函数包括
table()
xtabs()
prop.table()
margin.table()
addmargins()
ftable()

相关实验操作
频数表创建、频数转化比例、二维列联表创建、生成边际频数和比例、添加以及创建边际和、三位列联表创建以及数据的一些处理。

> #引入数据
> library(vcd)
> head(Arthritis)
ID Treatment
Sex Age Improved
1 57
Treated Male
27
Some
2 46
Treated Male
29
None
3 77
Treated Male
30
None
4 17
Treated Male
32
Marked
5 36
Treated Male
46
Marked
6 23
Treated Male
58
Marked
> #一维列联表
> mytable <- with(Arthritis,table(Improved))
> mytable
Improved
None
Some Marked
42
14
28
> #转化为比例值
> prop.table(mytable)
Improved
None
Some
Marked
0.5000000 0.1666667 0.3333333
> #转为百分比
>
> prop.table(mytable)*100
Improved
None
Some
Marked
50.00000 16.66667 33.33333
> #二维列联表
> mytable1 <- xtabs(~ Treatment+Improved,data=Arthritis)
> mytable1
Improved
Treatment None Some Marked
Placebo
29
7
7
Treated
13
7
21
> #行和与行比
> #安慰剂治疗和用药治疗边际频数
> margin.table(mytable1,1)
Treatment
Placebo Treated
43
41
> #安慰剂治疗和用药治疗比例
> prop.table(mytable1,1)
Improved
Treatment
None
Some
Marked
Placebo 0.6744186 0.1627907 0.1627907
Treated 0.3170732 0.1707317 0.5121951
> #列和与列比
> #安慰剂治疗和用药治疗边际频数
> margin.table(mytable1,2)
Improved
None
Some Marked
42
14
28
> #安慰剂治疗和用药治疗比例
> prop.table(mytable1,2)
Improved
Treatment
None
Some
Marked
Placebo 0.6904762 0.5000000 0.2500000
Treated 0.3095238 0.5000000 0.7500000
> #各单元格所占比例
> prop.table(mytable1)
Improved
Treatment
None
Some
Marked
Placebo 0.34523810 0.08333333 0.08333333
Treated 0.15476190 0.08333333 0.25000000
> #添加边际和
>
> addmargins(mytable1)
Improved
Treatment None Some Marked Sum
Placebo
29
7
7
43
Treated
13
7
21
41
Sum
42
14
28
84
> addmargins(prop.table(mytable1))
Improved
Treatment
None
Some
Marked
Sum
Placebo 0.34523810 0.08333333 0.08333333 0.51190476
Treated 0.15476190 0.08333333 0.25000000 0.48809524
Sum
0.50000000 0.16666667 0.33333333 1.00000000
> #使用CrossTable生成二维列联表
> library(gmodels)
> CrossTable(Arthritis$Treatment,Arthritis$Improved)
Cell Contents
|-------------------------|
|
N |
| Chi-square contribution |
|
N / Row Total |
|
N / Col Total |
|
N / Table Total |
|-------------------------|
Total Observations in Table:
84
| Arthritis$Improved
Arthritis$Treatment |
None |
Some |
Marked | Row Total |
--------------------|-----------|-----------|-----------|-----------|
Placebo |
29 |
7 |
7 |
43 |
|
2.616 |
0.004 |
3.752 |
|
|
0.674 |
0.163 |
0.163 |
0.512 |
|
0.690 |
0.500 |
0.250 |
|
|
0.345 |
0.083 |
0.083 |
|
--------------------|-----------|-----------|-----------|-----------|
Treated |
13 |
7 |
21 |
41 |
|
2.744 |
0.004 |
3.935 |
|
|
0.317 |
0.171 |
0.512 |
0.488 |
|
0.310 |
0.500 |
0.750 |
|
|
0.155 |
0.083 |
0.250 |
|
--------------------|-----------|-----------|-----------|-----------|
Column Total |
42 |
14 |
28 |
84 |
|
0.500 |
0.167 |
0.333 |
|
--------------------|-----------|-----------|-----------|-----------|
> #三位列联表
>
> mytable2 <- xtabs(~ Treatment+Sex+Improved,data = Arthritis)
> mytable2
, , Improved = None
Sex
Treatment Female Male
Placebo
19
10
Treated
6
7
, , Improved = Some
Sex
Treatment Female Male
Placebo
7
0
Treated
5
2
, , Improved = Marked
Sex
Treatment Female Male
Placebo
6
1
Treated
16
5
> #三位列联表边际频数
> margin.table(mytable2,1)
Treatment
Placebo Treated
43
41
> margin.table(mytable2,2)
Sex
Female
Male
59
25
> margin.table(mytable2,3)
Improved
None
Some Marked
42
14
28
> #治疗情况和改善情况的边际频数
> margin.table(mytable2,c(1,3))
Improved
Treatment None Some Marked
Placebo
29
7
7
Treated
13
7
21
> #治疗情况和性别各类改善情况比例
> ftable(prop.table(mytable2,c(1,2)))
Improved
None
Some
Marked
Treatment Sex
Placebo
Female
0.59375000 0.21875000 0.18750000
Male
0.90909091 0.00000000 0.09090909
Treated
Female
0.22222222 0.18518519 0.59259259
Male
0.50000000 0.14285714 0.35714286
> ftable(prop.table(mytable2,c(1,2)),3)
Improved
None
Some
Marked
Treatment Sex
Placebo
Female
0.59375000 0.21875000 0.18750000
Male
0.90909091 0.00000000 0.09090909
Treated
Female
0.22222222 0.18518519 0.59259259
Male
0.50000000 0.14285714 0.35714286

最后

以上就是体贴大船为你收集整理的频数表和列联表的使用的全部内容,希望文章能够帮你解决频数表和列联表的使用所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部