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!)
colMeans(data)
##       x1       x2       x3       x4       y1       y2       y3       y4 
## 9.000000 9.000000 9.000000 9.000000 7.500909 7.500909 7.500000 7.500909
c(var(data$x1), var(data$y1), var(data$x2), var(data$y2), var(data$x3), var(data$y3), var(data$x4), var(data$y4))
## [1] 11.000000  4.127269 11.000000  4.127629 11.000000  4.122620 11.000000
## [8]  4.123249
  1. Create scatter plots for each \(x, y\) pair of data.
plot(data$x1, data$y1, main="Scatter plot for (x1, y1)", xlab="x1", ylab="y1")

plot(data$x2, data$y2, main="Scatter plot for (x2, y2)", xlab="x2", ylab="y2")

plot(data$x3, data$y3, main="Scatter plot for (x3, y3)", xlab="x3", ylab="y3")

plot(data$x4, data$y4, main="Scatter plot for (x4, y4)", xlab="x4", ylab="y4")

  1. Now change the symbols on the scatter plots to solid circles and plot them together as a 4 panel graphic
op <- par(mfcol = c(2, 2))
c11 <- plot(data$x1, data$y1, main="Scatter plot for (x1, y1)", xlab="x1", ylab="y1", pch=19)
c21 <- plot(data$x2, data$y2, main="Scatter plot for (x2, y2)", xlab="x2", ylab="y2", pch=19)
c12 <- plot(data$x3, data$y3, main="Scatter plot for (x3, y3)", xlab="x3", ylab="y3", pch=19)
c22 <- plot(data$x4, data$y4, main="Scatter plot for (x4, y4)", xlab="x4", ylab="y4", pch=19)

  1. Now fit a linear model to each data set using the lm() function.
lm(formula = data$y1 ~ data$x1, data = data)
## 
## Call:
## lm(formula = data$y1 ~ data$x1, data = data)
## 
## Coefficients:
## (Intercept)      data$x1  
##      3.0001       0.5001
lm(formula = data$y2 ~ data$x2, data = data)
## 
## Call:
## lm(formula = data$y2 ~ data$x2, data = data)
## 
## Coefficients:
## (Intercept)      data$x2  
##       3.001        0.500
lm(formula = data$y3 ~ data$x3, data = data)
## 
## Call:
## lm(formula = data$y3 ~ data$x3, data = data)
## 
## Coefficients:
## (Intercept)      data$x3  
##      3.0025       0.4997
lm(formula = data$y4 ~ data$x4, data = data)
## 
## Call:
## lm(formula = data$y4 ~ data$x4, data = data)
## 
## Coefficients:
## (Intercept)      data$x4  
##      3.0017       0.4999
  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!)
op <- par(mfcol = c(2, 2))
c11 <- plot(data$x1, data$y1, main="Scatter plot for (x1, y1)", xlab="x1", ylab="y1", pch=19)
abline(lm(data$y1 ~ data$x1))
c21 <- plot(data$x2, data$y2, main="Scatter plot for (x2, y2)", xlab="x2", ylab="y2", pch=19)
abline(lm(data$y2 ~ data$x2))
c12 <- plot(data$x3, data$y3, main="Scatter plot for (x3, y3)", xlab="x3", ylab="y3", pch=19)
abline(lm(data$y3 ~ data$x3))
c22 <- plot(data$x4, data$y4, main="Scatter plot for (x4, y4)", xlab="x4", ylab="y4", pch=19)
abline(lm(data$y4 ~ data$x4))

  1. Now compare the model fits for each model object.
summary(lm(data$y1 ~ data$x1))$adj.r.squared

[1] 0.6294916

summary(lm(data$y2 ~ data$x2))$adj.r.squared

[1] 0.6291578

summary(lm(data$y3 ~ data$x3))$adj.r.squared

[1] 0.6292489

summary(lm(data$y4 ~ data$x4))$adj.r.squared

[1] 0.6296747

  1. In text, summarize the lesson of Anscombe’s Quartet and what it says about the value of data visualization.

Data visualization shows some information which may not be available from the summary itself. To be more specific, the coefficients, R squared, residual standard errors, and p-values are roughtly the same for the data sets. However, looking at plots, we will be able to see the differences.