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.
data(anscombe)

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!)
sapply(data,mean)
##       x1       x2       x3       x4       y1       y2       y3       y4 
## 9.000000 9.000000 9.000000 9.000000 7.500909 7.500909 7.500000 7.500909
sapply(data,var)
##        x1        x2        x3        x4        y1        y2        y3        y4 
## 11.000000 11.000000 11.000000 11.000000  4.127269  4.127629  4.122620  4.123249
cor(data$x1, data$y1, method = c("pearson"))
## [1] 0.8164205
cor(data$x2, data$y2, method = c("pearson"))
## [1] 0.8162365
cor(data$x3, data$y3, method = c("pearson"))
## [1] 0.8162867
cor(data$x4, data$y4, method = c("pearson"))
## [1] 0.8165214
  1. Using ggplot, create scatter plots for each \(x, y\) pair of data (maybe use ‘facet_grid’ or ‘facet_wrap’).
library(ggplot2)

ggplot(data, aes(x=x1, y=y1)) + 
  geom_point()

ggplot(data, aes(x=x2, y=y2)) + 
  geom_point()

ggplot(data, aes(x=x3, y=y3)) + 
  geom_point()

ggplot(data, aes(x=x4, y=y4)) + 
  geom_point()

  1. Now change the symbols on the scatter plots to solid blue circles.
ggplot(data, aes(x=x1, y=y1)) + 
  geom_point(pch=19, col="blue")

ggplot(data, aes(x=x2, y=y2)) + 
  geom_point(pch=19, col="blue")

ggplot(data, aes(x=x3, y=y3)) + 
  geom_point(pch=19, col="blue")

ggplot(data, aes(x=x4, y=y4)) + 
  geom_point(pch=19, col="blue")

  1. Now fit a linear model to each data set using the lm() function.
ggplot(data, aes(x=x1, y=y1)) + 
  geom_smooth(method = "lm", se=FALSE)
## `geom_smooth()` using formula = 'y ~ x'

ggplot(data, aes(x=x2, y=y2)) + 
  geom_smooth(method = "lm", se=FALSE)
## `geom_smooth()` using formula = 'y ~ x'

ggplot(data, aes(x=x3, y=y3)) + 
  geom_smooth(method = "lm", se=FALSE)
## `geom_smooth()` using formula = 'y ~ x'

ggplot(data, aes(x=x4, y=y4)) + 
  geom_smooth(method = "lm", se=FALSE)
## `geom_smooth()` using formula = 'y ~ x'

  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!)
ggplot(data, aes(x=x1, y=y1)) + 
  geom_smooth(method = "lm", se=FALSE)+
  geom_point(pch=19, col="blue")
## `geom_smooth()` using formula = 'y ~ x'

ggplot(data, aes(x=x2, y=y2)) + 
  geom_smooth(method = "lm", se=FALSE)+
  geom_point(pch=19, col="blue")
## `geom_smooth()` using formula = 'y ~ x'

ggplot(data, aes(x=x3, y=y3)) + 
  geom_smooth(method = "lm", se=FALSE)+
  geom_point(pch=19, col="blue")
## `geom_smooth()` using formula = 'y ~ x'

ggplot(data, aes(x=x4, y=y4)) + 
  geom_smooth(method = "lm", se=FALSE)+
  geom_point(pch=19, col="blue")
## `geom_smooth()` using formula = 'y ~ x'

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

Call: lm(formula = data\(x1 ~ data\)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
data$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(lm(data$x2~data$y2))

Call: lm(formula = data\(x2 ~ data\)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
data$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(lm(data$x3~data$y3))

Call: lm(formula = data\(x3 ~ data\)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
data$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(lm(data$x4~data$y4))

Call: lm(formula = data\(x4 ~ data\)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
data$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

#These 4 graphs have very similar statistics, the only way to recognize the model fit is through these graphs, based on my observation, 4th plot has the worse model fit. 
  1. In text, summarize the lesson of Anscombe’s Quartet and what it says about the value of data visualization.
# By adding commend step by step, we are able to plot these scatter plot and know the meaning behind each function. In addition, plotting graph is really important to make a plot because plot provide additional information