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
enter your code or text response in the code chunk that
completes/answers the activity or question requested. To submit this
homework you will create the document in Rstudio, using the knitr
package (button included in Rstudio) and then submit the document to
your Rpubs account. Once uploaded you
will submit the link to that document on Canvas. Please make sure that
this link is hyper linked and that I can see the visualization and the
code required to create it. Each question is worth 5 points.
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
attach(data)
dplyr package!)library(dplyr)
sapply(data, var)
## x1 x2 x3 x4 y1 y2 y3 y4
## 11.000000 11.000000 11.000000 11.000000 4.127269 4.127629 4.122620 4.123249
sapply(data, mean)
## x1 x2 x3 x4 y1 y2 y3 y4
## 9.000000 9.000000 9.000000 9.000000 7.500909 7.500909 7.500000 7.500909
cor(data[,1:4],data[,5:8])
## y1 y2 y3 y4
## x1 0.8164205 0.8162365 0.8162867 -0.3140467
## x2 0.8164205 0.8162365 0.8162867 -0.3140467
## x3 0.8164205 0.8162365 0.8162867 -0.3140467
## x4 -0.5290927 -0.7184365 -0.3446610 0.8165214
library(ggplot2)
par(mfrow=c(2,2))
plot(x1,y1,main="x1 against y1", xlab = "x1", ylab = "y1")
plot(x1,y1,main="x1 against y1", xlab = "x1", ylab = "y1")
plot(x2,y2,main="x2 against y2", xlab = "x2", ylab = "y2")
plot(x3,y3,main="x3 against y3", xlab = "x3", ylab = "y3")
plot(x4,y4,main="x4 against y4", xlab = "x4", ylab = "y4")
par(mfrow=c(2,2))
plot(x1,y1,main="x1 against y1", xlab = "x1", ylab = "y1", col = "blue")
plot(x2,y2,main="x2 against y2", xlab = "x2", ylab = "y2", col = "blue")
plot(x3,y3,main="x3 against y3", xlab = "x3", ylab = "y3", col = "blue")
plot(x4,y4,main="x4 against y4", xlab = "x4", ylab = "y4", col = "blue")
lm()
function.model1<-lm(y1~x1)
model2<-lm(y2~x2)
model3<-lm(y3~x3)
model4<-lm(y4~x4)
par(mfrow=c(2,2))
plot(x1,y1,main="x1 against y1", xlab = "x1", ylab = "y1", col = "blue", abline(model1,col="red"))
plot(x2,y2,main="x2 against y2", xlab = "x2", ylab = "y2", col = "blue", abline(model2,col="red"))
plot(x3,y3,main="x3 against y3", xlab = "x3", ylab = "y3", col = "blue", abline(model3,col="red"))
plot(x4,y4,main="x4 against y4", xlab = "x4", ylab = "y4", col = "blue", abline(model4,col="red"))
summary(model1)
Call: lm(formula = y1 ~ 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 * 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(model2)
Call: lm(formula = y2 ~ 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 * 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(model3)
Call: lm(formula = y3 ~ 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 * 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(model4)
Call: lm(formula = y4 ~ 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 * 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
#Data visualization is a very important aspect of statistical analysis. Often, knowing only a correlation coefficient is not enough to understand the relationship between the variables fully. In this lesson, we found that the correlation coefficients between each pair of xi and yi (i = 1,2,3,4) are equal. However, the visualization showed us how much the situation is varied. In the first plot, there was a significantly dispersed data, but the points were quite balanced above and below the line, and therefore the correlation was strong. In the second example, we clearly see a curvilinear association, and the linear approximation is not an appropriate option for this relationship. Without visualization, we could not know about that. The third plot shows how one outlier may ruin the whole picture of the relationship. Without that point, we could have found a functional relationship (with 100% R-squared). Finally, the last plot illustrates another influence of an outlier - when it creates an impression about an existent association. As we can see, the other points form a vertical line parallel to y-axis. This means that the variables are not correlated. However, an outlier affects the linear trend and makes a significant correlation and linear model.
#Summing up, we should always support our statistical inference procedures with appropriate plots. Otherwise, we may conclude a misleading information about the relationships in the data.