1. 频数统计与独立性检验
离散型变量通过计算频数,然后进行独立性检验
# 频数统计
library(vcd)
head(Arthritis)
# one way table
mytable <- with(Arthritis, table(Improved))
mytable # frequencies
prop.table(mytable) # proportions
prop.table(mytable)*100 # percentages
# two way table
mytable <- xtabs(~ Treatment+Improved, data=Arthritis)
mytable # frequencies
margin.table(mytable,1) #row sums
margin.table(mytable, 2) # column sums
prop.table(mytable) # cell proportions
prop.table(mytable, 1) # row proportions
prop.table(mytable, 2) # column proportions
addmargins(mytable) # add row and column sums to table
# Three way table
mytable <- xtabs(~ Treatment+Sex+Improved, data=Arthritis)
mytable
ftable(mytable)
# 独立性检验
# 卡方独立检验
library(vcd)
mytable <- xtabs(~Treatment+Improved, data=Arthritis)
chisq.test(mytable)
mytable <- xtabs(~Improved+Sex, data=Arthritis)
chisq.test(mytable)
2. t 检验与 x wilcox 检验
连续型变量独立性检验,如果数据分布满足正太分布可以使用 t 检验,否则使用 wilcox 检验
# 单个基因 t 检验
head(dta,1)
clu <-t.test(x=c(185.677,157.8727,134.354,150.355),y=c(5490.2100,1544.78100,3
195.68550,11603.02500),var.equal = T,alternative = "two.side")
clu
#取出 pvalue
clu_pavlue <- clu$p.value
clu_pavlue
#计算 Qvalue
p.adjust.methods
p.adjust(p =clu_pavlue,method = "fdr" )
#多个基因 t 检验计算
P_value <- round(apply(dta[2:9],1,FUN = function(x){t.test(x[1:4],x[5:8],paire
d = T)$p.value}),digits = 3)
Q_value <- p.adjust(P_value)
result <- data.frame(dta,BaseMeanA,BaseMeanB,log2FoldChange,P_value,Q_value)
result
# 非参数 wilcox test 检验
wilcox_Pvalue <- apply(dta[2:9],1,FUN = function(x){wilcox.test(x[1:4],x[5:8])
$p.value})
wilcox_Qvalue <- p.adjust(wilcox_Pvalue,method = "fdr")
wilcox_result <- cbind(dta,BaseMeanA,BaseMeanB,log2FoldChange,wilcox_Pvalue,wi
lcox_Qvalue)
3.相关性检验
R 可以计算多种相关系数,包括 Pearson 相关系数、Spearman 相关系数、Kendall 相关系数、偏相关系数等。直接利 用 cor()函数即可计算,输入数据必须是一个矩阵 .
#计算相关性矩阵
head(state.x77)#state.x77 数据集提供了美国 50 个州在 1997 年人口、收 入、文盲率、预期寿命、谋杀率和高中毕业率、气温以及土地面积的数据
cor(x=state.x77,use = "everything",method = "pearson")
cor(x=state.x77,use = "everything",method = "spearman")
cor(x=state.x77,use = "everything",method = "kendall")
#单独计算
x <- state.x77[,c("Population", "Income", "Illiteracy", "HS Grad")]
y <- state.x77[,c("Life Exp", "Murder")]
cor(x,y)
# 6.2 相关性检验
cor.test(state.x77[,3], state.x77[,5])
网友评论