R语言学习R数据科学

R数据科学-第五章 EDA

2021-01-02  本文已影响0人  Seurat_Satija

1.简介

提出问题→通过可视化,转换和建模来解决问题→优化并提出新问题

2.提出问题

变量自身会发生何种变动?
变量之间会发生何种相关变动?
术语:在tidydata(整洁的数据)中,行是观测,列是变量。

3.变动

3.1对分布进行可视化表示

分类变量--例如diamonds中的cut
图片
> library(tidyverse)
-- Attaching packages ------------------------------------------- tidyverse 1.3.0 --
√ ggplot2 3.3.2     √ purrr   0.3.4
√ tibble  3.0.4     √ dplyr   1.0.2
√ tidyr   1.1.2     √ stringr 1.4.0
√ readr   1.4.0     √ forcats 0.5.0
-- Conflicts ---------------------------------------------- tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()
> diamonds#先library才可以调用内置数据集
# A tibble: 53,940 x 10
   carat cut       color clarity depth table price     x     y     z
   <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
 1 0.23  Ideal     E     SI2      61.5    55   326  3.95  3.98  2.43
 2 0.21  Premium   E     SI1      59.8    61   326  3.89  3.84  2.31
 3 0.23  Good      E     VS1      56.9    65   327  4.05  4.07  2.31
 4 0.290 Premium   I     VS2      62.4    58   334  4.2   4.23  2.63
 5 0.31  Good      J     SI2      63.3    58   335  4.34  4.35  2.75
 6 0.24  Very Good J     VVS2     62.8    57   336  3.94  3.96  2.48
 7 0.24  Very Good I     VVS1     62.3    57   336  3.95  3.98  2.47
 8 0.26  Very Good H     SI1      61.9    55   337  4.07  4.11  2.53
 9 0.22  Fair      E     VS2      65.1    61   337  3.87  3.78  2.49
10 0.23  Very Good H     VS1      59.4    61   338  4     4.05  2.39
# ... with 53,930 more rows
> View(diamonds)

第一件事还是端详数据:

复习一下如何查看每列的非重复值

distinct(diamonds,cut)
# A tibble: 5 x 1
  cut      
  <ord>    
1 Ideal    
2 Premium  
3 Good     
4 Very Good
5 Fair    

如何查看每个非重复值出现的次数

count(diamonds,cut)
# A tibble: 5 x 2
  cut           n
  <ord>     <int>
1 Fair       1610
2 Good       4906
3 Very Good 12082
4 Premium   13791
5 Ideal     21551

cut仅有5个固定的取值,是分类变量,可以用条形图进行可视化。

> #library(tidyverse)
> ggplot(data = diamonds) +
+   geom_bar(mapping = aes(x = cut))
image.png

其中,binwidth是定义等宽分箱的宽度,也可以用bins定义分箱数。
在同一张图中叠加多个直方图,用geom_freqpoly()代替。
统计每个区间的观测数量

diamonds %>% 
  count(cut_width(carat, 0.5))
# A tibble: 11 x 2
   `cut_width(carat, 0.5)`     n
   <fct>                   <int>
 1 [-0.25,0.25]              785
 2 (0.25,0.75]             29498
 3 (0.75,1.25]             15977
 4 (1.25,1.75]              5313
 5 (1.75,2.25]              2002
 6 (2.25,2.75]               322
 7 (2.75,3.25]                32
 8 (3.25,3.75]                 5
 9 (3.75,4.25]                 4
10 (4.25,4.75]                 1
11 (4.75,5.25]                 1

3.2.典型值
直方图或条形图较高的读数,表示常见值。聚集成簇成为子组。

3.3.异常值
以diamonds中的y列为例作直方图,发现x轴取值范围出气地宽。(此处翻译有误,原文是x轴)

ggplot(diamonds) + 
  geom_histogram(mapping = aes(x = y), binwidth = 0.5)
image.png
ggplot(diamonds) + 
  geom_histogram(mapping = aes(x = y), binwidth = 0.5) +
  coord_cartesian(ylim = c(0, 50))
image.png

找出异常值

filter(diamonds,y < 3 | y > 20) %>%   select(price, x, y, z) %>%  arrange(y)
# A tibble: 9 x 4
  price     x     y     z
  <int> <dbl> <dbl> <dbl>
1  5139  0      0    0   
2  6381  0      0    0   
3 12800  0      0    0   
4 15686  0      0    0   
5 18034  0      0    0   
6  2130  0      0    0   
7  2130  0      0    0   
8  2075  5.15  31.8  5.12
9 12210  8.09  58.9  8.06
图片

在异常值中,有7个是缺失值,长宽高都是零蛋。

4.缺失值

#异常值的处理方法:#(1)丢弃diamonds2 <- diamonds %>%   filter(between(y, 3, 20))#(2)用缺失值代替异常值diamonds2 <- diamonds %>%   mutate(y = ifelse(y < 3 | y > 20, NA, y))#表示如果符合要求,就替换为NA
#ggplot中明确移除缺失值的命令:na.rm=TRUEggplot(data = diamonds2, mapping = aes(x = x, y = y)) +   geom_point(na.rm = TRUE)
image.png

flights中取消的航班:

nycflights13::flights %>%   mutate(    cancelled = is.na(dep_time),    sched_hour = sched_dep_time %/% 100,    sched_min = sched_dep_time %% 100,    sched_dep_time = sched_hour + sched_min / 60  ) %>%   ggplot(mapping = aes(sched_dep_time)) +     geom_freqpoly(mapping = aes(colour = cancelled), binwidth = 1/4)
image.png

新加的cancelled变量是逻辑值,TRUE和FALSE,所以将颜色映射给它,就自然分成了两组,变成了两条曲线。
练习(1):直方图会丢弃缺失值,条形图则会将NA单列为一个分类,统计其数目。

5.相关变动

(1)分类变量与连续变量

其实钻石质量、钻石切割水平都不是决定价格的决定性因素,因此出现负相关很正常,可以不理他。
reorder函数对变量进行重排序:
(请注意例子是根据hwy来对class进行重排序)

ggplot(data = mpg) +  geom_boxplot(mapping = aes(x = reorder(class, hwy, FUN = median), y = hwy)) #FUN是分类依据
image.png

翻转在ggplot2中讲过,用coord_flip()

(2)两个分类变量

geom_count和geom_tile
例子是diamonds中cut和color的相关变动,统计每个组合的观测数量:

count(diamonds,color,cut) ggplot(data = diamonds) +  geom_count(mapping = aes(x = cut, y = color)) #count的可视化表示
image.png
diamonds %>%   count(color, cut) %>%    ggplot(mapping = aes(x = color, y = cut)) +    geom_tile(mapping = aes(fill = n)) #geom_tile更好看
image.png

(3)两个连续变量

ggplot(data = diamonds) +  geom_point(mapping = aes(x = carat, y = price)) #散点图
image.png
ggplot(data = diamonds) +   geom_point(mapping = aes(x = carat, y = price), alpha = 1 / 100)#设置透明度更美观的散点图\
image.png
smaller <- diamonds %>%   filter(carat < 3)ggplot(data = smaller) +  geom_bin2d(mapping = aes(x = carat, y = price))#矩形分箱
image.png
# install.packages("hexbin")ggplot(data = smaller) +  geom_hex(mapping = aes(x = carat, y = price))#六边形分箱
image.png

将其中一个连续变量分箱,使其相当于分类变量,每组生成一个箱线图。其缺点是不体现每个分箱的观测数量。
设置var.width=TRUE使箱线图的宽度与观测数量成正比,或用cut_number设置箱线图分组的数目。

ggplot(data = smaller, mapping = aes(x = carat, y = price)) +   geom_boxplot(mapping = aes(group = cut_width(carat, 0.1)))
image.png

6.模式和模型

模式是两个变量之间存在的系统性关系,可以揭示相关变动。
如果说变动会生成不确定性,那么相关变动就会减少不确定性。
如果两个变量共同变化,可以根据一个预测另一个
如果相关变动归因于因果关系,可以使用一个控制另一个。
“模型是用于从数据中抽取模式的一种工具”

library(modelr)
mod <- lm(log(price) ~ log(carat), data = diamonds)
diamonds2 <- diamonds %>%   add_residuals(mod) %>%   mutate(resid = exp(resid))
ggplot(data = diamonds2) +   geom_point(mapping = aes(x = carat, y = resid))
image.png

参考
详解《R数据科学》--第五章 EDA

上一篇下一篇

猜你喜欢

热点阅读