白话统计学——相关分析
2019-04-28 本文已影响4人
drlee_fc74
白话统计学——相关分析
R语言当中相关分析的结果
R语言当中通过cor
函数我们可以实现相关分析的结果,最终得到其相关系数。如果是需要协方差可以使用cov
函数。另外使用cot.test
除了可以得到相关系数同时还可以得到95%CI以及P值。
PS:上述函数默认的都是使用全部数据,如果有NA值的话,则会返回NA。因此可以通过use = use = "pairwise.complete.obs"
自动的去掉NA来进行计算。 PPS:另外可以通过method
来改变相关分析的方法。
library(openintro)
## Please visit openintro.org for free statistics materials
##
## Attaching package: 'openintro'
## The following objects are masked from 'package:datasets':
##
## cars, trees
library(HistData)
library(tidyverse)
## ─ Attaching packages ───────────────── tidyverse 1.2.1 ─
## ✔ ggplot2 3.1.1 ✔ purrr 0.3.2
## ✔ tibble 2.1.1 ✔ dplyr 0.8.0.1
## ✔ tidyr 0.8.3 ✔ stringr 1.4.0
## ✔ readr 1.3.1 ✔ forcats 0.4.0
## ─ Conflicts ────────────────── tidyverse_conflicts() ─
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
data(ncbirths)
ncbirths %>%
summarize(N = n(), r = cor(weeks, weight),
r_nona = cor(weeks, weight, use = "pairwise.complete.obs"))
## N r r_nona
## 1 1000 NA 0.6701013
###cor.test的使用
cor.test(ncbirths$weeks, ncbirths$weight, use = "pairwise.complete.obs")
##
## Pearson's product-moment correlation
##
## data: ncbirths$weeks and ncbirths$weight
## t = 28.491, df = 996, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.6344277 0.7029265
## sample estimates:
## cor
## 0.6701013
分类资料的相关分析
分类资料的相关系数主要是通过vcd::assocstats
来进行分析的。
library(vcd)
## Loading required package: grid
data("Arthritis")
tab <- xtabs(~Improved + Treatment, data = Arthritis)
fit <- assocstats(tab)
summary(fit)
##
## Call: xtabs(formula = ~Improved + Treatment, data = Arthritis)
## Number of cases in table: 84
## Number of factors: 2
## Test for independence of all factors:
## Chisq = 13.055, df = 2, p-value = 0.001463
## 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
基于秩次的相关系数
基于秩次的相关系数常用于两种场合: 1.连续性变量不符合正态分布的时候,这个时候可以使用Spearman
相关 2.数据是等级资料的时候,这个时候最好是用Kendall
相关
- 在R当中我们可以通过修改
cor
函数当中的method
来实现修改
指标一致性评价
一致性和相关系数是不同的东西。其中一致性主要用于两种测量同时作用于同一批数据,所得的指标都是相同的,只是测量方式的不同。而相关性则一般是评价不同指标之间的相关性大小。
-
通过ICC和CCC我们来判断连续性变量的一致性检验。我们可以通过
ICC::ICCest
来进行ICC检验,可以通过cccrm::cccvc
来进行 -
如果是分类变量则可以使用Kappa检验。值得注意的是这个检验要求的分类资料类别必须是一样的,如2X2;3X3表。
-
其结果当中,Kappa值 > 0.8的时候,认为两者一致性非常好;0.6—0.8认为一致性较好;0.4-0.6认为一致性中等;9.2-0.4认为一致性勉强可以接受;<0.2则认为几乎没有一致性。
-
R当中我们可以通过
psych::cohen.kappa
进行kappa检验
相关分析当中P值的理解
相关分析当中的P值小并不代表强相关,相关分析当中的P值只能代表说数据的结果是真实的还是由抽样误差引起的。而相关系数才是告诉我变量之间的相关性有多大