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 upload your document to rpubs.com and share the link 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.library(datasets)
data <- anscombe
fBasics() package!)library(fBasics)
## Loading required package: timeDate
## Loading required package: timeSeries
##
## 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
means <- colMeans(data)
vars <- colVars(data)
cor1 <- cor(data$x1,data$y1)
cor2 <- cor(data$x2,data$y2)
cor3 <- cor(data$x3,data$y3)
cor4 <- cor(data$x4,data$y4)
plot(data$x1,data$y1, main="Scatterplot of x1,y1", xlab="x1", ylab="y1")
plot(data$x2,data$y2, main="Scatterplot of x2,y2", xlab="x2", ylab="y2")
plot(data$x3,data$y3, main="Scatterplot of x3,y3", xlab="x3", ylab="y3")
plot(data$x4,data$y4, main="Scatterplot of x4,y4", xlab="x4", ylab="y4")
par(mfrow=c(2,2))
plot(data$x1,data$y1, main="Scatterplot of x1,y1", xlab="x1", ylab="y1", pch=19)
plot(data$x2,data$y2, main="Scatterplot of x2,y2", xlab="x2", ylab="y2", pch=19)
plot(data$x3,data$y3, main="Scatterplot of x3,y3", xlab="x3", ylab="y3", pch=19)
plot(data$x4,data$y4, main="Scatterplot of x4,y4", xlab="x4", ylab="y4", pch=19)
lm() function.lm1 <- lm(data$y1~data$x1)
lm2 <- lm(data$y2~data$x2)
lm3 <- lm(data$y3~data$x3)
lm4 <- lm(data$y4~data$x4)
par(mfrow=c(2,2))
plot(data$x1,data$y1, main="Scatterplot of x1,y1", xlab="x1", ylab="y1", pch=19)
abline(lm1)
plot(data$x2,data$y2, main="Scatterplot of x2,y2", xlab="x2", ylab="y2", pch=19)
abline(lm2)
plot(data$x3,data$y3, main="Scatterplot of x3,y3", xlab="x3", ylab="y3", pch=19)
abline(lm3)
plot(data$x4,data$y4, main="Scatterplot of x4,y4", xlab="x4", ylab="y4", pch=19)
abline(lm4)
anova(lm1)
Analysis of Variance Table
Response: data\(y1 Df Sum Sq Mean Sq F value Pr(>F) data\)x1 1 27.510 27.5100 17.99 0.00217 ** Residuals 9 13.763 1.5292
— Signif. codes: 0 ‘’ 0.001 ’’ 0.01 ’’ 0.05 ‘.’ 0.1 ‘’ 1
anova(lm2)
Analysis of Variance Table
Response: data\(y2 Df Sum Sq Mean Sq F value Pr(>F) data\)x2 1 27.500 27.5000 17.966 0.002179 ** Residuals 9 13.776 1.5307
— Signif. codes: 0 ‘’ 0.001 ’’ 0.01 ’’ 0.05 ‘.’ 0.1 ‘’ 1
anova(lm3)
Analysis of Variance Table
Response: data\(y3 Df Sum Sq Mean Sq F value Pr(>F) data\)x3 1 27.470 27.4700 17.972 0.002176 ** Residuals 9 13.756 1.5285
— Signif. codes: 0 ‘’ 0.001 ’’ 0.01 ’’ 0.05 ‘.’ 0.1 ‘’ 1
anova(lm4)
Analysis of Variance Table
Response: data\(y4 Df Sum Sq Mean Sq F value Pr(>F) data\)x4 1 27.490 27.4900 18.003 0.002165 ** Residuals 9 13.742 1.5269
— Signif. codes: 0 ‘’ 0.001 ’’ 0.01 ’’ 0.05 ‘.’ 0.1 ‘’ 1
There are four pairs of data of Anscombe’s Quartet. From the data visualization results, we can see they are quite different. In addition, linear model doesn’t fit all data pairs. Only x3,y3 fit the linear model well. The data visualization help us understand the dataset clear and directly.