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.
anscombe data that is part of the library(datasets) in R. And assign that data to a new object called data.data("anscombe")
data = anscombe
fBasics() package!)library(fBasics)
## Warning: package 'fBasics' was built under R version 3.5.3
## Loading required package: timeDate
## Warning: package 'timeDate' was built under R version 3.5.3
## Loading required package: timeSeries
## Warning: package 'timeSeries' was built under R version 3.5.3
data.frame(Mean=colAvgs(data), Variance=colVars(data))
## Mean Variance
## x1 9.000000 11.000000
## x2 9.000000 11.000000
## x3 9.000000 11.000000
## x4 9.000000 11.000000
## y1 7.500909 4.127269
## y2 7.500909 4.127629
## y3 7.500000 4.122620
## y4 7.500909 4.123249
corr_data = cor(data)
corrs = diag(corr_data[1:4,5:8])
corrs
## [1] 0.8164205 0.8162365 0.8162867 0.8165214
plot(data$x1, data$y1, main = "Scatter plot - Pair1 (x1 & y1)", xlab = "x1", ylab = "y1")
plot(data$x2, data$y2, main = "Scatter plot - Pair1 (x2 & y2)", xlab = "x2", ylab = "y2")
plot(data$x3, data$y3, main = "Scatter plot - Pair1 (x3 & y3)", xlab = "x3", ylab = "y3")
plot(data$x4, data$y4, main = "Scatter plot - Pair1 (x4 & y4)", xlab = "x4", ylab = "y4")
par(mfrow = c(2,2))
plot(data$x1, data$y1, main = "Scatter plot - Pair1 (x1 & y1)", xlab = "x1", ylab = "y1", pch = 20)
plot(data$x2, data$y2, main = "Scatter plot - Pair1 (x2 & y2)", xlab = "x2", ylab = "y2", pch = 20)
plot(data$x3, data$y3, main = "Scatter plot - Pair1 (x3 & y3)", xlab = "x3", ylab = "y3", pch = 20)
plot(data$x4, data$y4, main = "Scatter plot - Pair1 (x4 & y4)", xlab = "x4", ylab = "y4", pch = 20)
lm() function.LM1 = lm(y1~x1,data)
summary(LM1)
##
## 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
LM2 = lm(y2~x2,data)
summary(LM2)
##
## 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
LM3 = lm(y3~x3,data)
summary(LM3)
##
## 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
LM4 = lm(y4~x4,data)
summary(LM4)
##
## 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
par(mfrow = c(2,2))
plot(data$x1, data$y1, main = "Scatter plot - Pair1 (x1 & y1)", xlab = "x1", ylab = "y1", pch = 20, col = "darkorange")
abline(LM1, col="blue")
plot(data$x2, data$y2, main = "Scatter plot - Pair1 (x2 & y2)", xlab = "x2", ylab = "y2", pch = 20, col = "darkorange")
abline(LM2, col="blue")
plot(data$x3, data$y3, main = "Scatter plot - Pair1 (x3 & y3)", xlab = "x3", ylab = "y3", pch = 20, col = "darkorange")
abline(LM3, col="blue")
plot(data$x4, data$y4, main = "Scatter plot - Pair1 (x4 & y4)", xlab = "x4", ylab = "y4", pch = 20, col = "darkorange")
abline(LM4, col="blue")
anova(LM1)
Analysis of Variance Table
Response: y1 Df Sum Sq Mean Sq F value Pr(>F)
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: y2 Df Sum Sq Mean Sq F value Pr(>F)
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: y3 Df Sum Sq Mean Sq F value Pr(>F)
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: y4 Df Sum Sq Mean Sq F value Pr(>F)
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
The Anscombe’s Quartet dataset has four pair of data, all of which have the same mean, variance and correlation. If one were to look at only the summary statistics, one would conclude that all four pair of data are exactly same. Once we plot the graph we realize that all the four pairs are different. Pair 1 has weak linear relationship; Pair 2 have a non linear relationship (quadratic or cubic or any other polynomial); Pair 3 has a much stronger linear relationship than Pair 1 except for one outlier and Pair 4 has constant values of x with one outlier. If not for visualizing the data, our conclusion about the data set might have been wrong. Hence, this example conveys that data visualization is an important aspect of any statistical analysis and give much better insights about the dataset