频数表和列联表,以及进一步处理分析 -- R

目的

数据框包含了一些分类变量,问?

1.如何统计分类变量的分布次数 -- 频数表
2.如何统计多分类变量的分布次数 -- 频联表
3.单个分类变量的分类结果是否满足理论分类结果  -- 拟合优度问题
4.多个分类变量的分类结果是否相关干扰  -- 分类变量(多因素)独立性检验

数据

library(vcd)

data(Arthritis)
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
# 频数表
table(Arthritis$Treatment)
# Placebo Treated 
     43      41 

# 频联表
table(Arthritis$Treatment,Arthritis$Improved)
#          None Some Marked
#  Placebo   29    7      7
#  Treated   13    7     21

代码

  • 操作频数表
# 把频数表变成百分比占比
prop.table(table(Arthritis$Treatment))
# Placebo Treated 
#  0.5119  0.4881 
prop.table(table(Arthritis$Treatment)) *100
# Placebo Treated 
#  51.19   48.81

# 把列联表变成百分比形式
prop.table(table(Arthritis$Treatment,Arthritis$Improved))
#              None     Some   Marked
# Placebo 0.345238 0.083333 0.08333
# Treated 0.154762 0.083333 0.250000

# 按行求百分比
prop.table(table(Arthritis$Treatment,Arthritis$Improved),1)
# None    Some  Marked
# Placebo 0.67442 0.16279 0.16279
# Treated 0.31707 0.17073 0.51220
# 按列求百分比
prop.table(table(Arthritis$Treatment,Arthritis$Improved),2)
# None    Some  Marked
#  Placebo 0.69048 0.50000 0.25000
#  Treated 0.30952 0.50000 0.75000

# 给列联表添加行列计数
addmargins(table(Arthritis$Treatment,Arthritis$Improved))
#       None Some Marked Sum
#  Placebo   29    7      7  43
# Treated   13    7     21  41
# Sum       42   14     28  84
addmargins(table(Arthritis$Treatment,Arthritis$Improved),1)
#          None Some Marked
#  Placebo   29    7      7
#  Treated   13    7     21
#  Sum       42   14     28
addmargins(table(Arthritis$Treatment,Arthritis$Improved),2)
#          None Some Marked Sum
#  Placebo   29    7      7  43
# Treated   13    7     21  41

prop.table(addmargins(table(Arthritis$Treatment,Arthritis$Improved)))
#             None     Some   Marked      Sum
#  Placebo 0.086310 0.020833 0.020833 0.127976
#  Treated 0.038690 0.020833 0.062500 0.122024
#  Sum     0.125000 0.041667 0.083333 0.250000
# 单个分类变量的分类结果是否满足理论分类结果
table(Arthritis$Improved)
# None   Some Marked 
#   42     14     28 

x <- Arthritis$Improved
# 模拟一个理论分布
y <- c(rep("None",30),rep("Some",30),rep("Marked",24))

# 卡方检验
chisq.test(x,y)
# Pearson's Chi-squared test
# data:  x and y
# X-squared = 6.78, df = 4, p-value = 0.15
# 原假设 H0: 实际频次分布和理论频次分布不相似
# 因为 p >= 0.05,接受原假设,也就是数据中的频次分布和理论上的分布(y)不相似

#模拟一个 和实际分布很相似的数据
y <- c(rep("None",39),rep("Some",16),rep("Marked",29))
chisq.test(x,y)

# Pearson's Chi-squared test
# data:  x and y
# X-squared = 16.8, df = 4, p-value = 0.0021
# 此时p <= 0.05,需要拒绝原假设接受备择假设,也就是实际频次分布和理论频次分布相似
# 多个分类变量的分类结果是否相关干扰  -- 分类变量(多因素)独立性检验
table(Arthritis$Treatment,Arthritis$Improved)
# None Some Marked
#  Placebo   29    7      7
#  Treated   13    7     21
#问 treatment 方式对improved的频次分布有影响嘛?或者说两个分类变量独立吗?

# 卡方检验,直接把频联表丢给chisq.test( )函数即可    <==== 参数检验
mytable <- table(Arthritis$Treatment,Arthritis$Improved)
chisq.test(mytable)

# Pearson's Chi-squared test
# data:  mytable
# X-squared = 13.1, df = 2, p-value = 0.0015
# p <= 0.05 拒绝原假设:相互独立,也就是treatment 方式对improved的频次分布有影响

# Fisher's精确检验(Fisher's exact test)         <=========非参数检验
fisher.test(mytable)

# 	Fisher's Exact Test for Count Data
# data:  mytable
# p-value = 0.0014
# alternative hypothesis: two.sided
# p <= 0.05 拒绝原假设:相互独立,也就是treatment 方式对improved的频次分布有影响
# 上面看到两个分类变量是相互影响的
# 问:如何度量它们之间的相关性强度呢?
library(vcd)

mytable <- table(Arthritis$Treatment,Arthritis$Improved)
assocstats(mytable)

#                     X^2 df  P(> X^2)
# Likelihood Ratio 13.530  2 0.0011536
# Pearson          13.055  2 0.0014626

# Phi-Coefficient   : NA 
# Contingency Coeff.: 0.367 
# Cramer's V        : 0.394 

vcdӉ中的assocstats()函数可以计算二维列联表的phi系数,列联系数,Cramer‘s V系数
总体来说,较大的数值意味着较强的相关性

你可能感兴趣的:(统计学,bioinfo,R,r语言)