[23] 《R数据科学》探索数据分析

没有一成不变的统计问题,统计上的一成不变都是有问题的 ——Sir David Cox

对分布进行可视化表示

对变量进行可视化表示取决于变量是分类变量还是连续变量。如果在较小的集合内取值,那么这个变量就是分类变量。要想检查分类变量的分布可以使用条形图

library(ggplot2)
ggplot(diamonds)+geom_bar(aes(cut))
image.png

条形图的高度表示每个x中观测的数量,可以使用dplyr::count()手动计算出这些值:

library(dplyr)
diamonds %>% count(cut)
# A tibble: 5 x 2
  cut           n
       
1 Fair       1610
2 Good       4906
3 Very Good 12082
4 Premium   13791
5 Ideal     21551

如果在无限大的有序集合中连续取值,这个变量就是连续变量。数值型和日期型就是连续变量的例子。要检查连续变量的分布,可以使用直方图

 ggplot(diamonds)+geom_histogram(aes(carat),binwidth = 0.5)

image.png

可以通过dplyr::count()ggplot2::cut_width()函数的组合来手动计算结果:

diamonds %>% count(cut_width(carat,0.5))
# A tibble: 11 x 2
   `cut_width(carat, 0.5)`     n
                      
 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

直方图会对x轴进行等宽分箱,binwidth参数可以设定直方图的间隔宽度。
如果只考虑质量小于3克拉的钻石,并选择一个更小的分箱宽度来绘制直方图:

smaller <- diamonds %>%  filter(carat<3)
ggplot(smaller)+geom_histogram(aes(carat),binwidth=0.1)

image.png

如果想要在同一张图上叠加多个直方图,建议使用geom_freqpoly()函数来代替geom_histogram()函数,geom_freqpoly()可以执行和geom_histogram()同样的计算过程,但前者不使用条形来显示计数,而是使用折线:

ggplot(smaller,aes(carat,color=cut))+geom_freqpoly(binwidth=0.1)
image.png

典型值

条形图和直方图都会用比较高的条形表示变量中的常见值,而用比较矮的条形表示变量中不常见的值。没有条形的位置表示数据中没有这样的值。
作为示例可以从以下直方图中发现几个有趣的问题。

  • 为什么重量为整数克拉和常见分数克拉的钻石更多?
  • 为什么位于每个峰值稍偏右的钻石比稍偏左的钻石更多?
  • 为什么没有重量超过3克拉的钻石?
ggplot(smaller,aes(carat))+geom_histogram(binwidth = 0.01)
image.png

异常值

异常值是与众不同的观测或者是模式之外的数据点。查看钻石数据集中y轴的变量分布:

ggplot(diamonds,aes(y))+geom_histogram(binwidth = 0.5)

image.png

此时y的分布范围出奇的宽,我们可以限定它的取值范围,使用coord_cartesian()函数将y靠近0的部分放大。

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

这样就可以看出有三个异常值,使用dplyr找出:

unusual <- diamonds %>% filter(y<3|y>20) %>% arrange(y)
unusual
# A tibble: 9 x 10
  carat cut       color clarity depth table price     x     y     z
                 
1  1    Very Good H     VS2      63.3    53  5139  0      0    0   
2  1.14 Fair      G     VS1      57.5    67  6381  0      0    0   
3  1.56 Ideal     G     VS2      62.2    54 12800  0      0    0   
4  1.2  Premium   D     VVS1     62.1    59 15686  0      0    0   
5  2.25 Premium   H     SI2      62.8    59 18034  0      0    0   
6  0.71 Good      F     SI2      64.1    60  2130  0      0    0   
7  0.71 Good      F     SI2      64.1    60  2130  0      0    0   
8  0.51 Ideal     E     VS1      61.8    55  2075  5.15  31.8  5.12
9  2    Premium   H     SI2      58.9    57 12210  8.09  58.9  8.06

y变量是钻石得三维度之一,钻石的宽度不可能为0,所以这些值肯定是错误的,而32mm和59mm的宽度也并不符合客观事实
如果要剔除异常值并绘图,结果如下:

unusual <- diamonds %>% filter(y>=3&y<=20) %>% arrange(y)
unusual
# A tibble: 53,931 x 10
   carat cut       color clarity depth table price     x     y     z
                  
 1  0.2  Premium   D     VS2      62.3    60   367  3.73  3.68  2.31
 2  0.2  Premium   F     VS2      62.6    59   367  3.73  3.71  2.33
 3  0.2  Very Good E     VS2      63.4    59   367  3.74  3.71  2.36
 4  0.2  Premium   D     VS2      61.7    60   367  3.77  3.72  2.31
 5  0.2  Ideal     E     VS2      62.2    57   367  3.76  3.73  2.33
 6  0.2  Premium   E     SI2      60.2    62   345  3.79  3.75  2.27
 7  0.2  Premium   E     VS2      59.8    62   367  3.79  3.77  2.26
 8  0.2  Ideal     D     VS2      61.5    57   367  3.81  3.77  2.33
 9  0.22 Fair      E     VS2      65.1    61   337  3.87  3.78  2.49
10  0.2  Premium   E     VS2      59      60   367  3.81  3.78  2.24
# ... with 53,921 more rows
ggplot(unusual)+geom_histogram(aes(y),binwidth = 0.5)
image.png

你可能感兴趣的:([23] 《R数据科学》探索数据分析)