443-970-2353 [email protected] CV Resume
Before we perform any analysis and come up with any assumptions about the distributions of and relationships between variables in our datasets, it is always a good idea to visualize our data in order to understand their properties and identify appropriate analytics techniques. In this post, let's see the dramatic differences in conclutions that we can make based on (1) simple statistics only, and (2) data visualization.
The Anscombe dataset, which is found in the base R datasets packege, is handy for showing the importance of data visualization in data analysis. It consists of four datasets and each dataset consists of eleven (x,y) points.
Create four groups: setA, setB, setC and setD.
library(ggplot2) library(dplyr) 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)
Add a third column which can help us to identify the four groups.
setA$group ='SetA' setB$group ='SetB' setC$group ='SetC' setD$group ='SetD' head(setA,4) # showing sample data points from setA
Now, let's merge the four datasets.
all_data=rbind(setA,setB,setC,setD) # merging all the four data sets all_data[c(1,13,23,43),] # showing sample
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") summary_stats_and_linear_fit = cbind(summary_stats, data_frame("Linear regression" = paste0("y = ",models$"(Intercept)"," + ",models$x,"x"))) summary_stats_and_linear_fit
|group||mean x||Sample variance x||mean y||Sample variance y||Correlation between x and y||Linear regression|
|1||SetA||9||11||7.5||4.1||0.82||y = 3 + 0.5x|
|2||SetB||9||11||7.5||4.1||0.82||y = 3 + 0.5x|
|3||SetC||9||11||7.5||4.1||0.82||y = 3 + 0.5x|
|4||SetD||9||11||7.5||4.1||0.82||y = 3 + 0.5x|
If we look only at the simple summary statistics shown above, we would conclude that these four data sets are identical.
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")
As we can see from the figures above, the datasets are very different from each other. The Anscombe's quartet is a good example that shows that we have to visualize the relatonships, distributuions and outliers of our data and we shoul not rely only on simple statistics.
We should look at the data graphically before we start analysis. Further, we should understand that basic statistics properties can often fail to capture real-world complexities (such as outliers, relationships and complex distributions) since summary statistics do not capture all of the complexities of the data.