Objectives

The objectives of this problem set is to orient you to a number of activities in R. And to conduct a thoughtful exercise in appreciating the importance of data visualization. For each question create a code chunk or text response that completes/answers the activity or question requested. Finally, upon completion name your final output .html file as: YourName_ANLY512-Section-Year-Semester.html and upload it to the “Problem Set 2” assignment to your R Pubs account and submit the link to Moodle. Points will be deducted for uploading the improper format.

Questions

  1. 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.
library(datasets)
data = anscombe
  1. 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!)
## 
## The downloaded binary packages are in
##  /var/folders/12/rwgk87hx5zx84v6tt6jpj5l40000gn/T//Rtmp8eT5gZ/downloaded_packages
## Loading required package: timeDate
## Loading required package: timeSeries
##       x1       x2       x3       x4       y1       y2       y3       y4 
## 9.000000 9.000000 9.000000 9.000000 7.500909 7.500909 7.500000 7.500909
##        x1        x2        x3        x4        y1        y2        y3 
## 11.000000 11.000000 11.000000 11.000000  4.127269  4.127629  4.122620 
##        y4 
##  4.123249
##            x1         x2         x3         x4         y1         y2
## x1  1.0000000  1.0000000  1.0000000 -0.5000000  0.8164205  0.8162365
## x2  1.0000000  1.0000000  1.0000000 -0.5000000  0.8164205  0.8162365
## x3  1.0000000  1.0000000  1.0000000 -0.5000000  0.8164205  0.8162365
## x4 -0.5000000 -0.5000000 -0.5000000  1.0000000 -0.5290927 -0.7184365
## y1  0.8164205  0.8164205  0.8164205 -0.5290927  1.0000000  0.7500054
## y2  0.8162365  0.8162365  0.8162365 -0.7184365  0.7500054  1.0000000
## y3  0.8162867  0.8162867  0.8162867 -0.3446610  0.4687167  0.5879193
## y4 -0.3140467 -0.3140467 -0.3140467  0.8165214 -0.4891162 -0.4780949
##            y3         y4
## x1  0.8162867 -0.3140467
## x2  0.8162867 -0.3140467
## x3  0.8162867 -0.3140467
## x4 -0.3446610  0.8165214
## y1  0.4687167 -0.4891162
## y2  0.5879193 -0.4780949
## y3  1.0000000 -0.1554718
## y4 -0.1554718  1.0000000
  1. Create scatter plots for each \(x, y\) pair of data.
plot(data$x1,data$y1)

plot(data$x2,data$y2)

plot(data$x3,data$y3)

plot(data$x4,data$y4)

  1. Now change the symbols on the scatter plots to solid circles and plot them together as a 4 panel graphic
attach(data)
par(mfrow=c(2,2))
plot(x1,y1,pch = 19)
plot(x2,y2, pch = 19)
plot(x3,y3,pch = 19 )
plot(x4,y4, pch = 19)

#From the scatter plots panle we could see that these four datset are actually very different from each other. 
  1. Now fit a linear model to each data set using the lm() function.
attach(data)
## The following objects are masked from data (pos = 3):
## 
##     x1, x2, x3, x4, y1, y2, y3, y4
lm1 = lm(x1 ~ y1)
lm2 = lm(x2 ~ y2)
lm3 = lm(x3 ~ y3)
lm4 = lm(x4 ~ y4)
  1. 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!)
attach(data)
## The following objects are masked from data (pos = 3):
## 
##     x1, x2, x3, x4, y1, y2, y3, y4
## The following objects are masked from data (pos = 4):
## 
##     x1, x2, x3, x4, y1, y2, y3, y4
par(mfrow=c(2,2))
plot(x1,y1, pch = 19)
abline(lm1)
plot(x2,y2, pch = 19)
abline(lm2)
plot(x3,y3, pch = 19)
abline(lm3)
plot(x4,y4, pch = 19)
abline(lm4)

#The regression line is the same for four plots, but the data points are different. 
  1. Now compare the model fits for each model object.
summary(lm1)

Call: lm(formula = x1 ~ y1)

Residuals: Min 1Q Median 3Q Max -2.6522 -1.5117 -0.2657 1.2341 3.8946

Coefficients: Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.9975 2.4344 -0.410 0.69156
y1 1.3328 0.3142 4.241 0.00217 ** — Signif. codes: 0 ‘’ 0.001 ’’ 0.01 ’’ 0.05 ‘.’ 0.1 ‘’ 1

Residual standard error: 2.019 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

summary(lm2)

Call: lm(formula = x2 ~ y2)

Residuals: Min 1Q Median 3Q Max -1.8516 -1.4315 -0.3440 0.8467 4.2017

Coefficients: Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.9948 2.4354 -0.408 0.69246
y2 1.3325 0.3144 4.239 0.00218 ** — Signif. codes: 0 ‘’ 0.001 ’’ 0.01 ’’ 0.05 ‘.’ 0.1 ‘’ 1

Residual standard error: 2.02 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

summary(lm3)

Call: lm(formula = x3 ~ y3)

Residuals: Min 1Q Median 3Q Max -2.9869 -1.3733 -0.0266 1.3200 3.2133

Coefficients: Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.0003 2.4362 -0.411 0.69097
y3 1.3334 0.3145 4.239 0.00218 ** — Signif. codes: 0 ‘’ 0.001 ’’ 0.01 ’’ 0.05 ‘.’ 0.1 ‘’ 1

Residual standard error: 2.019 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

summary(lm4)

Call: lm(formula = x4 ~ y4)

Residuals: Min 1Q Median 3Q Max -2.7859 -1.4122 -0.1853 1.4551 3.3329

Coefficients: Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.0036 2.4349 -0.412 0.68985
y4 1.3337 0.3143 4.243 0.00216 ** — Signif. codes: 0 ‘’ 0.001 ’’ 0.01 ’’ 0.05 ‘.’ 0.1 ‘’ 1

Residual standard error: 2.018 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 the output we could see that each model has a relatively same r-squared, 0.667. 
  1. In text, summarize the lesson of Anscombe’s Quartet and what it says about the value of data visualization.

Anscombe’s Quartet is a great example to demonstrate the power of data visualization. The above four datasets have exactly the same statistic parameters: mean, variance, correlation, and r-squared, but their data point components and scatter plots are so varied from each others. We could not realize how different these 4 pairs of data are without using the data visualization. Therefore, we could say that the value of merely statistic parameters is limited. That’s why we should introduce data visualization into data analysis. It is a great tool to reveal multiple dimensions of the dataset and help us to understand the dataset in a clearer and more straightforward way.