knitr::opts_chunk$set(echo = TRUE)
library(ggplot2)
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.4.4
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(datasets)
library(fBasics)
## Loading required package: timeDate
## Loading required package: timeSeries
library(ggpubr)
## Warning: package 'ggpubr' was built under R version 3.4.4
## Loading required package: magrittr
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” assignmenet on Moodle.
anscombe
data that is part of the library(datasets)
in R
. And assign that data to a new object called data
.data = datasets::anscombe
fBasics()
package!)# load required library
require("fBasics")
# mean of each column
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
#variance of each column
colSds(data)
## x1 x2 x3 x4 y1 y2 y3 y4
## 3.316625 3.316625 3.316625 3.316625 2.031568 2.031657 2.030424 2.030579
#correlation
cor(data$x1,data$y1)
## [1] 0.8164205
cor(data$x2,data$y2)
## [1] 0.8162365
cor(data$x3,data$y3)
## [1] 0.8162867
fBasics::correlationTest(data$x1,data$y1,method = "spearman")
##
## Title:
## Spearman's rho Correlation Test
##
## Test Results:
## SAMPLE ESTIMATES:
## rho: 0.8182
## STATISTIC:
## S: 40
## P VALUE:
## Alternative Two-Sided: 0.003734
## Alternative Less: 0.9984
## Alternative Greater: 0.001867
##
## Description:
## Tue Oct 2 18:30:33 2018
fBasics::correlationTest(data$x2,data$y2,method = "spearman")
##
## Title:
## Spearman's rho Correlation Test
##
## Test Results:
## SAMPLE ESTIMATES:
## rho: 0.6909
## STATISTIC:
## S: 68
## P VALUE:
## Alternative Two-Sided: 0.02306
## Alternative Less: 0.9896
## Alternative Greater: 0.01153
##
## Description:
## Tue Oct 2 18:30:33 2018
fBasics::correlationTest(data$x3,data$y3,method = "spearman")
##
## Title:
## Spearman's rho Correlation Test
##
## Test Results:
## SAMPLE ESTIMATES:
## rho: 0.9909
## STATISTIC:
## S: 2
## P VALUE:
## Alternative Two-Sided: < 2.2e-16
## Alternative Less: 1
## Alternative Greater: < 2.2e-16
##
## Description:
## Tue Oct 2 18:30:34 2018
# for x1, y1
ggplot(data = data, aes(x1, y1)) +
geom_point(colour = '#7C9EB2') +
labs(title = "Scatterplot of x1, y1") +
theme(plot.title = element_text(size=22,face = "bold",colour = "black", hjust = 0.5))
# for x2, y2
ggplot(data = data, aes(x2, y2)) +
geom_point(colour = '#52528C') +
labs(title = "Scatterplot of x2, y2") +
theme(plot.title = element_text(size=22,face = "bold",colour = "black", hjust = 0.5))
# for x3, y3
ggplot(data = data, aes(x3, y3)) +
geom_point(colour = 'royal blue') +
labs(title = "Scatterplot of x3, y3") +
theme(plot.title = element_text(size=22,face = "bold",colour = "black", hjust = 0.5))
# for x4, y4
ggplot(data = data, aes(x4, y4)) +
geom_point(colour = '#14BDEB') +
labs(title = "Scatterplot of x4, y4") +
theme(plot.title = element_text(size=22,face = "bold",colour = "black", hjust = 0.5))
# solid circles has shape number of 19
# for x1, y1
sc1 = ggplot(data = data, aes(x1, y1)) +
geom_point(colour = '#7C9EB2', shape = 19) +
labs(title = "Scatterplot of x1, y1") +
theme(plot.title = element_text(size=22,face = "bold",colour = "black", hjust = 0.5))
# for x2, y2
sc2 = ggplot(data = data, aes(x2, y2)) +
geom_point(colour = '#52528C', shape = 19) +
labs(title = "Scatterplot of x2, y2") +
theme(plot.title = element_text(size=22,face = "bold",colour = "black", hjust = 0.5))
# for x3, y3
sc3 = ggplot(data = data, aes(x3, y3)) +
geom_point(colour = 'royal blue', shape = 19) +
labs(title = "Scatterplot of x3, y3") +
theme(plot.title = element_text(size=22,face = "bold",colour = "black", hjust = 0.5))
# for x4, y4
sc4 = ggplot(data = data, aes(x4, y4)) +
geom_point(colour = '#14BDEB', shape = 19) +
labs(title = "Scatterplot of x4, y4") +
theme(plot.title = element_text(size=22,face = "bold",colour = "black", hjust = 0.5))
# create panel of 4
ggarrange(sc1, sc2, sc3, sc4, nrow = 2, ncol = 2)
lm()
function.# linear model for y1 ~ x1
fit1 = lm(y1 ~ x1, data = data)
# linear model for y2 ~ x2
fit2 = lm(y2 ~ x2, data = data)
# linear model for y3 ~ x3
fit3 = lm(y3 ~ x3, data = data)
# linear model for y4 ~ x4
fit4 = lm(y4 ~ x4, data = data)
# create scatterplot with regression lines
# for x1, y1
scr1 = ggplot(data = data, aes(x1, y1)) +
geom_point(colour = '#7C9EB2', shape = 19) +
geom_smooth(method=lm) +
labs(title = "Scatterplot of x1, y1") +
theme(plot.title = element_text(size=22,face = "bold",colour = "black", hjust = 0.5))
# for x2, y2
scr2 = ggplot(data = data, aes(x2, y2)) +
geom_point(colour = '#52528C', shape = 19) +
geom_smooth(method=lm) +
labs(title = "Scatterplot of x2, y2") +
theme(plot.title = element_text(size=22,face = "bold",colour = "black", hjust = 0.5))
# for x3, y3
scr3 = ggplot(data = data, aes(x3, y3)) +
geom_point(colour = 'royal blue', shape = 19) +
geom_smooth(method=lm) +
labs(title = "Scatterplot of x3, y3") +
theme(plot.title = element_text(size=22,face = "bold",colour = "black", hjust = 0.5))
# for x4, y4
scr4 = ggplot(data = data, aes(x4, y4)) +
geom_point(colour = '#14BDEB', shape = 19) +
geom_smooth(method=lm) +
labs(title = "Scatterplot of x4, y4") +
theme(plot.title = element_text(size=22,face = "bold",colour = "black", hjust = 0.5))
# create panel of 4
ggarrange(scr1, scr2, scr3, scr4, nrow = 2, ncol = 2)
summary(fit1)
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(fit2)
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(fit3)
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(fit4)
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
Anscombe’s Quartet is a great example of the limitation of statistical summary. Statistical summary such as mean, median, standard deviation gives us very high level information and overall picture of a distribution. And often times, we believe together with the additional information of correlation, linear regression, we would have a good view of the data. However, we might neglect the non-linear pattern in the data pairs.
In Anscombe’s Quartet, it gives us 4 pairs of data with all the same/almost same statistical summary and linear model formula and result. They seems to be pretty similar in this case. However, when we check the scatterplot, we can directly see the difference, and identify the different patterns between different pairs. The value of data visualization, as it shows, can help people easily, directly detect patterns and understand better of the relationships.