R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

  1. Anscombes 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.
#install.packages("datasets", repos = "http://cran.us.r-project.org")
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
View(anscombe)
data <- anscombe
View(data)
  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 fBasics() package!)
#install.packages("fBasics")
library(fBasics)
## Loading required package: timeDate
## Loading required package: timeSeries
View(data)
summary(data)
##        x1             x2             x3             x4    
##  Min.   : 4.0   Min.   : 4.0   Min.   : 4.0   Min.   : 8  
##  1st Qu.: 6.5   1st Qu.: 6.5   1st Qu.: 6.5   1st Qu.: 8  
##  Median : 9.0   Median : 9.0   Median : 9.0   Median : 8  
##  Mean   : 9.0   Mean   : 9.0   Mean   : 9.0   Mean   : 9  
##  3rd Qu.:11.5   3rd Qu.:11.5   3rd Qu.:11.5   3rd Qu.: 8  
##  Max.   :14.0   Max.   :14.0   Max.   :14.0   Max.   :19  
##        y1               y2              y3              y4        
##  Min.   : 4.260   Min.   :3.100   Min.   : 5.39   Min.   : 5.250  
##  1st Qu.: 6.315   1st Qu.:6.695   1st Qu.: 6.25   1st Qu.: 6.170  
##  Median : 7.580   Median :8.140   Median : 7.11   Median : 7.040  
##  Mean   : 7.501   Mean   :7.501   Mean   : 7.50   Mean   : 7.501  
##  3rd Qu.: 8.570   3rd Qu.:8.950   3rd Qu.: 7.98   3rd Qu.: 8.190  
##  Max.   :10.840   Max.   :9.260   Max.   :12.74   Max.   :12.500
x1<-data[,1]
x2<-data[,2]
x3<-data[,3]
x4<-data[,4]
y1<-data[,5]
y2<-data[,6]
y3<-data[,7]
y4<-data[,8]

#mean(x1)
#mean(x2)mean(x3)mean(x4)mean(y1)mean(y2)mean(y3)mean(y4)
#var(x1)var(x2)var(x3)var(x4)var(y1)var(y2)var(y3)var(y4)
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
C1 <- cor(data$x1, data$y1)
C1
## [1] 0.8164205
C2 <- cor(data$x2, data$y2)
C3 <- cor(data$x3, data$y3)
C4 <- cor(data$x4, data$y4)
  1. Create scatter plots for each \(x, y\) pair of data.
plot(x1, y1, main = "Correlation1",
     xlab = "X1", ylab = "Y1",
     pch = 11, frame = FALSE)

plot(x2, y2, main = "Correlation2",
     xlab = "X2", ylab = "Y2",
     pch = 11, frame = FALSE)

plot(x3, y3, main = "Correlation3",
     xlab = "X3", ylab = "Y3",
     pch = 12, frame = FALSE)

plot(x4, y4, main = "Correlation4",
     xlab = "X4", ylab = "Y4",
     pch = 13, frame = FALSE)

  1. Now change the symbols on the scatter plots to solid circles and plot them together as a 4 panel graphic
par(mfrow=c(2,2))
    
plot(x1, y1, main = "Correlation1",
     xlab = "X1", ylab = "Y1",
     pch = 19, frame = FALSE)

plot(x2, y2, main = "Correlation2",
     xlab = "X2", ylab = "Y2",
     pch = 19, frame = FALSE)

plot(x3, y3, main = "Correlation3",
     xlab = "X3", ylab = "Y3",
     pch = 19, frame = FALSE)


plot(x4, y4, main = "Correlation4",
     xlab = "X4", ylab = "Y4",
     pch = 19, frame = FALSE)

  1. Now fit a linear model to each data set using the lm() function.
Model1 <- lm(y1~x1, data = data)
summary(Model1)
## 
## 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
Model2 <- lm(y2~x2, data = data)
summary(Model2)
## 
## 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
Model3 <- lm(y3~x3, data = data)
summary(Model3)
## 
## 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
Model4 <- lm(y4~x4, data = data)
summary(Model4)
## 
## 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
  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!)
par(mfrow=c(2,2))

plot(x1, y1, main = "Correlation1",
     xlab = "X1", ylab = "Y1",
     pch = 19, frame = FALSE, abline(Model1))

plot(x2, y2, main = "Correlation2",
     xlab = "X2", ylab = "Y2",
     pch = 19, frame = FALSE, abline(Model2))

plot(x3, y3, main = "Correlation3",
     xlab = "X3", ylab = "Y3",
     pch = 19, frame = FALSE, abline(Model3))


plot(x4, y4, main = "Correlation4",
     xlab = "X4", ylab = "Y4",
     pch = 19, frame = FALSE, abline(Model4))

  1. Now compare the model fits for each model object.
anova(Model1)

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(Model2)

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(Model3)

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(Model4)

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