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

  1. > library(vcd)
  2. 载入需要的程辑包:MASS
  3. 载入需要的程辑包:grid
  4. 载入需要的程辑包:colorspace
  5. > head(Arthritis)
  6. ID Treatment Sex Age Improved
  7. 1 57 Treated Male 27 Some
  8. 2 46 Treated Male 29 None
  9. 3 77 Treated Male 30 None
  10. 4 17 Treated Male 32 Marked
  11. 5 36 Treated Male 46 Marked
  12. 6 23 Treated Male 58 Marked

1. generating frequency tables

(1) ONE-WAY TABLE

例01:

  1. > mytable<-with(Arthritis,table(Improved))
  2. > mytable
  3. Improved
  4. None Some Marked
  5. 42 14 28
  6. >
    > prop.table(mytable)
  7. Improved
  8. None Some Marked
  9. 0.5000000 0.1666667 0.3333333
  10. >
    > prop.table(mytable)*100
  11. Improved
  12. None Some Marked
  13. 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:

  1. > mytable<-xtabs(~Treatment+Improved,data=Arthritis)
  2. > mytable
  3. Improved
  4. Treatment None Some Marked
  5. Placebo 29 7 7
  6. 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:

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

margin.table():产生marginal frequencies;

prop.table():产生proportions。

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

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

例04:

  1. > addmargins(mytable)
  2. Improved
  3. Treatment None Some Marked Sum
  4. Placebo 29 7 7 43
  5. Treated 13 7 21 41
  6. Sum 42 14 28 84
  7. > addmargins(prop.table(mytable))
  8. Improved
  9. Treatment None Some Marked Sum
  10. Placebo 0.34523810 0.08333333 0.08333333 0.51190476
  11. Treated 0.15476190 0.08333333 0.25000000 0.48809524
  12. Sum 0.50000000 0.16666667 0.33333333 1.00000000

addmargins():add marginal sums to these tables;

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

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

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

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

  1. > addmargins(prop.table(mytable,2),1)
  2. Improved
  3. Treatment None Some Marked
  4. Placebo 0.6904762 0.5000000 0.2500000
  5. Treated 0.3095238 0.5000000 0.7500000
  6. 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

  1. > library(vcd)
  2. > mytable<-xtabs(~Treatment+Improved,data=Arthritis)
  3. > chisq.test(mytable)
  4.  
  5. Pearson's Chi-squared test
  6.  
  7. data: mytable
  8. X-squared = 13.055, df = 2, p-value = 0.001463
  9. > mytable<-xtabs(~Improved+Sex,data=Arthritis)
  10. > chisq.test(mytable)
  11.  
  12. Pearson's Chi-squared test
  13.  
  14. data: mytable
  15. X-squared = 4.8407, df = 2, p-value = 0.08889
  16.  
  17. Warning message:
  18. In chisq.test(mytable) : Chi-squared近似算法有可能不准

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

例08:FISHER'S EXACT TEST

  1. > mytable<-xtabs(~Treatment+Improved,data=Arthritis)
  2. > fisher.test(mytable)
  3.  
  4. Fisher's Exact Test for Count Data
  5.  
  6. data: mytable
  7. p-value = 0.001393
  8. 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

  1. > mytable<-xtabs(~Treatment+Improved+Sex,data=Arthritis)
  2. > mantelhaen.test(mytable)
  3.  
  4. Cochran-Mantel-Haenszel test
  5.  
  6. data: mytable
  7. 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:

  1. > library(vcd)
  2. > mytable<-xtabs(~Treatment+Improved,data=Arthritis)
  3. > assocstats(mytable)
  4. X^2 df P(> X^2)
  5. Likelihood Ratio 13.530 2 0.0011536
  6. Pearson 13.055 2 0.0014626
  7.  
  8. Phi-Coefficient : 0.394
  9. Contingency Coeff.: 0.367
  10. 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. 建议收藏:.net core 使用导入导出Excel详细案例,精心整理源码已更新至开源模板

    还记得刚曾经因为导入导出不会做而发愁的自己吗?我见过自己前同事因为一个导出改了好几天,然后我们发现虽然有开源的库但是用起来却不得心应手,主要是因为百度使用方案的时候很多方案并不能解决问题. 尤其是尝试 ...

  2. [loj2546][JSOI2018]潜入行动(树形DP)

    题目描述 外星人又双叒叕要攻打地球了,外星母舰已经向地球航行!这一次,JYY 已经联系好了黄金舰队,打算联合所有 JSOIer 抵御外星人的进攻. 在黄金舰队就位之前,JYY 打算事先了解外星人的进攻 ...

  3. C++学习笔记3_类.和相关函数

    1. 类*在C++中,struct和class没有明显差别,不同C#,class一定要new(手动开辟内存)出来struct Hero{ char name[64]; int sex;}void pr ...

  4. [考试反思]1031csp-s模拟测试96:常态

    按照smily的说法这一场的大众分暴力分是不是265啊QwQ那我可真是个大垃圾 总算还是回归了常态. T3文件名写错,把“city.in”写成“city,in” 还好,只丢了20分. T2乱打$O(n ...

  5. CSP-S:追忆

    Warning:这一篇极其中二,开了那个大会莫名有感而发. 模拟测试17那套题啊... 开的这个大会为什么弄得我退役感如此强烈... 早就想收藏了,还是记下来吧 <入阵曲> 丹青千秋酿, ...

  6. [考试反思]1010csp-s模拟测试67:摸索

    嗯...所谓RP守恒? 仍然延续着好一场烂一场的规律. 虽说我也想打破这个规律,但是并不想在考烂之后打破这个规律.(因为下一场要考好???) 我也不知道我现在是什么状态,相较于前一阶段有所提升(第一鸡 ...

  7. 算法编程题积累(3)——腾讯笔试"构造回文“问题

    首先理解题意,回文串的特点:倒序后跟原串相同.故而可以将原串看成向一个回文串在任意位置添加任意字符后形成的字符串,也就是说原串中存在一段未必连续的回文序列. 通过分析可以知道AC本题的核心思路:求出回 ...

  8. 玩转VSCode-完整构建VSCode开发调试环境

    随着VSCode的不断完善和强大,是时候将部分开发迁移到VS Code中了. 目前使用VS2019开发.NET Core应用,一直有一个想法,在VS Code中复刻VS的开发环境,同时迁移到VS Co ...

  9. 《计算机网络 自顶向下方法》 第8章 计算机网络中的安全 Part2

    SSL(使 TCP 连接安全) SSL(Secure Socket Layer),即安全套接字层,是对 TCP 的强化 HTTPS 使用 SSL,而 HTTP 不使用 SSL 通过采用机密性.数据完整 ...

  10. go中的数据结构通道-channel

    1. channel的使用 很多文章介绍channel的时候都和并发揉在一起,这里我想把它当做一种数据结构来单独介绍它的实现原理. channel,通道.golang中用于数据传递的一种数据结构.是g ...