这一部分使用R基础已安装包中的state.x77数据集。该数据集的数据是关于美国50个州在1977年对人口,收入,文盲率,平均寿命,谋杀率,高中毕业率统计所得。

1.关联的种类(types of correlations)

(1)PEARSON,SPEARMAN,KENDALL CORRELATIONS

·Pearson:评估两个数值变量间的线性关系的程度的暂时性关联;

·Spearman’s Rank Order:评估两个有排序关系的变量的相关率;

·Kendall's Tau:是非参数参与的排序关联。

cor(x,use=,method=)

其中,缺省时,use="everything",method="pearson"

例01:


> states<-state.x77[,1:6]
> cov(states)
Population Income Illiteracy Life Exp Murder HS Grad
Population 19931683.7588 571229.7796 292.8679592 -407.8424612 5663.523714 -3551.509551
Income 571229.7796 377573.3061 -163.7020408 280.6631837 -521.894286 3076.768980
Illiteracy 292.8680 -163.7020 0.3715306 -0.4815122 1.581776 -3.235469
Life Exp -407.8425 280.6632 -0.4815122 1.8020204 -3.869480 6.312685
Murder 5663.5237 -521.8943 1.5817755 -3.8694804 13.627465 -14.549616
HS Grad -3551.5096 3076.7690 -3.2354694 6.3126849 -14.549616 65.237894
> 
> cor(states)
Population Income Illiteracy Life Exp Murder HS Grad
Population 1.00000000 0.2082276 0.1076224 -0.06805195 0.3436428 -0.09848975
Income 0.20822756 1.0000000 -0.4370752 0.34025534 -0.2300776 0.61993232
Illiteracy 0.10762237 -0.4370752 1.0000000 -0.58847793 0.7029752 -0.65718861
Life Exp -0.06805195 0.3402553 -0.5884779 1.00000000 -0.7808458 0.58221620
Murder 0.34364275 -0.2300776 0.7029752 -0.78084575 1.0000000 -0.48797102
HS Grad -0.09848975 0.6199323 -0.6571886 0.58221620 -0.4879710 1.00000000
> cor(states,method="spearman")
Population Income Illiteracy Life Exp Murder HS Grad
Population 1.0000000 0.1246098 0.3130496 -0.1040171 0.3457401 -0.3833649
Income 0.1246098 1.0000000 -0.3145948 0.3241050 -0.2174623 0.5104809
Illiteracy 0.3130496 -0.3145948 1.0000000 -0.5553735 0.6723592 -0.6545396
Life Exp -0.1040171 0.3241050 -0.5553735 1.0000000 -0.7802406 0.5239410
Murder 0.3457401 -0.2174623 0.6723592 -0.7802406 1.0000000 -0.4367330
HS Grad -0.3833649 0.5104809 -0.6545396 0.5239410 -0.4367330 1.0000000

·cov()函数:产生了variances & covariances;

·cor()函数:提供了Pearson Product Moment correlation coefficients;

·cor( ,method="spearman"):提供了Spearman Rank Order correlation coefficients。

例02:

> x<-states[,c("Population","Income","Illiteracy","HS Grad")]
> y<-states[,c("Life Exp","Murder")]
> cor(x,y)
Life Exp Murder
Population -0.06805195 0.3436428
Income 0.34025534 -0.2300776
Illiteracy -0.58847793 0.7029752
HS Grad 0.58221620 -0.4879710

用上面的方法可以产生非方矩阵(nonsquare matrices)。

注意,结果不能表明是否the correlations differ significantly from 0,故需要tests of significance.

(2)部分关联(PARTIAL CORRELATIONS)

partial correlation: a correlation between two quantitative variables,controlling for one or more other quantitative variables.

ggm包中的pcor()函数:提供partial correlation coefficients。

pcor(u,S)

·u: 数据的向量,前两个数标记需要关联的数,剩余的数标记其他conditioning variables,即variables being partialed out;

·S: covariance matrix among the variables。

例03:

> library(ggm)
> # partial correlation of population and murder rate, controlling
> # for income, illiteracy rate, and HS graduation rate
> pcor(c(1,5,2,3,6), cov(states))
[1] 0.346

注释:0.346是指人口与谋杀率之间的correlation,并且要控制收入,文盲率,高中毕业率的影响。

(3)OTHER TYPES OF CORRELATIONS

polycor()包中的函数hetcor()函数:计算一个heterogeneous correlation matrix,

在数值变量(numerical variables)间包含Pearson product-moment correlations,

在数值变量与常量(ordinal variables)间有polyserial correlations,

在常量间有polychoric correlations,

在two dichotomous variables间有tetrachoric correlations。

2. 测试关联的重要值(Testing correlations for significance)

cor.test()函数:测试一个个体的Pearson,Spearman,Kendall correlation coefficient.

cor.test(x,y,alternative=,method=)

·x与y:是要被关联的变量;

·alternative:特指two-tailed,one-tailed test, ("two.side","less","greater");

alternative="less":研究的假设是人口关联小于0;

alternative="greater":研究的假设是人口关联大于0;

缺省状态是,alternative="two side".

·method:特指关联的方式,(“pearson”,“kendall”,“spearman”)。

例04:

> states<-state.x77[,1:6]
> cor(states)
            Population     Income Illiteracy    Life Exp     Murder     HS Grad
Population  1.00000000  0.2082276  0.1076224 -0.06805195  0.3436428 -0.09848975
Income      0.20822756  1.0000000 -0.4370752  0.34025534 -0.2300776  0.61993232
Illiteracy  0.10762237 -0.4370752  1.0000000 -0.58847793  0.7029752 -0.65718861
Life Exp   -0.06805195  0.3402553 -0.5884779  1.00000000 -0.7808458  0.58221620
Murder      0.34364275 -0.2300776  0.7029752 -0.78084575  1.0000000 -0.48797102
HS Grad    -0.09848975  0.6199323 -0.6571886  0.58221620 -0.4879710  1.00000000
> cor.test(states[,3],states[,5]) Pearson's product-moment correlation data: states[, 3] and states[, 5]
t = 6.8479, df = 48, p-value = 1.258e-08
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.5279280 0.8207295
sample estimates:
cor
0.7029752

注意:cor.test()函数一次只能测试一组关联。

corr.test()函数(psych包中)为pearson,spearman,kendall关联的矩阵提供关联与重要值。

例05:

> library(psych)
> corr.test(states,use="complete")
Call:corr.test(x = states, use = "complete")
Correlation matrix
Population Income Illiteracy Life Exp Murder HS Grad
Population 1.00 0.21 0.11 -0.07 0.34 -0.10
Income 0.21 1.00 -0.44 0.34 -0.23 0.62
Illiteracy 0.11 -0.44 1.00 -0.59 0.70 -0.66
Life Exp -0.07 0.34 -0.59 1.00 -0.78 0.58
Murder 0.34 -0.23 0.70 -0.78 1.00 -0.49
HS Grad -0.10 0.62 -0.66 0.58 -0.49 1.00
Sample Size
[1] 50
Probability values (Entries above the diagonal are adjusted for multiple tests.)
Population Income Illiteracy Life Exp Murder HS Grad
Population 0.00 0.59 1.00 1.0 0.10 1
Income 0.15 0.00 0.01 0.1 0.54 0
Illiteracy 0.46 0.00 0.00 0.0 0.00 0
Life Exp 0.64 0.02 0.00 0.0 0.00 0
Murder 0.01 0.11 0.00 0.0 0.00 0
HS Grad 0.50 0.00 0.00 0.0 0.00 0

注意:corr.test()函数中:

·use="pairwise"或"complete", pairwise deletion of missing values respectively;

·method="pearson"(缺省), "spearman", "kendall".

psych包中的pcor.test()函数:用来测试conditional independence of two variables controlling for one or more additional variables,assuming multivariate normality.

pcor.test(r,q,n)

r:pcor()函数产生的partial correlation;

q:被控制的变量数;

n:样本大小。

psych包中的r.test()函数:提供许多significance的测试。

Chapter 07-Basic statistics(Part3 correlations)的更多相关文章

  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. Django实现WebSSH操作物理机或虚拟机

    我想用它替换掉xshell.crt之类的工具 WebSSH操作物理机或虚拟机 Django实现WebSSH操作Kubernetes Pod文章发布后,有小伙伴说咖啡哥,我们现在还没有用上Kuberne ...

  2. Linux下基本操作

    强行转Linux,开始以为会很不适应,其实还好,换汤不换药 本文只讲基本操作,足够让你愉快的打代码,想飞上天的自行百度,或找其他大神(友链) Update 6/20:由于写得太烂被学长爆踩了一顿 直接 ...

  3. 搞清楚 Python 的迭代器、可迭代对象、生成器

    很多伙伴对 Python 的迭代器.可迭代对象.生成器这几个概念有点搞不清楚,我来说说我的理解,希望对需要的朋友有所帮助. 1 迭代器协议 迭代器协议是核心,搞懂了这个,上面的几个概念也就很好理解了. ...

  4. JSP——底层原理

    都知道jsp就是在HTML文件中写java代码,以实现动态页面的效果,但是这种动态是如何实现的呢?今天就在研究一下. 首先,我写了一个简单的jsp文件: <%@page import=" ...

  5. 最新JetBrains PyCharm 使用教程--常用功能设置(三)

    选择代码路径和Python解释器版本 ​ 设置Pycharm菜单字体的大小 ​ 设置编辑器里面字体大小 ​ 设置文件编码 ​ 设置背景颜色 ​ 设置tab键为4个空格 ​ 设置代码内容和关键字颜色 ​ ...

  6. python经典面试算法题1.2:如何从无序链表中移除重复项

    本题目摘自<Python程序员面试算法宝典>,我会每天做一道这本书上的题目,并分享出来,统一放在我博客内,收集在一个分类中. 1.2 如何实现链表的逆序 [蚂蚁金服面试题] 难度系数:⭐⭐ ...

  7. EFK教程 - ElasticSearch高性能高可用架构

    通过将elasticsearch的data.ingest.master角色进行分离,搭建起高性能+高可用的ES架构 作者:"发颠的小狼",欢迎转载与投稿 目录 ▪ 用途 ▪ 架构 ...

  8. HttpClient 上传文件

    /// <summary> /// 发送post请求 /// </summary> /// <param name="filePath">文件路 ...

  9. Linux centos5.6版本下mysql5.6主从环境安装配置

    MySQL数据库支持数据库的主从复制功能,因此在集群方面具有其独特的优势,国内外大型网站架构体系中,均采用了MySQL的主从数据库配置来实现查询负载.数据库热备等功能.本人在此将如何配置实现做了个简单 ...

  10. element - ui tree

    一行代码两行泪,没有外网真可怕!好久没写博客了,更新一把. <template> <div> <el-tree :data="data" :props ...