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)
data <- anscombe
data
  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!)
library(fBasics)
## Warning: package 'fBasics' was built under R version 3.6.3
## Loading required package: timeDate
## Loading required package: timeSeries
## Warning: package 'timeSeries' was built under R version 3.6.3
x1<-data[,1];x2<-data[,2];x3<-data[,3];x4<-data[,4]
y1<-data[,5];y2<-data[,6];y3<-data[,7];y4<-data[,8]

mean(x1);var(x1)
## [1] 9
## [1] 11
mean(x2);var(x2)
## [1] 9
## [1] 11
mean(x3);var(x3)
## [1] 9
## [1] 11
mean(x4);var(x4)
## [1] 9
## [1] 11
mean(y1);var(y1)
## [1] 7.500909
## [1] 4.127269
mean(y2);var(y2)
## [1] 7.500909
## [1] 4.127629
mean(y3);var(y3)
## [1] 7.5
## [1] 4.12262
mean(y4);var(y4)
## [1] 7.500909
## [1] 4.123249
correlationTest(x1,y1);correlationTest(x2,y2);correlationTest(x3,y3);correlationTest(x4,y4)
## 
## Title:
##  Pearson's Correlation Test
## 
## Test Results:
##   PARAMETER:
##     Degrees of Freedom: 9
##   SAMPLE ESTIMATES:
##     Correlation: 0.8164
##   STATISTIC:
##     t: 4.2415
##   P VALUE:
##     Alternative Two-Sided: 0.00217 
##     Alternative      Less: 0.9989 
##     Alternative   Greater: 0.001085 
##   CONFIDENCE INTERVAL:
##     Two-Sided: 0.4244, 0.9507
##          Less: -1, 0.9388
##       Greater: 0.5113, 1
## 
## Description:
##  Wed May 20 14:15:26 2020
## 
## Title:
##  Pearson's Correlation Test
## 
## Test Results:
##   PARAMETER:
##     Degrees of Freedom: 9
##   SAMPLE ESTIMATES:
##     Correlation: 0.8162
##   STATISTIC:
##     t: 4.2386
##   P VALUE:
##     Alternative Two-Sided: 0.002179 
##     Alternative      Less: 0.9989 
##     Alternative   Greater: 0.001089 
##   CONFIDENCE INTERVAL:
##     Two-Sided: 0.4239, 0.9506
##          Less: -1, 0.9387
##       Greater: 0.5109, 1
## 
## Description:
##  Wed May 20 14:15:26 2020
## 
## Title:
##  Pearson's Correlation Test
## 
## Test Results:
##   PARAMETER:
##     Degrees of Freedom: 9
##   SAMPLE ESTIMATES:
##     Correlation: 0.8163
##   STATISTIC:
##     t: 4.2394
##   P VALUE:
##     Alternative Two-Sided: 0.002176 
##     Alternative      Less: 0.9989 
##     Alternative   Greater: 0.001088 
##   CONFIDENCE INTERVAL:
##     Two-Sided: 0.4241, 0.9507
##          Less: -1, 0.9387
##       Greater: 0.511, 1
## 
## Description:
##  Wed May 20 14:15:26 2020
## 
## Title:
##  Pearson's Correlation Test
## 
## Test Results:
##   PARAMETER:
##     Degrees of Freedom: 9
##   SAMPLE ESTIMATES:
##     Correlation: 0.8165
##   STATISTIC:
##     t: 4.243
##   P VALUE:
##     Alternative Two-Sided: 0.002165 
##     Alternative      Less: 0.9989 
##     Alternative   Greater: 0.001082 
##   CONFIDENCE INTERVAL:
##     Two-Sided: 0.4246, 0.9507
##          Less: -1, 0.9388
##       Greater: 0.5115, 1
## 
## Description:
##  Wed May 20 14:15:26 2020
  1. Create scatter plots for each \(x, y\) pair of data.
pairs(data)

plot(x1,y1, main="Scatter plot x1,y1") 

plot(x2,y2, main="Scatter plot x2,y2") 

plot(x3,y3, main="Scatter plot x3,y3") 

plot(x4,y4, main="Scatter plot x4,y4") 

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

  1. Now fit a linear model to each data set using the lm() function.
model1<-lm(y1~x1)
model2<-lm(y2~x2)
model3<-lm(y3~x3)
model4<-lm(y4~x4)
  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(x1,y1, main="Scatter plot for x1,y1",pch=19) 
abline(model1, col="blue")
plot(x2,y2, main="Scatter plot for x2,y2",pch=19) 
abline(model2, col="red")
plot(x3,y3, main="Scatter plot for x3,y3",pch=19) 
abline(model3, col="green")
plot(x4,y4, main="Scatter plot for x4,y4",pch=19)
abline(model4)

  1. Now compare the model fits for each model object.
anova(model1);anova(model2);anova(model3);anova(model4)
  1. In text, summarize the lesson of Anscombe’s Quartet and what it says about the value of data visualization.
## It tells us that both statistical calculation and graph shoud be considered when we are trying to do analysis, and they all have their pro and con. Some assumption of statistical calculation could be right and that's why we need data visualization to help us to have a better understanding.