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.
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!)
library(fBasics)
## Loading required package: timeDate
## Loading required package: timeSeries
colAvgs(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
colVars(data)
##        x1        x2        x3        x4        y1        y2        y3 
## 11.000000 11.000000 11.000000 11.000000  4.127269  4.127629  4.122620 
##        y4 
##  4.123249
paste0("Correlation of X1-Y1 is ",cor(data[,1],data[,5]))
## [1] "Correlation of X1-Y1 is 0.81642051634484"
paste0("Correlation of X2-Y2 is ",cor(data[,2],data[,6]))
## [1] "Correlation of X2-Y2 is 0.816236506000243"
paste0("Correlation of X3-Y3 is ",cor(data[,3],data[,7]))
## [1] "Correlation of X3-Y3 is 0.816286739489598"
paste0("Correlation of X4-Y4 is ",cor(data[,4],data[,8]))
## [1] "Correlation of X4-Y4 is 0.816521436888503"
  1. Create scatter plots for each \(x, y\) pair of data.
plot(data[,1],data[,5], xlab="X1",ylab="Y1",main="Scatterplot between x1-y1") 

plot(data[,2],data[,6], xlab="X2",ylab="Y2",main="Scatterplot between x2-y2") 

plot(data[,3],data[,7], xlab="X3",ylab="Y3",main="Scatterplot between x3-y3") 

plot(data[,4],data[,8], xlab="X4",ylab="Y4",main="Scatterplot between 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(data[,1],data[,5], main="Scatterplot between x1-y1",xlab="X1",ylab="Y1",pch=19) 
plot(data[,2],data[,6], main="Scatterplot between x2-y2",xlab="X2",ylab="Y2",pch=19) 
plot(data[,3],data[,7], main="Scatterplot between x3-y3",xlab="X3",ylab="Y3",pch=19) 
plot(data[,4],data[,8], main="Scatterplot between x4-y4",xlab="X4",ylab="Y4",pch=19) 

  1. Now fit a linear model to each data set using the lm() function.
model1<-lm(data[,1]~data[,5])
model2<-lm(data[,2]~data[,6])
model3<-lm(data[,3]~data[,7])
model4<-lm(data[,4]~data[,8])
  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[,1],data[,5], main="Scatterplot between x1-y1",xlab="X1",ylab="Y1",pch=19,abline(model1)) 
plot(data[,2],data[,6], main="Scatterplot between x2-y2",xlab="X2",ylab="Y2",pch=19,abline(model2)) 
plot(data[,3],data[,7], main="Scatterplot between x3-y3",xlab="X3",ylab="Y3",pch=19,abline(model3)) 
plot(data[,4],data[,8], main="Scatterplot between x4-y4",xlab="X4",ylab="Y4",pch=19,abline(model4)) 

  1. Now compare the model fits for each model object.
summary(model1)

Call: lm(formula = data[, 1] ~ data[, 5])

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[, 5] 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(model2)

Call: lm(formula = data[, 2] ~ data[, 6])

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[, 6] 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(model3)

Call: lm(formula = data[, 3] ~ data[, 7])

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[, 7] 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(model4)

Call: lm(formula = data[, 4] ~ data[, 8])

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[, 8] 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

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

From the summary of Models, their Coefficients, R Squared, REsidual Standard errors, and p-values are very close (almost the same). However, when we look at the plots, we can see that they totally different models. Data visualization can show some summarized information that is easily ignored by the summary. Also, we also need summary which can show us detailed metrics.