分析方法统计生物统计

白话统计学——相关分析

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相关

指标一致性评价

一致性和相关系数是不同的东西。其中一致性主要用于两种测量同时作用于同一批数据,所得的指标都是相同的,只是测量方式的不同。而相关性则一般是评价不同指标之间的相关性大小。

相关分析当中P值的理解

相关分析当中的P值小并不代表强相关,相关分析当中的P值只能代表说数据的结果是真实的还是由抽样误差引起的。而相关系数才是告诉我变量之间的相关性有多大

上一篇 下一篇

猜你喜欢

热点阅读