Anscombes quartet is a set of 4 \(x,y\) data sets that were published by Francis Anscombe in a 1973 paper Graphs in statistical analysis. For this first question load the anscombe data that is part of the library(datasets) in R. And assign that data to a new object called data.
data("anscombe")
data <- anscombe
Summarise the data by calculating the mean, variance, for each column and the correlation between each pair (eg. x1 and y1, x2 and y2, etc) (Hint: use the fBasics() package!)
data <- anscombe
#Data Summary
summary(data)
## x1 x2 x3 x4
## Min. : 4.0 Min. : 4.0 Min. : 4.0 Min. : 8
## 1st Qu.: 6.5 1st Qu.: 6.5 1st Qu.: 6.5 1st Qu.: 8
## Median : 9.0 Median : 9.0 Median : 9.0 Median : 8
## Mean : 9.0 Mean : 9.0 Mean : 9.0 Mean : 9
## 3rd Qu.:11.5 3rd Qu.:11.5 3rd Qu.:11.5 3rd Qu.: 8
## Max. :14.0 Max. :14.0 Max. :14.0 Max. :19
## y1 y2 y3 y4
## Min. : 4.260 Min. :3.100 Min. : 5.39 Min. : 5.250
## 1st Qu.: 6.315 1st Qu.:6.695 1st Qu.: 6.25 1st Qu.: 6.170
## Median : 7.580 Median :8.140 Median : 7.11 Median : 7.040
## Mean : 7.501 Mean :7.501 Mean : 7.50 Mean : 7.501
## 3rd Qu.: 8.570 3rd Qu.:8.950 3rd Qu.: 7.98 3rd Qu.: 8.190
## Max. :10.840 Max. :9.260 Max. :12.74 Max. :12.500
#Calculating the variance
#install.packages("fBasics")
library(fBasics)
colVars(data)
## x1 x2 x3 x4 y1 y2 y3
## 11.000000 11.000000 11.000000 11.000000 4.127269 4.127629 4.122620
## y4
## 4.123249
#Determing the correlation between each pair
for(i in 1:4)
{
for(j in 5:length(data))
{
corelations <- round(cor(data[i],data[j], method = "spearman"),2)
print(corelations)
j=j+1
i=i+1
}
break
}
## y1
## x1 0.82
## y2
## x2 0.69
## y3
## x3 0.99
## y4
## x4 0.5
library(corrplot)
corrplot.mixed(cor(data), title = "Correlation of X-Y", lower.col = "black", number.cex = 0.7,
tl.srt = 90,tl.cex = 0.8, cl.ratio = 0.3, cl.align = "r", sig.level = 0.01, insig = "blank",
mar=c(0,0,1,0)
)
Create scatter plots for each \(x, y\) pair of data.
#Scatterplot for x1-y1
plot(data$x1, data$y1, main="Scatterplot of x1 vs y1",
xlab="x1 ", ylab="y1 ", pch=15, cex =1.5, col= "green")
#Scatterplot for x2-y2
plot(data$x2, data$y2, main="Scatterplot of x2 vs y2",
xlab="x2 ", ylab="y2 ", pch=17,cex =1.5, col= "red")
#Scatterplot for x3-y3
plot(data$x3, data$y3, main="Scatterplot of x3 vs y3",
xlab="x3 ", ylab="y3 ", pch=18,cex =1.5, col= "yellow")
#Scatterplot for x4-y4
plot(data$x4, data$y4, main="Scatterplot of x4 vs y4",
xlab="x4 ", ylab="y4 ", pch=19,cex =1.5, col= "blue")
Now change the symbols on the scatter plots to solid circles and plot them together as a 4 panel graphic
#Creating 4 panel diplay
par(mfrow=c(2,2), oma =c(0,0,2,0),mar=c(4,2,2,2))
#Scatterplot for x1-y1
plot(data$x1, data$y1, main="Scatterplot of x1 vs y1",
xlab="x1 ", ylab="y1 ", pch=19, cex =1.5, col= "green")
#Scatterplot for x1-y1
plot(data$x2, data$y2, main="Scatterplot of x2 vs y2",
xlab="x2 ", ylab="y2 ", pch=19,cex =1.5, col= "red")
#Scatterplot for x1-y1
plot(data$x3, data$y3, main="Scatterplot of x3 vs y3",
xlab="x3 ", ylab="y3 ", pch=19,cex =1.5, col= "yellow")
#Scatterplot for x1-y1
plot(data$x4, data$y4, main="Scatterplot of x4 vs y4",
xlab="x4 ", ylab="y4 ", pch=19,cex =1.5, col= "blue")
#Adding title to the 4-panel display
title(main = "Scatterplots of X-Y", outer = TRUE , cex.main = 1.75, font.main = 4,
col.main = rgb(0.1,0.3,0.5,0.5))
Now fit a linear model to each data set using the lm() function.
#Model 1
model_x1y1 <- lm(formula = y1~x1, data = data)
summary(model_x1y1)
##
## Call:
## lm(formula = y1 ~ x1, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.92127 -0.45577 -0.04136 0.70941 1.83882
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.0001 1.1247 2.667 0.02573 *
## x1 0.5001 0.1179 4.241 0.00217 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.237 on 9 degrees of freedom
## Multiple R-squared: 0.6665, Adjusted R-squared: 0.6295
## F-statistic: 17.99 on 1 and 9 DF, p-value: 0.00217
#Model 2
model_x2y2 <- lm(formula = y2~x2, data = data)
summary(model_x2y2)
##
## Call:
## lm(formula = y2 ~ x2, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.9009 -0.7609 0.1291 0.9491 1.2691
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.001 1.125 2.667 0.02576 *
## x2 0.500 0.118 4.239 0.00218 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.237 on 9 degrees of freedom
## Multiple R-squared: 0.6662, Adjusted R-squared: 0.6292
## F-statistic: 17.97 on 1 and 9 DF, p-value: 0.002179
#Model 3
model_x3y3 <- lm(formula = y3~x3, data = data)
summary(model_x3y3)
##
## Call:
## lm(formula = y3 ~ x3, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.1586 -0.6146 -0.2303 0.1540 3.2411
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.0025 1.1245 2.670 0.02562 *
## x3 0.4997 0.1179 4.239 0.00218 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.236 on 9 degrees of freedom
## Multiple R-squared: 0.6663, Adjusted R-squared: 0.6292
## F-statistic: 17.97 on 1 and 9 DF, p-value: 0.002176
#Model 4
model_x4y4 <- lm(formula = y4~x4, data = data)
summary(model_x4y4)
##
## Call:
## lm(formula = y4 ~ x4, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.751 -0.831 0.000 0.809 1.839
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.0017 1.1239 2.671 0.02559 *
## x4 0.4999 0.1178 4.243 0.00216 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.236 on 9 degrees of freedom
## Multiple R-squared: 0.6667, Adjusted R-squared: 0.6297
## F-statistic: 18 on 1 and 9 DF, p-value: 0.002165
From above, all the models have relatively equal significance.
Now combine the last two tasks. Create a four panel scatter plot matrix that has both the data points and the regression lines. (hint: the model objects will carry over chunks!)
#Creating 4 panel diplay
par(mfrow=c(2,2), oma =c(0,0,2,0),mar=c(4,2,2,2))
#Scatterplot for x1-y1 with regression line (y1~x1)
plot(data$x1, data$y1, main="Scatterplot of x1 vs y1",
xlab="x1 ", ylab="y1 ", pch=19, cex =1.5, col= "green", abline(model_x1y1, col = "red", lwd=2))
#Scatterplot for x1-y1 with regression line (y2~x2)
plot(data$x2, data$y2, main="Scatterplot of x2 vs y2",
xlab="x2 ", ylab="y2 ", pch=19,cex =1.5, col= "black", abline(model_x2y2, col = "red", lwd=2))
#Scatterplot for x1-y1 with regression line (y3~x3)
plot(data$x3, data$y3, main="Scatterplot of x3 vs y3",
xlab="x3 ", ylab="y3 ", pch=19,cex =1.5, col= "yellow", abline(model_x3y3, col = "red", lwd=2))
#Scatterplot for x1-y1 with regression line (y4~x4)
plot(data$x4, data$y4, main="Scatterplot of x4 vs y4",
xlab="x4 ", ylab="y4 ", pch=19,cex =1.5, col= "blue", abline(model_x4y4, col = "red",lwd=2))
#Adding title to the 4-panel display
title(main = "Scattleplots of X-Y with regression lines", outer = TRUE , cex.main = 1.75, font.main = 4, col.main = rgb(0.1,0.3,0.5,0.5))
The first scatter plot (top left) displays simple linear relationship between x1 and y1, thus following normality.
The second graph (top right) displays a non-linear relationship between x2 and y2 and hence the data points in the set are not distributed normally.
The third graph (bottom left), displays a linear distribution, however, the presence of one outlier lowers the regression correlation coefficient considerably.
Finally, the fourth graph (bottom right) displays a non significant relationship between x4 and y4, however, one outlier is enought to produce a high correlation coefficient.
Now compare the model fits for each model object. #### Answer
anova(model_x1y1)
Analysis of Variance Table
Response: y1 Df Sum Sq Mean Sq F value Pr(>F)
x1 1 27.510 27.5100 17.99 0.00217 ** Residuals 9 13.763 1.5292
— Signif. codes: 0 ‘’ 0.001 ’’ 0.01 ’’ 0.05 ‘.’ 0.1 ‘’ 1
anova(model_x2y2)
Analysis of Variance Table
Response: y2 Df Sum Sq Mean Sq F value Pr(>F)
x2 1 27.500 27.5000 17.966 0.002179 ** Residuals 9 13.776 1.5307
— Signif. codes: 0 ‘’ 0.001 ’’ 0.01 ’’ 0.05 ‘.’ 0.1 ‘’ 1
anova(model_x3y3)
Analysis of Variance Table
Response: y3 Df Sum Sq Mean Sq F value Pr(>F)
x3 1 27.470 27.4700 17.972 0.002176 ** Residuals 9 13.756 1.5285
— Signif. codes: 0 ‘’ 0.001 ’’ 0.01 ’’ 0.05 ‘.’ 0.1 ‘’ 1
anova(model_x4y4)
Analysis of Variance Table
Response: y4 Df Sum Sq Mean Sq F value Pr(>F)
x4 1 27.490 27.4900 18.003 0.002165 ** Residuals 9 13.742 1.5269
— Signif. codes: 0 ‘’ 0.001 ’’ 0.01 ’’ 0.05 ‘.’ 0.1 ‘’ 1
From above, all the models have relatively equal significance.
In text, summarize the lesson of Anscombe’s Quartet and what it says about the value of data visualization.
Anscombe’s quartet comprises of four datasets that have nearly identical simple descriptive statistics displaying approximatley equivalent results (Coefficients, R Squared, Residual Standard errors, and p-values) using regression modelling, yet appear very different when visualized.
Thus, data visualization makes us more aware by providing a complete picture of the dataset describing the entire perspective on a more intutive level, thereby overcoming the shortcomings of the descriptive data analysis. It presents the data in a way the human brain processes information, using charts or graphs to visualize large amounts of complex data that is easier than poring over spreadsheets or reports. Data visualization is a quick, easy way to convey concepts in a universal manner where you can experiment with different scenarios by making slight adjustments.