Objectives

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.

Questions

  1. Anscombe’s quartet is a set of 4 \(x,y\) data sets that were published by Francis Anscombe in a 1973 paper Graphs in statistical analysis. For this first question load the anscombe data that is part of the library(datasets) in R. And assign that data to a new object called data.
## Loading the anscombe data from the library(datasets):

anscombe
##    x1 x2 x3 x4    y1   y2    y3    y4
## 1  10 10 10  8  8.04 9.14  7.46  6.58
## 2   8  8  8  8  6.95 8.14  6.77  5.76
## 3  13 13 13  8  7.58 8.74 12.74  7.71
## 4   9  9  9  8  8.81 8.77  7.11  8.84
## 5  11 11 11  8  8.33 9.26  7.81  8.47
## 6  14 14 14  8  9.96 8.10  8.84  7.04
## 7   6  6  6  8  7.24 6.13  6.08  5.25
## 8   4  4  4 19  4.26 3.10  5.39 12.50
## 9  12 12 12  8 10.84 9.13  8.15  5.56
## 10  7  7  7  8  4.82 7.26  6.42  7.91
## 11  5  5  5  8  5.68 4.74  5.73  6.89
str(anscombe)
## 'data.frame':    11 obs. of  8 variables:
##  $ x1: num  10 8 13 9 11 14 6 4 12 7 ...
##  $ x2: num  10 8 13 9 11 14 6 4 12 7 ...
##  $ x3: num  10 8 13 9 11 14 6 4 12 7 ...
##  $ x4: num  8 8 8 8 8 8 8 19 8 8 ...
##  $ y1: num  8.04 6.95 7.58 8.81 8.33 ...
##  $ y2: num  9.14 8.14 8.74 8.77 9.26 8.1 6.13 3.1 9.13 7.26 ...
##  $ y3: num  7.46 6.77 12.74 7.11 7.81 ...
##  $ y4: num  6.58 5.76 7.71 8.84 8.47 7.04 5.25 12.5 5.56 7.91 ...
## Assigning "anscombe" data to a new object 'data':

data <- anscombe

str(data)
## 'data.frame':    11 obs. of  8 variables:
##  $ x1: num  10 8 13 9 11 14 6 4 12 7 ...
##  $ x2: num  10 8 13 9 11 14 6 4 12 7 ...
##  $ x3: num  10 8 13 9 11 14 6 4 12 7 ...
##  $ x4: num  8 8 8 8 8 8 8 19 8 8 ...
##  $ y1: num  8.04 6.95 7.58 8.81 8.33 ...
##  $ y2: num  9.14 8.14 8.74 8.77 9.26 8.1 6.13 3.1 9.13 7.26 ...
##  $ y3: num  7.46 6.77 12.74 7.11 7.81 ...
##  $ y4: num  6.58 5.76 7.71 8.84 8.47 7.04 5.25 12.5 5.56 7.91 ...
summary(data)
##        x1             x2             x3             x4           y1        
##  Min.   : 4.0   Min.   : 4.0   Min.   : 4.0   Min.   : 8   Min.   : 4.260  
##  1st Qu.: 6.5   1st Qu.: 6.5   1st Qu.: 6.5   1st Qu.: 8   1st Qu.: 6.315  
##  Median : 9.0   Median : 9.0   Median : 9.0   Median : 8   Median : 7.580  
##  Mean   : 9.0   Mean   : 9.0   Mean   : 9.0   Mean   : 9   Mean   : 7.501  
##  3rd Qu.:11.5   3rd Qu.:11.5   3rd Qu.:11.5   3rd Qu.: 8   3rd Qu.: 8.570  
##  Max.   :14.0   Max.   :14.0   Max.   :14.0   Max.   :19   Max.   :10.840  
##        y2              y3              y4        
##  Min.   :3.100   Min.   : 5.39   Min.   : 5.250  
##  1st Qu.:6.695   1st Qu.: 6.25   1st Qu.: 6.170  
##  Median :8.140   Median : 7.11   Median : 7.040  
##  Mean   :7.501   Mean   : 7.50   Mean   : 7.501  
##  3rd Qu.:8.950   3rd Qu.: 7.98   3rd Qu.: 8.190  
##  Max.   :9.260   Max.   :12.74   Max.   :12.500
  1. Summarise the data by calculating the mean, variance, for each column and the correlation between each pair (eg. x1 and y1, x2 and y2, etc) (Hint: use the dplyr package!)
## Central tendencies calculation for each of the column and correlation between each data set pairs:


## Variable 1: 'X1' calculations:


# Mean:

Mean_x1<- mean(data$x1)

Mean_x1
## [1] 9
# Variance:

Var_x1<- var(data$x1)

Var_x1
## [1] 11
## Variable 2: 'X2' calculations:


# Mean:

Mean_x2<- mean(data$x2)

Mean_x2
## [1] 9
# Variance:

Var_x2<- var(data$x2)

Var_x2
## [1] 11
## Variable 3: 'X3' calculations:


# Mean:

Mean_x3<- mean(data$x3)

Mean_x3
## [1] 9
# Variance:

Var_x3<- var(data$x3)

Var_x3
## [1] 11
## Variable 3: 'X4' calculations:


# Mean:

Mean_x4<- mean(data$x4)

Mean_x4
## [1] 9
# Variance:

Var_x4<- var(data$x4)

Var_x4
## [1] 11
## Variable 5: 'Y1' calculations:


# Mean:

Mean_y1<- mean(data$y1)

Mean_y1
## [1] 7.500909
# Variance:

Var_y1<- var(data$y1)

Var_y1
## [1] 4.127269
## Variable 6: 'Y2' calculations:


# Mean:

Mean_y2<- mean(data$y2)

Mean_y2
## [1] 7.500909
# Variance:

Var_y2<- var(data$y2)

Var_y2
## [1] 4.127629
## Variable 7: 'Y3' calculations:


# Mean:

Mean_y3<- mean(data$y3)

Mean_y3
## [1] 7.5
# Variance:

Var_y3<- var(data$y3)

Var_y3
## [1] 4.12262
## Variable 8: 'Y4' calculations:


# Mean:

Mean_y4<- mean(data$y4)

Mean_y4
## [1] 7.500909
# Determination of Correlation between "X" and "Y" pairs:


library(dplyr)

set.seed(5)


## Correlation between "X1", and "Y1" pair:

cor(data$x1, data$y1)
## [1] 0.8164205
## Correlation between "X2", and "Y2" pair:

cor(data$x2, data$y2)
## [1] 0.8162365
## Correlation between "X3", and "Y3" pair:

cor(data$x3, data$y3)
## [1] 0.8162867
## Correlation between "X4", and "Y4" pair:

cor(data$x4, data$y4)
## [1] 0.8165214
  1. Using ggplot, create scatter plots for each \(x, y\) pair of data (maybe use ‘facet_grid’ or ‘facet_wrap’).
## Extracting the dataset:

data <- anscombe


## Plot a: Scatter Plot for X1 and Y1 data pair:


# Ggplot Object layer:

X1_Y1_Scatter_Plot_a<-ggplot(data, 
                                   aes(data$x1, data$y1))

# Addition of subsequent layers to the grouped scatter object layer

X1_Y1_Plot <- X1_Y1_Scatter_Plot_a + geom_point() +
  labs(title = "Scatter Plot for X1 and Y1 Dataset Pair ", x = "X1 Value Scale ", y = "Y1 Value Scale") + coord_cartesian(ylim = c(0, 12), xlim = c(2 , 15)) + cleanup



## Plot b: Scatter Plot for X2 and Y2 data pair:


# Ggplot Object layer:

X2_Y2_Scatter_Plot_b<-ggplot(data, 
                                   aes(data$x2, data$y2))

# Addition of subsequent layers to the grouped scatter object layer

X2_Y2_Plot <- X2_Y2_Scatter_Plot_b + geom_point() +
  labs(title = "Scatter Plot for X2 and Y2 Dataset Pair ", x = "X2 Value Scale ", y = "Y2 Value Scale") + coord_cartesian(ylim = c(0, 12), xlim = c(2 , 15))  + cleanup




## Plot c: Scatter Plot for X3 and Y3 data pair:


# Ggplot Object layer:

X3_Y3_Scatter_Plot_c<-ggplot(data, 
                                   aes(data$x3, data$y3))

# Addition of subsequent layers to the grouped scatter object layer

X3_Y3_Plot <- X3_Y3_Scatter_Plot_c + geom_point() +
  labs(title = "Scatter Plot for X3 and Y3 Dataset Pair ", x = "X3 Value Scale ", y = "Y3 Value Scale") + coord_cartesian(ylim = c(0, 15), xlim = c(2 , 15)) + cleanup



## Plot d: Scatter Plot for X4 and Y4 data pair:


# Ggplot Object layer:

X4_Y4_Scatter_Plot_d<-ggplot(data, 
                                   aes(data$x4, data$y4))

# Addition of subsequent layers to the grouped scatter object layer

X4_Y4_Plot <- X4_Y4_Scatter_Plot_d + geom_point() +
  labs(title = "Scatter Plot for X4 and Y4 Dataset Pair ", x = "X4 Value Scale ", y = "Y4 Value Scale") + coord_cartesian(ylim = c(0, 12), xlim = c(2 , 15))  + cleanup



## GGplot determination of all the Data set "X" and "Y" pairs:

plot_grid(X1_Y1_Plot, X2_Y2_Plot, X3_Y3_Plot, X4_Y4_Plot)

  1. Now change the symbols on the scatter plots to solid blue circles.
# Plot-1:

X1_Y1_Scatter_Plot_a<-ggplot(data, 
                                   aes(data$x1, data$y1, colour = "blue"))

# Addition of subsequent layers to the grouped scatter object layer

X1_Y1_Plot <- X1_Y1_Scatter_Plot_a + geom_point() + scale_colour_manual(values = "Blue") +
  labs(title = "Scatter Plot for X1 and Y1 Dataset Pair ", x = "X1 Value Scale ", y = "Y1 Value Scale") + coord_cartesian(ylim = c(0, 12), xlim = c(2 , 15)) + cleanup



## Plot-2

X2_Y2_Scatter_Plot_b<-ggplot(data, 
                                   aes(data$x2, data$y2, colour = "blue"))

# Addition of subsequent layers to the grouped scatter object layer

X2_Y2_Plot <- X2_Y2_Scatter_Plot_b + geom_point() + scale_colour_manual(values = "Blue") +
  labs(title = "Scatter Plot for X2 and Y2 Dataset Pair ", x = "X2 Value Scale ", y = "Y2 Value Scale") + coord_cartesian(ylim = c(0, 12), xlim = c(2 , 15)) + cleanup


# Plot-3:

X3_Y3_Scatter_Plot_c<-ggplot(data, 
                                   aes(data$x3, data$y3, colour = "blue"))

# Addition of subsequent layers to the grouped scatter object layer

X3_Y3_Plot <- X3_Y3_Scatter_Plot_c + geom_point() + scale_colour_manual(values = "Blue") +
  labs(title = "Scatter Plot for X3 and Y3 Dataset Pair ", x = "X3 Value Scale ", y = "Y3 Value Scale") + coord_cartesian(ylim = c(0, 15), xlim = c(2 , 15)) + cleanup



# Plot-4:


X4_Y4_Scatter_Plot_d<-ggplot(data, 
                                   aes(data$x4, data$y4, colour = "blue"))

# Addition of subsequent layers to the grouped scatter object layer

X4_Y4_Plot <- X4_Y4_Scatter_Plot_d + geom_point() + scale_colour_manual(values = "Blue") +
  labs(title = "Scatter Plot for X4 and Y4 Dataset Pair ", x = "X4 Value Scale ", y = "Y4 Value Scale") + coord_cartesian(ylim = c(0, 12), xlim = c(2 , 15)) + cleanup



## GGplot determination of all the Data set "X" and "Y" pairs:

Plot_b <- plot_grid(X1_Y1_Plot, X2_Y2_Plot, X3_Y3_Plot, X4_Y4_Plot)

Plot_b

  1. Now fit a linear model to each data set using the lm() function.
## Determination for Linear Model Fit to each data set leveraging 'lm()' function:


# Model -1:

Linear_Model_1<- lm(data$y1 ~ data$x1, data = data)

summary(Linear_Model_1)
## 
## Call:
## lm(formula = data$y1 ~ data$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 * 
## 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
# Model -2:

Linear_Model_2<- lm(data$y2 ~ data$x2, data = data)

summary(Linear_Model_2)
## 
## Call:
## lm(formula = data$y2 ~ data$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 * 
## 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
# Model -3:

Linear_Model_3<- lm(data$y3 ~ data$x3, data = data)

summary(Linear_Model_3)
## 
## Call:
## lm(formula = data$y3 ~ data$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 * 
## 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
# Model -4:

Linear_Model_4<- lm(data$y4 ~ data$x4, data = data)

summary(Linear_Model_4)
## 
## Call:
## lm(formula = data$y4 ~ data$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 * 
## 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
  1. Now combine the last two tasks. Create a four panel scatter plot matrix that has both the data points and the regression lines. (hint: the model objects will carry over chunks!)
## Creation of a four panel scatter plot comprising of data points and regression lines via ggplot function:

# Plot-1:

X1_Y1_Scatter_Plot_a<-ggplot(data, 
                                   aes(data$x1, data$y1, colour = "blue"))

# Addition of subsequent layers to the grouped scatter object layer

X1_Y1_Plot <- X1_Y1_Scatter_Plot_a + geom_point() + scale_colour_manual(values = "Blue") +
  labs(title = "Scatter Plot for X1 and Y1 Dataset Pair ", x = "X1 Value Scale ", y = "Y1 Value Scale") + coord_cartesian(ylim = c(0, 12), xlim = c(2 , 15)) +  geom_smooth(method = "lm", se = FALSE, colour ='darkgrey') + cleanup



## Plot-2

X2_Y2_Scatter_Plot_b<-ggplot(data, 
                                   aes(data$x2, data$y2, colour = "blue"))

# Addition of subsequent layers to the grouped scatter object layer

X2_Y2_Plot <- X2_Y2_Scatter_Plot_b + geom_point() + scale_colour_manual(values = "Blue") +
  labs(title = "Scatter Plot for X2 and Y2 Dataset Pair ", x = "X2 Value Scale ", y = "Y2 Value Scale") + coord_cartesian(ylim = c(0, 12), xlim = c(2 , 15)) +  geom_smooth(method = "lm", se = FALSE, colour ='darkgrey') + cleanup


# Plot-3:

X3_Y3_Scatter_Plot_c<-ggplot(data, 
                                   aes(data$x3, data$y3, colour = "blue"))

# Addition of subsequent layers to the grouped scatter object layer

X3_Y3_Plot <- X3_Y3_Scatter_Plot_c + geom_point() + scale_colour_manual(values = "Blue") +
  labs(title = "Scatter Plot for X3 and Y3 Dataset Pair ", x = "X3 Value Scale ", y = "Y3 Value Scale") + coord_cartesian(ylim = c(0, 15), xlim = c(2 , 15)) +  geom_smooth(method = "lm", se = FALSE, colour ='darkgrey') + cleanup



# Plot-4:


X4_Y4_Scatter_Plot_d<-ggplot(data, 
                                   aes(data$x4, data$y4, colour = "blue"))

# Addition of subsequent layers to the grouped scatter object layer

X4_Y4_Plot <- X4_Y4_Scatter_Plot_d + geom_point() + scale_colour_manual(values = "Blue") +
  labs(title = "Scatter Plot for X4 and Y4 Dataset Pair ", x = "X4 Value Scale ", y = "Y4 Value Scale") + coord_cartesian(ylim = c(0, 12), xlim = c(2 , 15)) +  geom_smooth(method = "lm", se = FALSE, colour ='darkgrey') + cleanup



## GGplot determination of all the Data set "X" and "Y" pairs:

Plot_c <- plot_grid(X1_Y1_Plot, X2_Y2_Plot, X3_Y3_Plot, X4_Y4_Plot)
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'
Plot_c

## Creation of a four panel scatter plot comprising of data points and regression lines via "R" function:

## Linear Model-1 Plot_1:

par(mfrow = c(2,2))
plot(Linear_Model_1)

## Linear Model-2 Plot_2:

par(mfrow = c(2,2))
plot(Linear_Model_2)

## Linear Model-3 Plot_3:

par(mfrow = c(2,2))
plot(Linear_Model_3)

## Linear Model-4 Plot_4:

par(mfrow = c(2,2))
plot(Linear_Model_4)

  1. Now compare the model fits for each model object.
## Determination of Model Fit for different derived Linear Models:

# Model-1 Fit:

Linear_Model_1<- lm(data$y1 ~ data$x1, data = data)

anova(Linear_Model_1)

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

# Model-2 Fit:

Linear_Model_2<- lm(data$y2 ~ data$x2, data = data)

anova(Linear_Model_2)

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

# Model-3 Fit:

Linear_Model_3<- lm(data$y3 ~ data$x3, data = data)

anova(Linear_Model_3)

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

# Model-4 Fit:

Linear_Model_4<- lm(data$y4 ~ data$x4, data = data)

anova(Linear_Model_4)

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

  1. In text, summarize the lesson of Anscombe’s Quartet and what it says about the value of data visualization.
## Ancombe's Quartet speaks a lot about the huge variations that might exist in the trend analysis between the various dataset pairs, and which before the implementation or application of the data visualization techniques, or before generating data visuals would hardly be even visible or noticeable in the statistical data variable format. Also, it is further clearly evident from the multiple data visualization scatter plot outputs derived for all the four different "x", and "y" pairs of the Ancombe's Quartet dataset, that how unique the trend and correlation between the various "X", and "Y" variables data pairs might represent, and with an aid of the data visualization technique's driven outputs, we can further determine if the relationship trend between the various response or explanatory variables is either positive or negative and in addition to have an idea about the strength of the linear or non-linear relationship that might exist between the dataset variables.