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 enter your code or text response in the code chunk that completes/answers the activity or question requested. To submit this homework you will create the document in Rstudio, using the knitr package (button included in Rstudio) and then submit the document to your Rpubs account. Once uploaded you will submit the link to that document on Canvas. Please make sure that this link is hyper linked and that I can see the visualization and the code required to create it. Each question is worth 5 points.

Questions

  1. Anscombe’s 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 dplyr package!)
library(resample)
## Registered S3 method overwritten by 'resample':
##   method         from  
##   print.resample modelr
data %>%
  summarise(variable = colnames (.),
            mean = colMeans(.),
            variance = colVars(.))
##   variable     mean  variance
## 1       x1 9.000000 11.000000
## 2       x2 9.000000 11.000000
## 3       x3 9.000000 11.000000
## 4       x4 9.000000 11.000000
## 5       y1 7.500909  4.127269
## 6       y2 7.500909  4.127629
## 7       y3 7.500000  4.122620
## 8       y4 7.500909  4.123249
data %>%
  summarise(correlation_x1_y1 = cor(x1, y1),
            correlation_x2_y2 = cor(x2, y2),
            correlation_x3_y3 = cor(x3, y3),
            correlation_x4_y4 = cor(x4, y4))
##   correlation_x1_y1 correlation_x2_y2 correlation_x3_y3 correlation_x4_y4
## 1         0.8164205         0.8162365         0.8162867         0.8165214
  1. Using ggplot, create scatter plots for each \(x, y\) pair of data (maybe use ‘facet_grid’ or ‘facet_wrap’).
library(ggplot2)
par(mfrow = c(2, 2))
plot(data$x1, data$y1, main = "Scatterplot for Pair x1 and y1", xlab = "x1", ylab = "y1")
plot(data$x2, data$y2, main = "Scatterplot for Pair x2 and y2", xlab = "x2", ylab = "y2")
plot(data$x3, data$y3, main = "Scatterplot for Pair x3 and y3", xlab = "x3", ylab = "y3")
plot(data$x4, data$y4, main = "Scatterplot for Pair x4 and y4", xlab = "x4", ylab = "y4")

  1. Now change the symbols on the scatter plots to solid blue circles.
par(mfrow = c(2, 2))
plot(data$x1, data$y1, main = "Scatterplot for Pair x1 and y1", xlab = "x1", ylab = "y1", pch = 19, col='Blue')
plot(data$x2, data$y2, main = "Scatterplot for Pair x2 and y2", xlab = "x2", ylab = "y2", pch = 19, col='Blue')
plot(data$x3, data$y3, main = "Scatterplot for Pair x3 and y3", xlab = "x3", ylab = "y3", pch = 19, col='Blue')
plot(data$x4, data$y4, main = "Scatterplot for Pair x4 and y4", xlab = "x4", ylab = "y4", pch = 19, col='Blue')

  1. Now fit a linear model to each data set using the lm() function.
lm_1 <- lm(formula = y1 ~ x1, data = data)
lm_2 <- lm(formula = y2 ~ x2, data = data)
lm_3 <- lm(formula = y3 ~ x3, data = data)
lm_4 <- lm(formula = y4 ~ x4, data = data)
  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!)
par(mfrow = c(2, 2))
plot(data$x1, data$y1, main = "Scatterplot for Pair x1 and y1", xlab = "x1", ylab = "y1", pch = 19, col='Blue')
abline(lm_1)
plot(data$x2, data$y2, main = "Scatterplot for Pair x2 and y2", xlab = "x2", ylab = "y2", pch = 19, col='Blue')
abline(lm_2)
plot(data$x3, data$y3, main = "Scatterplot for Pair x3 and y3", xlab = "x3", ylab = "y3", pch = 19, col='Blue')
abline(lm_3)
plot(data$x4, data$y4, main = "Scatterplot for Pair x4 and y4", xlab = "x4", ylab = "y4", pch = 19, col='Blue')
abline(lm_4)

  1. Now compare the model fits for each model object.
summary(lm_1)
## 
## 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
summary(lm_2)
## 
## 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
summary(lm_3)
## 
## 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
summary(lm_4)
## 
## 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
  1. In text, summarize the lesson of Anscombe’s Quartet and what it says about the value of data visualization.
"Anscombe’s Quartet is the modal example to demonstrate the importance of data visualization. It comprises of four data-set and each data-set consists of eleven (x,y) points. The basic thing to analyze about these data-sets is that they all share the same descriptive statistics(mean, variance, standard deviation etc) but different graphical representation. Each graph plot shows the different behavior irrespective of statistical analysis"
## [1] "Anscombe’s Quartet is the modal example to demonstrate the importance of data visualization. It comprises of four data-set and each data-set consists of eleven (x,y) points. The basic thing to analyze about these data-sets is that they all share the same descriptive statistics(mean, variance, standard deviation etc) but different graphical representation. Each graph plot shows the different behavior irrespective of statistical analysis"