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 post your assignment on Rpubs and upload a link to it to the “Problem Set 2” assignmenet on Moodle.
## Loading required package: timeDate
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
fBasics()
package!)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
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
library(fBasics)
##
## Rmetrics Package fBasics
## Analysing Markets and calculating Basic Statistics
## Copyright (C) 2005-2014 Rmetrics Association Zurich
## Educational Software for Financial Engineering and Computational Science
## Rmetrics is free software and comes with ABSOLUTELY NO WARRANTY.
## https://www.rmetrics.org --- Mail to: info@rmetrics.org
correlationTest(data$x1, data$y1)
##
## 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:
## Tue Jul 25 18:37:36 2017
correlationTest(data$x2, data$y2)
##
## 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:
## Tue Jul 25 18:37:36 2017
correlationTest(data$x3, data$y3)
##
## 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:
## Tue Jul 25 18:37:37 2017
correlationTest(data$x4, data$y4)
##
## 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:
## Tue Jul 25 18:37:37 2017
plot(data$x1, data$y1, main = "scatter plot: y1 vs x1")
plot(data$x2, data$y2, main = "scatter plot: y2 vs x2")
plot(data$x3, data$y3, main = "scatter plot: y3 vs x3")
plot(data$x4, data$y4, main = "scatter plot: y4 vs x4")
par(mfrow=c(2,2))
plot(data$x1, data$y1, pch = 20, main = "scatter plot: y1 vs x1")
plot(data$x2, data$y2, pch = 20, main = "scatter plot: y2 vs x2")
plot(data$x3, data$y3, pch = 20, main = "scatter plot: y3 vs x3")
plot(data$x4, data$y4, pch = 20, main = "scatter plot: y4 vs x4")
lm()
function.fit1 <- lm(data$y1 ~ data$x1)
fit1
##
## Call:
## lm(formula = data$y1 ~ data$x1)
##
## Coefficients:
## (Intercept) data$x1
## 3.0001 0.5001
summary(fit1)
##
## Call:
## lm(formula = data$y1 ~ data$x1)
##
## 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 *
## data$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
fit2 <- lm(data$y2 ~ data$x2)
fit2
##
## Call:
## lm(formula = data$y2 ~ data$x2)
##
## Coefficients:
## (Intercept) data$x2
## 3.001 0.500
summary(fit2)
##
## Call:
## lm(formula = data$y2 ~ data$x2)
##
## 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 *
## data$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
fit3 <- lm(data$y3 ~ data$x3)
fit3
##
## Call:
## lm(formula = data$y3 ~ data$x3)
##
## Coefficients:
## (Intercept) data$x3
## 3.0025 0.4997
summary(fit3)
##
## Call:
## lm(formula = data$y3 ~ data$x3)
##
## 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 *
## data$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
fit4 <- lm(data$y4 ~ data$x4)
fit4
##
## Call:
## lm(formula = data$y4 ~ data$x4)
##
## Coefficients:
## (Intercept) data$x4
## 3.0017 0.4999
summary(fit4)
##
## Call:
## lm(formula = data$y4 ~ data$x4)
##
## 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 *
## data$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
par(mfrow=c(2,2))
plot(data$x1, data$y1, pch = 20, main = "scatter plot: y1 vs x1")
abline(fit1)
plot(data$x2, data$y2, pch = 20, main = "scatter plot: y2 vs x2")
abline(fit2)
plot(data$x3, data$y3, pch = 20, main = "scatter plot: y3 vs x3")
abline(fit3)
plot(data$x4, data$y4, pch = 20, main = "scatter plot: y4 vs x4")
abline(fit4)
# There are numerous ways to compare two linear models, one of which is to compare each model's adjusted R-squared value which suggests as to how much variability a respective model explains.
adj.Rsq <- c(summary(fit1)$adj.r.squared, summary(fit2)$adj.r.squared, summary(fit3)$adj.r.squared, summary(fit4)$adj.r.squared)
adj.Rsq
[1] 0.6294916 0.6291578 0.6292489 0.6296747
# Here, adjusted R-squared value of each models are equivalent and therefore, it can be said that each model explains equal amount of variability in the respective model i.e. around 63% variability.
Anscombe’s Quartet has 8 variables and 11 observations. The first three variables are identical. The forth variable has the same observation for the most part while the rest of the variables seems to be scattered.
As such, by looking at these variables, one would be unable to identify any sort of pattern from this dataset. However, when we visualize the dataset using different visualization tools in R, we came to know how scattered and different from each other these variables look despite each model (formed by pairing variables) explaining almost equal amount of variability in their respective models.
Without the power of visulization, one would have simply assumed that the data are similarly distributed in a plane by just looking at or calculating residuals of models since each model explains equal amount of variability in the model.