Estadística descriptiva
En este ejemplo veremos algunos métodos gráficos de la estadística descriptiva.
Lo primero que hay que tener claro es que este primer análisis tiene diferentes objetivos como:
Este paso resulta de gran utilidad ya que puede guiarnos en la dirección adecuada cuando no sabemos qué tecnica utilizar para resolver un problema. También puede ayudarnos a responder preguntas sencillas y ver problemas que de otra forma sería dificl identificar, como la presencia de valorea atípicos o relaciones entre variables.
En R existen dos “maneras” de hacer gráficas, elegir una u otra depende del objetivo de las gráficas. la primera forma es usar R base (tambien llamado paquete {base}) que consta de todas las funciones que tenemos disponibles al instalar por primera vez R; la segunda forma es utilizar el paquete {ggplot2} que se caracteriza por tener un mejor diseño. Veremos algunos ejemplos.
Usaremos la base iris de R, para saber qué es lo que contiene la base podemos usar la ayuda:
?iris
head(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
str(iris)
## 'data.frame': 150 obs. of 5 variables:
## $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
summary(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
## 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
## Median :5.800 Median :3.000 Median :4.350 Median :1.300
## Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
## 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
## Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
## Species
## setosa :50
## versicolor:50
## virginica :50
##
##
##
mean(iris$Sepal.Length)
## [1] 5.843333
median(iris$Sepal.Length)
## [1] 5.8
tabla<-table(iris$Sepal.Length)
sort(tabla, decreasing=T)
##
## 5 5.1 6.3 5.7 6.7 5.5 5.8 6.4 4.9 5.4 5.6 6 6.1 4.8 6.5 4.6 5.2 6.2 6.9 7.7
## 10 9 9 8 8 7 7 7 6 6 6 6 6 5 5 4 4 4 4 4
## 4.4 5.9 6.8 7.2 4.7 6.6 4.3 4.5 5.3 7 7.1 7.3 7.4 7.6 7.9
## 3 3 3 3 2 2 1 1 1 1 1 1 1 1 1
tabla2<-table(iris$Species)
sort(tabla2, decreasing=T)
##
## setosa versicolor virginica
## 50 50 50
min(iris$Sepal.Length)
## [1] 4.3
max(iris$Sepal.Length)
## [1] 7.9
rango<-range(iris$Sepal.Length)
rango
## [1] 4.3 7.9
rango[2]-rango[1]
## [1] 3.6
rango.fun<-function(x){
return(max(x)-min(x))
}
rango.fun(iris$Sepal.Length)
## [1] 3.6
quantile(iris$Sepal.Length, 0.25)
## 25%
## 5.1
quantile(iris$Sepal.Length, 0.5)
## 50%
## 5.8
IQR(iris$Sepal.Length)
## [1] 1.3
var(iris$Sepal.Length)
## [1] 0.6856935
sd(iris$Sepal.Length)
## [1] 0.8280661
Es posible aplicar una función a cada variable usando la función sapply:
sapply(iris[, 1:4], sd)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 0.8280661 0.4358663 1.7652982 0.7622377
o aplicar una fución a cada grupo:
by(iris, iris$Species, summary)
## iris$Species: setosa
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.300 Min. :2.300 Min. :1.000 Min. :0.100
## 1st Qu.:4.800 1st Qu.:3.200 1st Qu.:1.400 1st Qu.:0.200
## Median :5.000 Median :3.400 Median :1.500 Median :0.200
## Mean :5.006 Mean :3.428 Mean :1.462 Mean :0.246
## 3rd Qu.:5.200 3rd Qu.:3.675 3rd Qu.:1.575 3rd Qu.:0.300
## Max. :5.800 Max. :4.400 Max. :1.900 Max. :0.600
## Species
## setosa :50
## versicolor: 0
## virginica : 0
##
##
##
## ------------------------------------------------------------
## iris$Species: versicolor
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## Min. :4.900 Min. :2.000 Min. :3.00 Min. :1.000 setosa : 0
## 1st Qu.:5.600 1st Qu.:2.525 1st Qu.:4.00 1st Qu.:1.200 versicolor:50
## Median :5.900 Median :2.800 Median :4.35 Median :1.300 virginica : 0
## Mean :5.936 Mean :2.770 Mean :4.26 Mean :1.326
## 3rd Qu.:6.300 3rd Qu.:3.000 3rd Qu.:4.60 3rd Qu.:1.500
## Max. :7.000 Max. :3.400 Max. :5.10 Max. :1.800
## ------------------------------------------------------------
## iris$Species: virginica
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.900 Min. :2.200 Min. :4.500 Min. :1.400
## 1st Qu.:6.225 1st Qu.:2.800 1st Qu.:5.100 1st Qu.:1.800
## Median :6.500 Median :3.000 Median :5.550 Median :2.000
## Mean :6.588 Mean :2.974 Mean :5.552 Mean :2.026
## 3rd Qu.:6.900 3rd Qu.:3.175 3rd Qu.:5.875 3rd Qu.:2.300
## Max. :7.900 Max. :3.800 Max. :6.900 Max. :2.500
## Species
## setosa : 0
## versicolor: 0
## virginica :50
##
##
##
o a cada grupo y variable
by(iris, iris$Species, FUN = function(x) colMeans(x[,1:4]))
## iris$Species: setosa
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 5.006 3.428 1.462 0.246
## ------------------------------------------------------------
## iris$Species: versicolor
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 5.936 2.770 4.260 1.326
## ------------------------------------------------------------
## iris$Species: virginica
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 6.588 2.974 5.552 2.026
iris$size<-ifelse(iris$Sepal.Length < median(iris$Sepal.Length),"small", "big")
table(iris$size)
##
## big small
## 77 73
table(iris$Species, iris$size)
##
## big small
## setosa 1 49
## versicolor 29 21
## virginica 47 3
Barras
barplot(table(iris$size))
library(ggplot2)
ggplot(iris)+geom_bar(aes(x=size, fill=size), show.legend=F)+
labs(
x="Tamaño",
y="Frecuencia",
title="Gráfica de barras",
subtitle="Usando colores",
caption="Quitando etiquetas"
)
Histograma
hist(iris$Sepal.Length)
ggplot(iris)+geom_histogram(aes(x=Sepal.Length))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(iris)+geom_histogram(aes(x=Sepal.Length, fill=Species),alpha=0.5,bins=12)
Diagrama de caja
boxplot(iris$Sepal.Length)
boxplot(iris$Sepal.Length ~ iris$Species)
ggplot(iris) +aes(x = Species, y = Sepal.Length) +geom_boxplot(aes(color=Species), show.legend = F)
Diagrama de dispersión
plot(iris$Sepal.Length, iris$Petal.Length)
ggplot(iris) +aes(x = Sepal.Length, y = Petal.Length) + geom_point()
ggplot(iris) +
geom_point(aes(x = Sepal.Length, y = Petal.Length, colour = Species, shape= Species), alpha=0.5)
QQ-plot
qqnorm(iris$Sepal.Length)
qqline(iris$Sepal.Length)
library(car)
## Loading required package: carData
qqPlot(iris$Sepal.Length)
## [1] 132 118
library(ggpubr)
ggqqplot(iris$Sepal.Length)
qqPlot(iris$Sepal.Length, groups = iris$size)
Densidad
plot(density(iris$Sepal.Length))
ggplot(iris) +aes(x = Sepal.Length) + geom_density()
OTRAS HERRAMIENTAS
#older version devtools::install_github('dcomtois/summarytools', ref='0-8-9')
library(summarytools)
## Registered S3 method overwritten by 'pryr':
## method from
## print.bytes Rcpp
freq(iris$Species)
## Frequencies
## iris$Species
## **Type:** Factor (unordered)
##
## Freq % Valid % Valid Cum. % Total % Total Cum.
## ---------------- ------ --------- -------------- --------- --------------
## setosa 50 33.33 33.33 33.33 33.33
## versicolor 50 33.33 66.67 33.33 66.67
## virginica 50 33.33 100.00 33.33 100.00
## <NA> 0 0.00 100.00
## Total 150 100.00 100.00 100.00 100.00
ctable(x = iris$Species,y = iris$size)
## Cross-Tabulation / Row Proportions
## **Variables:** Species * size
## **Data Frame:** iris
##
## ------------ ------ ------------- ------------- ---------------
## size big small Total
## Species
## setosa 1 ( 2.00%) 49 (98.00%) 50 (100.00%)
## versicolor 29 (58.00%) 21 (42.00%) 50 (100.00%)
## virginica 47 (94.00%) 3 ( 6.00%) 50 (100.00%)
## Total 77 (51.33%) 73 (48.67%) 150 (100.00%)
## ------------ ------ ------------- ------------- ---------------