Anscombe
En este ejemplo trabajaremos con el cuarteto de Anscombe, el cual es un cuarteto de conjuntos de datos creados para remarcar la importancia de las graficas en el análisis estadÃstico. Primero veamos las caracterÃsticas de los conjuntos:
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
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 ...
summary(anscombe)
## 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
var(anscombe)
## x1 x2 x3 x4 y1 y2 y3 y4
## x1 11.000 11.000 11.000 -5.500 5.501000 5.500000 5.49700 -2.115000
## x2 11.000 11.000 11.000 -5.500 5.501000 5.500000 5.49700 -2.115000
## x3 11.000 11.000 11.000 -5.500 5.501000 5.500000 5.49700 -2.115000
## x4 -5.500 -5.500 -5.500 11.000 -3.565000 -4.841000 -2.32100 5.499000
## y1 5.501 5.501 5.501 -3.565 4.127269 3.095609 1.93343 -2.017731
## y2 5.500 5.500 5.500 -4.841 3.095609 4.127629 2.42524 -1.972351
## y3 5.497 5.497 5.497 -2.321 1.933430 2.425240 4.12262 -0.641000
## y4 -2.115 -2.115 -2.115 5.499 -2.017731 -1.972351 -0.64100 4.123249
Rcuerde que los conjuntos van por pares, es decir, (x1,y1), (x2,y2), (x3,y3) y (x4,y4).
A continuación ajustamos un modelo lineal a cada par
modelo1 <- lm(y1~x1, data = anscombe)
modelo2 <- lm(y2~x2, data = anscombe)
modelo3 <- lm(y3~x3, data = anscombe)
modelo4 <- lm(y4~x4, data = anscombe)
summary(modelo1)
##
## Call:
## lm(formula = y1 ~ x1, data = anscombe)
##
## 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(modelo2)
##
## Call:
## lm(formula = y2 ~ x2, data = anscombe)
##
## 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(modelo3)
##
## Call:
## lm(formula = y3 ~ x3, data = anscombe)
##
## 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(modelo4)
##
## Call:
## lm(formula = y4 ~ x4, data = anscombe)
##
## 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
¿Qué información se puede extraer del summary?
Finalmente grafiquemos los datos junto con el modelo correspondiente en cada caso
ggplot(anscombe, aes(x1,y1))+ geom_point()+geom_smooth(method = 'lm', formula= y~x)
ggplot(anscombe, aes(x2,y2))+ geom_point()+geom_smooth(method = 'lm', formula= y~x)
ggplot(anscombe, aes(x3,y3))+ geom_point()+geom_smooth(method = 'lm', formula= y~x)
ggplot(anscombe, aes(x4,y4))+ geom_point()+geom_smooth(method = 'lm', formula= y~x)
Existe otro paquete que nos muestra conjutnos creados de manera similar:
library(datasauRus)
ggplot(data=datasaurus_dozen, aes(x=x, y=y, colour=dataset))+geom_point()+facet_wrap(~dataset, ncol=3)