这一部分使用在vcd包中的Arthritis数据集。

> library(vcd)
载入需要的程辑包:MASS
载入需要的程辑包:grid
载入需要的程辑包:colorspace
> 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

1. generating frequency tables

(1) ONE-WAY TABLE

例01:

> 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

table()函数:简单的频率(frequency)表示;

· table()函数会缺省自动的忽略missing values(NAs),要包含NA值需要使用选项useNA="ifany"

prop.table()函数:比例(proportion)表示;

prop.table()*100函数:百分数(percentage)表示。

(2)TWO-WAY TABLES

例02:

> mytable<-xtabs(~Treatment+Improved,data=Arthritis)
> mytable
Improved
Treatment None Some Marked
Placebo 29 7 7
Treated 13 7 21

(1)mytable<-table(A,B)

·A是行变量,B是列变量。

(2)xtabs()函数:使用公式方式的输入(formula style input)来创建一个列联表(contingency table)。

mytable<-xtabs(~A+B,data=mydata)

例03:

> margin.table(mytable,1)
Treatment
Placebo Treated
43 41
> prop.table(mytable,1)
Improved
Treatment None Some Marked
Placebo 0.6744186 0.1627907 0.1627907
Treated 0.3170732 0.1707317 0.5121951
> margin.table(mytable,2)
Improved
None Some Marked
42 14 28
> prop.table(mytable,2)
Improved
Treatment None Some Marked
Placebo 0.6904762 0.5000000 0.2500000
Treated 0.3095238 0.5000000 0.7500000
> prop.table(mytable)
Improved
Treatment None Some Marked
Placebo 0.34523810 0.08333333 0.08333333
Treated 0.15476190 0.08333333 0.25000000

margin.table():产生marginal frequencies;

prop.table():产生proportions。

·index(1):指在table()中的第一个变量;

·index(2):指在table()中的第二个变量。

例04:

> addmargins(mytable)
Improved
Treatment None Some Marked Sum
Placebo 29 7 7 43
Treated 13 7 21 41
Sum 42 14 28 84
> addmargins(prop.table(mytable))
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

addmargins():add marginal sums to these tables;

·缺省时为所有变量创建sum margins;

例04(变1):仅仅添加一个 sum column

> addmargins(prop.table(mytable,1),2)
Improved
Treatment None Some Marked Sum
Placebo 0.6744186 0.1627907 0.1627907 1.0000000
Treated 0.3170732 0.1707317 0.5121951 1.0000000

例04(变2):仅仅添加一个sum row

> addmargins(prop.table(mytable,2),1)
Improved
Treatment None Some Marked
Placebo 0.6904762 0.5000000 0.2500000
Treated 0.3095238 0.5000000 0.7500000
Sum 1.0000000 1.0000000 1.0000000

(3)MULTIDIMENSIONAL TABLES

例05:

> install.packages("gmodels")

--- 在此連線階段时请选用CRAN的鏡子 --- also installing the dependencies ‘gtools’, ‘gdata’

试开URL

’http://ftp.ctex.org/mirrors/CRAN/bin/windows/contrib/3.0/gtools_3.0.0.zip'

Content type 'application/zip' length 112950 bytes (110 Kb)

打开了URL

downloaded 110 Kb

试开URL

’http://ftp.ctex.org/mirrors/CRAN/bin/windows/contrib/3.0/gdata_2.13.2.zip'

Content type 'application/zip' length 850387 bytes (830 Kb)

打开了URL

downloaded 830 Kb

试开URL

’http://ftp.ctex.org/mirrors/CRAN/bin/windows/contrib/3.0/gmodels_2.15.4.zip'

Content type 'application/zip' length 76708 bytes (74 Kb)

打开了URL

downloaded 74 Kb

程序包‘gtools’打开成功,MD5和检查也通过

程序包‘gdata’打开成功,MD5和检查也通过

程序包‘gmodels’打开成功,MD5和检查也通过

下载的二进制程序包在

C:\Users\seven-wang\AppData\Local\Temp\RtmpIlHLxM\downloaded_packages里

> library(vcd)

载入需要的程辑包:MASS

载入需要的程辑包:grid

载入需要的程辑包:colorspace

> 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 |                   |

-----------------------------------------------|-------------|------------|-----------|--------------|

gmodels包中的CrossTable()函数:创建two-way tables models  after PROC FREO in SAS or CROSSTABS SPSS.

例06:

> mytable<-xtabs(~Treatment+Sex+Improved,data=Arthritis)

> mytable

, , 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

> ftable(mytable)

Improved None Some Marked

Treatment Sex

Placebo   Female              19    7      6

Male              10    0      1

Treated   Female               6    5     16

Male               7    2      5

> margin.table(mytable,1)

Treatment

Placebo Treated

43      41

> margin.table(mytable,2)

Sex

Female   Male

59     25

> margin.table(mytable,3)

Improved   None   Some Marked

42     14     28

> margin.table(mytable,c(,31))

Improved Treatment None Some Marked

Placebo             29    7      7

Treated             13    7     21

> ftable(prop.table(mytable,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

2. Test of independence

例07:CHI-AQUARE TEST OF INDEPENDENCE

> library(vcd)
> mytable<-xtabs(~Treatment+Improved,data=Arthritis)
> chisq.test(mytable) Pearson's Chi-squared test data: mytable
X-squared = 13.055, df = 2, p-value = 0.001463
> mytable<-xtabs(~Improved+Sex,data=Arthritis)
> chisq.test(mytable) Pearson's Chi-squared test data: mytable
X-squared = 4.8407, df = 2, p-value = 0.08889 Warning message:
In chisq.test(mytable) : Chi-squared近似算法有可能不准

chisq.test()函数: 产生一个chi-square of independence of the row and column variables.

例08:FISHER'S EXACT TEST

> mytable<-xtabs(~Treatment+Improved,data=Arthritis)
> fisher.test(mytable) Fisher's Exact Test for Count Data data: mytable
p-value = 0.001393
alternative hypothesis: two.sided

fisher.test()函数:产生一个Fisher 's exact test。

·Fisher's exact test :evaluate the hypothesis of independence of rows and columns in a contingency table with fixed marginals.

例09:COCHRAN-MANTEL-HAENSZEL TEST

> mytable<-xtabs(~Treatment+Improved+Sex,data=Arthritis)
> mantelhaen.test(mytable) Cochran-Mantel-Haenszel test data: mytable
Cochran-Mantel-Haenszel M^2 = 14.6323, df = 2, p-value = 0.0006647

mantelhaen.test()函数:提供一个Cochran-Mantel-Haenszel chi-aquare test of null ·hypothesis that two nominal variables are conditionally independent in each straum of a third variable.

3. measures of association

例10:

> library(vcd)
> mytable<-xtabs(~Treatment+Improved,data=Arthritis)
> assocstats(mytable)
X^2 df P(> X^2)
Likelihood Ratio 13.530 2 0.0011536
Pearson 13.055 2 0.0014626 Phi-Coefficient : 0.394
Contingency Coeff.: 0.367
Cramer's V : 0.394

vcd包中associstats()函数:计算 phi coefficient,contingency coefficient,Cramer's V.

Chapter 07-Basic statistics(Part2 Frequency and contingency tables)的更多相关文章

  1. Intro to Python for Data Science Learning 8 - NumPy: Basic Statistics

    NumPy: Basic Statistics from:https://campus.datacamp.com/courses/intro-to-python-for-data-science/ch ...

  2. Spark MLlib 之 Basic Statistics

    Spark MLlib提供了一些基本的统计学的算法,下面主要说明一下: 1.Summary statistics 对于RDD[Vector]类型,Spark MLlib提供了colStats的统计方法 ...

  3. Chapter 06—Basic graphs

    三. 柱状图(Histogram) 1. hist():画柱状图 ·breaks(可选项):控制柱状图的小柱子的条数: ·freq=FALSE:基于概率(probability),而非频率(frequ ...

  4. Chapter 04—Basic Data Management

    1. 创建新的变量 variable<-expression expression:包含一组大量的操作符和函数.常用的算术操作符如下表: 例1:根据已知变量,创建新变量的三种途径 > my ...

  5. Chapter 2 Basic Elements of JAVA

    elaborate:详细说明 Data TypesJava categorizes data into different types, and only certain operationscan ...

  6. 吴裕雄--天生自然 R语言开发学习:基本统计分析(续三)

    #---------------------------------------------------------------------# # R in Action (2nd ed): Chap ...

  7. 吴裕雄--天生自然 R语言开发学习:基本统计分析

    #---------------------------------------------------------------------# # R in Action (2nd ed): Chap ...

  8. [Hive - LanguageManual] Statistics in Hive

    Statistics in Hive Statistics in Hive Motivation Scope Table and Partition Statistics Column Statist ...

  9. BK: Data mining, Chapter 2 - getting to know your data

    Why: real-world data are typically noisy, enormous in volume, and may originate from a hodgepodge of ...

随机推荐

  1. Nexus 上传项目到私服

    1. maven setting配置 <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed ...

  2. Spring 动态代理 之 but was actually of type 'com.sun.proxy.$Proxy14 Exception

    今天在写Spring的引介代理的时候,报了一个错: Exception in thread "main" org.springframework.beans.factory.Bea ...

  3. Alibaba Java Coding Guidelines,以后的Java代码规范,就靠它了

    前言 对于Java代码规范,业界有统一的标准,不少公司对此都有一定的要求.但是即便如此,庞大的Java使用者由于经验很水平的限制,未必有规范编码的意识,而且即便经验丰富的老Java程序员也无法做到时刻 ...

  4. 关于路由器漏洞利用,qemu环境搭建,网络配置的总结

    FAT 搭建的坑 1 先按照官方步骤进行,完成后进行如下步骤 2 修改 move /firmadyne into /firmware-analysis-toolkit navigate to the ...

  5. CSPS模拟 58

    爆炸 没算内存见祖宗 为什么偏偏这次卡内存我没算 T1 HashMap各种水 T2 智障背包!但是卡内存! T3 Dashspeed 考试用点分治+线段树水到了80 实际上是个没见过的套路题 在之前的 ...

  6. windows系统一台电脑先后添加多个git账号

    概述 电脑上已经配置了github的ssh连接.现在又有一个不同的git账户,也就是要在一台电脑上配置两个git账号. 下面记录一下我配置的方法. 一.取消git全局配置 之前配置github的时候, ...

  7. NOIP模拟测试19考试反思

    这次考试是存在很大问题的,(如果不是T1T2出乎意料地A了,鬼知道会发生什么) T2A是情理之中,考试的时候测的极限数据跑的很快(无论m什么范围),但是T1真的...... T3没有分配太多的时间+没 ...

  8. vue 单页面应用 app自适应方案

    本文是使用淘宝的方案进行布局开发的,遇到的问题是会对app内使用的第三方插件,当页面进行缩放后,比如高德地图中的文字会显得过小,我使用的方法就是手动的动每一个尺寸进行手动的px 到 rem的替换,而不 ...

  9. mysql用find_in_set代替like搜索提高性能

    mysql用find_in_set代替like搜索提高性能 <pre>SELECT * from mobantestinfo1 where find_in_set('33',info2); ...

  10. 怎么把CAT客户端的RootMessageId记录到每条日志中?

    什么是RootMessageId? 为了理解RootMessageId先简单介绍一下CAT的数据结构设计.CAT客户端会将所有消息都封装为一个完整的消息树(MessageTree),消息树可能包括Tr ...