首页 > 其他 > 详细

频数统计函数 独立性检验函数 相关性分析函数 相关性检验函数

时间:2021-04-20 14:05:16      阅读:27      评论:0      收藏:0      [点我收藏+]

频数统计函数 独立性检验函数 相关性分析函数 相关性检验函数

频数统计函数

#frequency tables
mtcars$cyl <- as.factor(mtcars$cyl)
#将mtcars中的cyl做为因子赋值给mtcars$cyl
#使用split函数,按照mtcars$cyl进行分组
split(mtcars,mtcars$cyl)
split(mtcars,as.factor (mtcars$cyl))
num <- 1:100
cut(num,c(seq(0,100,10)))#使用cut将数据进行分组
cut(mtcars$mpg,c(seq(10,50,10)))
table(mtcars$cyl)#使用table统计频数
table(cut(mtcars$mpg,c(seq(10,50,10))))

prop.table(table(mtcars$cyl))#计算频率函数
prop.table (table(cut(mtcars$mpg,c(seq(10,50,10)))))
prop.table(table(mtcars$cyl))*100

library(vcd)
head(Arthritis)
table(Arthritis$Treatment,Arthritis$Improved)
#统计treatment与improved频数的统计
#one way table

mytable <- with(Arthritis, table(Improved))
mytable  # frequencies
prop.table(mytable) # proportions
prop.table(mytable)*100 # percentages


#two way table
mytable <- table(Arthritis$Treatment,Arthritis$Improved)
with(data = Arthritis,{table(Treatment,Improved)})
mytable <- xtabs(~ Treatment+Improved+Sex, 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


mytable <- xtabs(~ Treatment+Sex+Improved, data=Arthritis)
mytable
ftable(mytable) 
margin.table(mytable, 1)
margin.table(mytable, 2)
margin.table(mytable, 2)
margin.table(mytable, c(1,3))
ftable(prop.table(mytable, c(1,2)))
ftable(addmargins(prop.table(mytable, c(1, 2)), 3))

独立性检验函数

#独立性检验函数
library(vcd)
mytable <- table(Arthritis$Treatment,Arthritis$Improved)
#使用table计算频数
#mytable <- xtabs(~Treatment+Improved, data=Arthritis)
chisq.test(mytable)
#卡方独立检验,检测实施治疗与患者情况改善有没有关系
mytable <- table(Arthritis$Treatment,Arthritis$Improved)
chisq.test(mytable)

mytable <- table(Arthritis$Sex,Arthritis$Improved)
chisq.test(mytable)
#使用fisher精确检验
#Fisher‘s exact test
mytable <- xtabs(~Treatment+Improved, data=Arthritis)
fisher.test(mytable)
#使用xtabs统计Arthritis中的Treatment和Improved的频数
#再使用fisher.test检验这两个变量有没有联系
#P值小于0.05,说明原假设,即两者没关系的假设的成立可能性低
#那么我们就可以认为两者是有联系的
#Chochran-Mantel-Haenszel test
mytable <- xtabs(~Treatment+Improved+Sex, data=Arthritis)
mantelhaen.test(mytable)

相关性分析函数

#进行相关性分析
?cor
#state.x77是一个矩阵
state.x77
cor(state.x77)#简单粗暴,直接算相关系数
cor(state.x77, method="spearman")
cov(state.x77)#计算协方差,用来衡量两个变量的总体误差
colnames(state.x77)
#x <- states[,c("Population", "Income", "Illiteracy", "HS Grad")]
x <- state.x77[,c(1,2,3,6)]#x为x77中的第1236列
#y <- states[,c("Life Exp", "Murder")]
y <- state.x77[,c(4,5)]#y为x77中的45列
cor(x,y)#计算x与y之间的相关性
#partial correlations
install.packages("ggm")
library(ggm)#计算偏相关系数
#partial correlation of population and murder rate, controlling
#for income, illiteracy rate, and HS graduation rate
colnames(state.x77)
pcor(c(1,5,2,3,6), cov(state.x77))
#cov用来计算两个变量的总体误差,在计算偏相关时要用到
#1,5表示计算1列与5列的关系2,3,6表示控制2,3,6列的影响,cov是协方差的结果

相关性检验函数

#Testing a correlation coefficient for significance
cor.test(state.x77[,3],state.x77[,5])
#检测文盲率与谋杀率的关系

#Correlation matrix and tests of significance via corr.test
corr.test(states, use="complete")
library(psych)
corr.test (state.x77)
#直接计算多个变量的相关系数
library(ggm)
x <- pcor(c(1,5,2,3,6),cov(state.x77))
pcor.test(x,3,50)
#t test
library(MASS)
t.test(Prob ~ So, data=UScrime)



#dependent t test
sapply(UScrime[c("U1","U2")], function(x)(c(mean=mean(x),sd=sd(x))))
with(UScrime, t.test(U1, U2, paired=TRUE))


#Wilcoxon two group comparison
with(UScrime, by(Prob, So, median))
wilcox.test(Prob ~ So, data=UScrime)

sapply(UScrime[c("U1", "U2")], median)
with(UScrime, wilcox.test(U1, U2, paired=TRUE))


#Kruskal Wallis test
states <- data.frame(state.region, state.x77)
kruskal.test(Illiteracy ~ state.region, data=states)

#Nonparametric multiple comparisons
source("http://www.statmethods.net/RiA/wmc.txt")              
states <- data.frame(state.region, state.x77)
wmc(Illiteracy ~ state.region, data=states, method="holm")

频数统计函数 独立性检验函数 相关性分析函数 相关性检验函数

原文:https://www.cnblogs.com/kwq717/p/14680479.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!