原文链接:http://datascienceplus.com/the-importance-of-data-visualization/ by Fisseha Berhane

在我们对数据中的变量的数学分布及其相互关系进行任何分析和建立任何假设之前,为了便于理解它们的特征和找到合适的分析技术,对数据进行可视化通常是一个好主意。在本文当中,我会展现我们利用(1)简单的统计和(2)数据可视化所能得出的结论之间的巨大差异。

安斯库姆数据

我们所要使用的是安斯库姆数据(Anscombe dataset),又被称之为安斯庫姆四重奏(Anscombe’s quartet)。这个数据在R软件的基础库里就可以找到,通过它可以非常方便地说明数据可视化的重要性。这个数据包含了四个数据,每一个数据都有11个(x, y)数据点。

anscombe 
##    x1 x2 x3 x4    y1   y2    y3    y4
## 1  10 10 10  8  8.04 9.14  7.46  6.58
## 2   8  8  8  8  6.95 8.14  6.77  5.76
## 3  13 13 13  8  7.58 8.74 12.74  7.71
## 4   9  9  9  8  8.81 8.77  7.11  8.84
## 5  11 11 11  8  8.33 9.26  7.81  8.47
## 6  14 14 14  8  9.96 8.10  8.84  7.04
## 7   6  6  6  8  7.24 6.13  6.08  5.25
## 8   4  4  4 19  4.26 3.10  5.39 12.50
## 9  12 12 12  8 10.84 9.13  8.15  5.56
## 10  7  7  7  8  4.82 7.26  6.42  7.91
## 11  5  5  5  8  5.68 4.74  5.73  6.89

为了便于分析和可视化,我们在R软件当中进行一下处理。

创建四个数据组: setA, setB, setC and setD.

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.2.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.2.5
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(reshape2)

setA=select(anscombe, x=x1,y=y1)
setB=select(anscombe, x=x2,y=y2)
setC=select(anscombe, x=x3,y=y3)
setD=select(anscombe, x=x4,y=y4)

建立一个新的名为group的列,它可以帮助我们区分四组数据。

setA$group ='SetA'
setB$group ='SetB'
setC$group ='SetC'
setD$group ='SetD'

head(setA,4)  #  看一下setA数据的前几行
##    x    y group
## 1 10 8.04  SetA
## 2  8 6.95  SetA
## 3 13 7.58  SetA
## 4  9 8.81  SetA

现在,让我们将这四个数据合并起来。

all_data=rbind(setA,setB,setC,setD)  # 合并所有的四个数据
all_data[c(1,13,23,43),]  # 展现其中的一些行
##     x    y group
## 1  10 8.04  SetA
## 13  8 8.14  SetB
## 23 10 7.46  SetC
## 43  8 7.91  SetD

比较它们的一些统计结果(均值、方差、相关系数):

summary_stats =all_data%>%group_by(group)%>%summarize("mean x"=mean(x),
                       "Sample variance x"=var(x),
                       "mean y"=round(mean(y),2),
                       "Sample variance y"=round(var(y),1),
                       'Correlation between x and y '=round(cor(x,y),2) )

还有简单线性回归模型的结果:

models = all_data %>% 
      group_by(group) %>%
      do(mod = lm(y ~ x, data = .)) %>%
      do(data.frame(var = names(coef(.$mod)),
                    coef = round(coef(.$mod),2),
                    group = .$group)) %>%
dcast(., group~var, value.var = "coef")
## Warning in bind_rows_(x, .id): Unequal factor levels: coercing to character

将简单统计结果和线性回归的结果放在一起:

summary_reg = data_frame("Linear regression" = paste0("y = ",models$"(Intercept)"," + ",models$x,"x"))
summary_stats_and_linear_fit = cbind(summary_stats,  summary_reg)

来看一下简单统计的结果:

summary_stats_and_linear_fit
##   group mean x Sample variance x mean y Sample variance y
## 1  SetA      9                11    7.5               4.1
## 2  SetB      9                11    7.5               4.1
## 3  SetC      9                11    7.5               4.1
## 4  SetD      9                11    7.5               4.1
##   Correlation between x and y  Linear regression
## 1                         0.82      y = 3 + 0.5x
## 2                         0.82      y = 3 + 0.5x
## 3                         0.82      y = 3 + 0.5x
## 4                         0.82      y = 3 + 0.5x

我们会发现这四组数据中,x值的平均数都是9.0,y值的平均数都是7.5;x值的方差都是10.0,y值的方差都是3.75;它们的相关度都是0.816,线性回归线都是y=3+0.5x。也就是说单从这些统计数字上看来,四组数据所反映出的实际情况非常相近,而事实上,这四组数据有着天壤之别。

让我们来通过可视化来展现这些数据:

 ggplot(all_data, aes(x=x,y=y)) +geom_point(shape = 21, colour = "red", fill = "orange", size = 3)+
    ggtitle("Anscombe's data sets")+geom_smooth(method = "lm",se = FALSE,color='blue') + 
    facet_wrap(~group, scales="free")