Bioestadística - Doctorado en Ciencias Biomédicas

Dr. Manuel A. Barrios Izás

2024-03-01

Variables en Estadística

1. Variables Categóricas o Nominales:

2. Variables Ordinales:

3. Variables Numéricas o Cuantitativas:

a. Variables Discretas:

  • Toman valores individuales y a menudo son conteos.
  • No pueden tomar valores intermedios.
  • Ejemplos: número de hijos, cantidad de libros vendidos, número de accidentes de tráfico en un día.

b. Variables Continuas:

  • Pueden tomar cualquier valor dentro de un rango específico.
  • A menudo surgen de la medición.
  • Ejemplos: altura, peso, temperatura, ingresos.

Ejemplos

  1. Variable Categórica/Nominal:
    • Variable: Tipo de animal doméstico.
    • Valores: Perro, gato, conejo, pájaro.
  2. Variable Ordinal:
    • Variable: Nivel de satisfacción con un servicio (en una escala de 1 a 5).
    • Valores: 1 (Muy insatisfecho), 2 (Insatisfecho), 3 (Neutral), 4 (Satisfecho), 5 (Muy satisfecho).
  3. Variable Discreta:
    • Variable: Número de hijos en una familia.
    • Valores: 0, 1, 2, 3, …
  4. Variable Continua:
    • Variable: Altura de los estudiantes.
    • Valores: 160 cm, 165.5 cm, 172 cm, …

MEDIDAS DE TENDENCIA CENTRAL Y DE DISPERSION

Medidas de Tendencia Central

Las medidas de tendencia central y de dispersión son herramientas fundamentales en estadística descriptiva que nos ayudan a resumir y comprender conjuntos de datos.

Medidas de tendencia central

Medida de Tendencia Central Comando en R
Media mean(datos)
Media geométrica exp(mean(log(datos)))
Mediana median(datos)
Moda mlv(datos)$moda

Las medidas de tendencia central son valores que representan el centro o la concentración de un conjunto de datos. Las tres medidas de tendencia central más comunes son la media, la mediana y la moda.

#install.packages(r2resize)
library(r2resize);add.resizer()
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(tidyr)

st<-(starwars)

names(st)
##  [1] "name"       "height"     "mass"       "hair_color" "skin_color"
##  [6] "eye_color"  "birth_year" "sex"        "gender"     "homeworld" 
## [11] "species"    "films"      "vehicles"   "starships"
head(st)
## # A tibble: 6 × 14
##   name      height  mass hair_color skin_color eye_color birth_year sex   gender
##   <chr>      <int> <dbl> <chr>      <chr>      <chr>          <dbl> <chr> <chr> 
## 1 Luke Sky…    172    77 blond      fair       blue            19   male  mascu…
## 2 C-3PO        167    75 <NA>       gold       yellow         112   none  mascu…
## 3 R2-D2         96    32 <NA>       white, bl… red             33   none  mascu…
## 4 Darth Va…    202   136 none       white      yellow          41.9 male  mascu…
## 5 Leia Org…    150    49 brown      light      brown           19   fema… femin…
## 6 Owen Lars    178   120 brown, gr… light      blue            52   male  mascu…
## # ℹ 5 more variables: homeworld <chr>, species <chr>, films <list>,
## #   vehicles <list>, starships <list>
#View(st)
#st %>% 
#  st.filtrado1 <- filter(species == 'Human') %>%
#  na.omit()
st.human.birth <- st %>%
  filter(species == 'Human') %>%
  drop_na(birth_year)
barplot(st.human.birth$birth_year)

##Estimar media
mean(st.human.birth$birth_year)
## [1] 53.74231
#Estimar Media Geometrica
exp(mean(log(st.human.birth$birth_year)))
## [1] 48.07766
#Estimar Mediana
median(st.human.birth$birth_year)
## [1] 50
min(st.human.birth$birth_year)
## [1] 19
max(st.human.birth$birth_year)
## [1] 102
#install.packages('modeest')
library(modeest)

mlv(st.human.birth$birth_year, method = "mfv")
## [1] 19.0 41.9 72.0 82.0

Medidas de dispersión

Las medidas de dispersión nos indican cuánto se alejan los datos individuales de la medida de tendencia central. Las dos medidas de dispersión más comunes son el rango y la desviación estándar.

Estas medidas son esenciales para resumir y comprender conjuntos de datos, permitiendo a los analistas y científicos obtener una visión clara de la distribución y variabilidad de los datos que están estudiando.

boxplot(st.human.birth$birth_year, col="red",
        xlab="Humanos", ylab="Altura (cms)")

names(st)
##  [1] "name"       "height"     "mass"       "hair_color" "skin_color"
##  [6] "eye_color"  "birth_year" "sex"        "gender"     "homeworld" 
## [11] "species"    "films"      "vehicles"   "starships"
st.human.height <- st %>%
  filter(species == 'Human') %>%
  drop_na(height)
# Calcular el rango
rango <- max(st.human.height$height) - min(st.human.height$height)
print(rango)
## [1] 52
# Calcular la varianza
varianza <- var(st.human.height$height)
print(varianza)
## [1] 145.1034
# Calcular la desviación estándar
desviacion_estandar <- sd(st.human.height$height)
print(desviacion_estandar)
## [1] 12.04589
# Calcular la desviación media absoluta (DMA)
desviacion_media_absoluta <- mean(abs(st.human.height$height - mean(st.human.height$height)))
print(desviacion_media_absoluta)
## [1] 9.466667
# Calcular el coeficiente de variación
coeficiente_variacion <- sd(st.human.height$height) / mean(st.human.height$height) * 100
print(coeficiente_variacion)
## [1] 6.767354

Ejercicio

Elabore un grafico de cajas entre hombres y mujeres humanos vs altura (height)

names(st)
##  [1] "name"       "height"     "mass"       "hair_color" "skin_color"
##  [6] "eye_color"  "birth_year" "sex"        "gender"     "homeworld" 
## [11] "species"    "films"      "vehicles"   "starships"
st.height.human <- st %>%
  filter(species == 'Human') %>%
  drop_na(height) %>%
  drop_na(species)
#View(st.height.human)
str(st.human.height)
## tibble [30 × 14] (S3: tbl_df/tbl/data.frame)
##  $ name      : chr [1:30] "Luke Skywalker" "Darth Vader" "Leia Organa" "Owen Lars" ...
##  $ height    : int [1:30] 172 202 150 178 165 183 182 188 180 180 ...
##  $ mass      : num [1:30] 77 136 49 120 75 84 77 84 NA 80 ...
##  $ hair_color: chr [1:30] "blond" "none" "brown" "brown, grey" ...
##  $ skin_color: chr [1:30] "fair" "white" "light" "light" ...
##  $ eye_color : chr [1:30] "blue" "yellow" "brown" "blue" ...
##  $ birth_year: num [1:30] 19 41.9 19 52 47 24 57 41.9 64 29 ...
##  $ sex       : chr [1:30] "male" "male" "female" "male" ...
##  $ gender    : chr [1:30] "masculine" "masculine" "feminine" "masculine" ...
##  $ homeworld : chr [1:30] "Tatooine" "Tatooine" "Alderaan" "Tatooine" ...
##  $ species   : chr [1:30] "Human" "Human" "Human" "Human" ...
##  $ films     :List of 30
##   ..$ : chr [1:5] "A New Hope" "The Empire Strikes Back" "Return of the Jedi" "Revenge of the Sith" ...
##   ..$ : chr [1:4] "A New Hope" "The Empire Strikes Back" "Return of the Jedi" "Revenge of the Sith"
##   ..$ : chr [1:5] "A New Hope" "The Empire Strikes Back" "Return of the Jedi" "Revenge of the Sith" ...
##   ..$ : chr [1:3] "A New Hope" "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr [1:3] "A New Hope" "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr "A New Hope"
##   ..$ : chr [1:6] "A New Hope" "The Empire Strikes Back" "Return of the Jedi" "The Phantom Menace" ...
##   ..$ : chr [1:3] "The Phantom Menace" "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr [1:2] "A New Hope" "Revenge of the Sith"
##   ..$ : chr [1:4] "A New Hope" "The Empire Strikes Back" "Return of the Jedi" "The Force Awakens"
##   ..$ : chr [1:3] "A New Hope" "The Empire Strikes Back" "Return of the Jedi"
##   ..$ : chr [1:5] "The Empire Strikes Back" "Return of the Jedi" "The Phantom Menace" "Attack of the Clones" ...
##   ..$ : chr [1:3] "The Empire Strikes Back" "Return of the Jedi" "Attack of the Clones"
##   ..$ : chr [1:2] "The Empire Strikes Back" "Return of the Jedi"
##   ..$ : chr "The Empire Strikes Back"
##   ..$ : chr "Return of the Jedi"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr [1:3] "The Phantom Menace" "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr [1:2] "The Phantom Menace" "Attack of the Clones"
##   ..$ : chr [1:3] "The Phantom Menace" "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr [1:2] "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr [1:2] "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr [1:2] "A New Hope" "Revenge of the Sith"
##  $ vehicles  :List of 30
##   ..$ : chr [1:2] "Snowspeeder" "Imperial Speeder Bike"
##   ..$ : chr(0) 
##   ..$ : chr "Imperial Speeder Bike"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr "Tribubble bongo"
##   ..$ : chr [1:2] "Zephyr-G swoop bike" "XJ-6 airspeeder"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr "Snowspeeder"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr "Tribubble bongo"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr "Flitknot speeder"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##  $ starships :List of 30
##   ..$ : chr [1:2] "X-wing" "Imperial shuttle"
##   ..$ : chr "TIE Advanced x1"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr "X-wing"
##   ..$ : chr [1:5] "Jedi starfighter" "Trade Federation cruiser" "Naboo star skiff" "Jedi Interceptor" ...
##   ..$ : chr [1:3] "Naboo fighter" "Trade Federation cruiser" "Jedi Interceptor"
##   ..$ : chr(0) 
##   ..$ : chr [1:2] "Millennium Falcon" "Imperial shuttle"
##   ..$ : chr "X-wing"
##   ..$ : chr(0) 
##   ..$ : chr "Slave 1"
##   ..$ : chr "Millennium Falcon"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr [1:3] "Naboo fighter" "H-type Nubian yacht" "Naboo star skiff"
##   ..$ : chr "Naboo Royal Starship"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0)
st.height.human$sex <- as.factor(st.height.human$sex)
st.height.human$height <- as.numeric(st.height.human$height)

plot(st.height.human$height~st.height.human$sex)

plot(st.height.human$height ~st.height.human$sex, 
     xlab='Sexo', ylab='Altura (cms)',
     col='red')

Elabore un grafico de dispersión para visualizar como afecta la Altura a la biomasa

head(st.height.human)
## # A tibble: 6 × 14
##   name      height  mass hair_color skin_color eye_color birth_year sex   gender
##   <chr>      <dbl> <dbl> <chr>      <chr>      <chr>          <dbl> <fct> <chr> 
## 1 Luke Sky…    172    77 blond      fair       blue            19   male  mascu…
## 2 Darth Va…    202   136 none       white      yellow          41.9 male  mascu…
## 3 Leia Org…    150    49 brown      light      brown           19   fema… femin…
## 4 Owen Lars    178   120 brown, gr… light      blue            52   male  mascu…
## 5 Beru Whi…    165    75 brown      light      blue            47   fema… femin…
## 6 Biggs Da…    183    84 black      light      brown           24   male  mascu…
## # ℹ 5 more variables: homeworld <chr>, species <chr>, films <list>,
## #   vehicles <list>, starships <list>
plot(st.height.human$mass ~st.height.human$height, 
     xlab='Altura (cms)', ylab='Peso (kg)',
     col='blue', pch = 3)

Elabore un grafico de cajas para ver como cambian la media y la varianza entre el peso de cada especie

st.height <- st %>%
  drop_na(height) 

str(st.height)
## tibble [81 × 14] (S3: tbl_df/tbl/data.frame)
##  $ name      : chr [1:81] "Luke Skywalker" "C-3PO" "R2-D2" "Darth Vader" ...
##  $ height    : int [1:81] 172 167 96 202 150 178 165 97 183 182 ...
##  $ mass      : num [1:81] 77 75 32 136 49 120 75 32 84 77 ...
##  $ hair_color: chr [1:81] "blond" NA NA "none" ...
##  $ skin_color: chr [1:81] "fair" "gold" "white, blue" "white" ...
##  $ eye_color : chr [1:81] "blue" "yellow" "red" "yellow" ...
##  $ birth_year: num [1:81] 19 112 33 41.9 19 52 47 NA 24 57 ...
##  $ sex       : chr [1:81] "male" "none" "none" "male" ...
##  $ gender    : chr [1:81] "masculine" "masculine" "masculine" "masculine" ...
##  $ homeworld : chr [1:81] "Tatooine" "Tatooine" "Naboo" "Tatooine" ...
##  $ species   : chr [1:81] "Human" "Droid" "Droid" "Human" ...
##  $ films     :List of 81
##   ..$ : chr [1:5] "A New Hope" "The Empire Strikes Back" "Return of the Jedi" "Revenge of the Sith" ...
##   ..$ : chr [1:6] "A New Hope" "The Empire Strikes Back" "Return of the Jedi" "The Phantom Menace" ...
##   ..$ : chr [1:7] "A New Hope" "The Empire Strikes Back" "Return of the Jedi" "The Phantom Menace" ...
##   ..$ : chr [1:4] "A New Hope" "The Empire Strikes Back" "Return of the Jedi" "Revenge of the Sith"
##   ..$ : chr [1:5] "A New Hope" "The Empire Strikes Back" "Return of the Jedi" "Revenge of the Sith" ...
##   ..$ : chr [1:3] "A New Hope" "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr [1:3] "A New Hope" "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr "A New Hope"
##   ..$ : chr "A New Hope"
##   ..$ : chr [1:6] "A New Hope" "The Empire Strikes Back" "Return of the Jedi" "The Phantom Menace" ...
##   ..$ : chr [1:3] "The Phantom Menace" "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr [1:2] "A New Hope" "Revenge of the Sith"
##   ..$ : chr [1:5] "A New Hope" "The Empire Strikes Back" "Return of the Jedi" "Revenge of the Sith" ...
##   ..$ : chr [1:4] "A New Hope" "The Empire Strikes Back" "Return of the Jedi" "The Force Awakens"
##   ..$ : chr "A New Hope"
##   ..$ : chr [1:3] "A New Hope" "Return of the Jedi" "The Phantom Menace"
##   ..$ : chr [1:3] "A New Hope" "The Empire Strikes Back" "Return of the Jedi"
##   ..$ : chr "A New Hope"
##   ..$ : chr [1:5] "The Empire Strikes Back" "Return of the Jedi" "The Phantom Menace" "Attack of the Clones" ...
##   ..$ : chr [1:5] "The Empire Strikes Back" "Return of the Jedi" "The Phantom Menace" "Attack of the Clones" ...
##   ..$ : chr [1:3] "The Empire Strikes Back" "Return of the Jedi" "Attack of the Clones"
##   ..$ : chr "The Empire Strikes Back"
##   ..$ : chr "The Empire Strikes Back"
##   ..$ : chr [1:2] "The Empire Strikes Back" "Return of the Jedi"
##   ..$ : chr "The Empire Strikes Back"
##   ..$ : chr [1:2] "Return of the Jedi" "The Force Awakens"
##   ..$ : chr "Return of the Jedi"
##   ..$ : chr "Return of the Jedi"
##   ..$ : chr "Return of the Jedi"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr [1:3] "The Phantom Menace" "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr [1:3] "The Phantom Menace" "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr [1:2] "The Phantom Menace" "Attack of the Clones"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr [1:2] "The Phantom Menace" "Attack of the Clones"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr [1:2] "The Phantom Menace" "Attack of the Clones"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr "Return of the Jedi"
##   ..$ : chr [1:3] "The Phantom Menace" "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr [1:3] "The Phantom Menace" "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr [1:3] "The Phantom Menace" "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr [1:3] "The Phantom Menace" "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr [1:2] "The Phantom Menace" "Revenge of the Sith"
##   ..$ : chr [1:2] "The Phantom Menace" "Revenge of the Sith"
##   ..$ : chr [1:2] "The Phantom Menace" "Revenge of the Sith"
##   ..$ : chr "The Phantom Menace"
##   ..$ : chr [1:3] "The Phantom Menace" "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr [1:2] "The Phantom Menace" "Attack of the Clones"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr [1:2] "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr [1:2] "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr [1:2] "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr [1:2] "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr [1:2] "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr "Attack of the Clones"
##   ..$ : chr [1:2] "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr "Revenge of the Sith"
##   ..$ : chr "Revenge of the Sith"
##   ..$ : chr [1:2] "A New Hope" "Revenge of the Sith"
##   ..$ : chr [1:2] "Attack of the Clones" "Revenge of the Sith"
##   ..$ : chr "Revenge of the Sith"
##  $ vehicles  :List of 81
##   ..$ : chr [1:2] "Snowspeeder" "Imperial Speeder Bike"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr "Imperial Speeder Bike"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr "Tribubble bongo"
##   ..$ : chr [1:2] "Zephyr-G swoop bike" "XJ-6 airspeeder"
##   ..$ : chr(0) 
##   ..$ : chr "AT-ST"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr "Snowspeeder"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr "Tribubble bongo"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr "Sith speeder"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr "Flitknot speeder"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr "Koro-2 Exodrive airspeeder"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr "Tsmeu-6 personal wheel bike"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##  $ starships :List of 81
##   ..$ : chr [1:2] "X-wing" "Imperial shuttle"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr "TIE Advanced x1"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr "X-wing"
##   ..$ : chr [1:5] "Jedi starfighter" "Trade Federation cruiser" "Naboo star skiff" "Jedi Interceptor" ...
##   ..$ : chr [1:3] "Naboo fighter" "Trade Federation cruiser" "Jedi Interceptor"
##   ..$ : chr(0) 
##   ..$ : chr [1:2] "Millennium Falcon" "Imperial shuttle"
##   ..$ : chr [1:2] "Millennium Falcon" "Imperial shuttle"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr "X-wing"
##   ..$ : chr "X-wing"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr "Slave 1"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr "Millennium Falcon"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr "Millennium Falcon"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr [1:3] "Naboo fighter" "H-type Nubian yacht" "Naboo star skiff"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr "Naboo Royal Starship"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr "Scimitar"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr "Jedi starfighter"
##   ..$ : chr(0) 
##   ..$ : chr "Naboo fighter"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr "Belbullab-22 starfighter"
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0) 
##   ..$ : chr(0)
st.height$height<-as.numeric(st.height$height)
st.height$species<-as.factor(st.height$species)

plot(st.height$height~st.height$species,
     xlab='Especies', ylab='Altura (cms)')

plot(st.height$height~st.height$species,
     xlab='Especies', ylab='Altura (cms)',
     ylim=c(150,190))

st.height.mtc <- st %>%
  drop_na(height) %>%
  group_by(species) %>%
  summarize(media = mean(height), desv_est= sd(height))
st.height.mtc
## # A tibble: 38 × 3
##    species   media desv_est
##    <chr>     <dbl>    <dbl>
##  1 Aleena      79      NA  
##  2 Besalisk   198      NA  
##  3 Cerean     198      NA  
##  4 Chagrian   196      NA  
##  5 Clawdite   168      NA  
##  6 Droid      131.     49.1
##  7 Dug        112      NA  
##  8 Ewok        88      NA  
##  9 Geonosian  183      NA  
## 10 Gungan     209.     14.2
## # ℹ 28 more rows
#?filter(n())

plot(st.height$height~st.height$species,
     xlab='Especies', ylab='Altura (cms)')

st.species.n2 <- st %>%
  drop_na(height) %>%
  group_by(species) %>%
  mutate(freq = n()) %>%
  filter(freq >= 2) %>%
  select(-freq)

st.species.n2$species<-as.factor(st.species.n2$species)

plot(st.species.n2$height~st.species.n2$species,
     xlab='Especie', ylab='Altura (cms)',
     col='light blue')

library(ggplot2)

ggplot(st.species.n2, aes(x = species, y = height, fill = species)) + #Llamar la bd y las variables
  geom_boxplot(color = "black", alpha = 0.7) + #Tipo de gr[afico]
  scale_fill_brewer(palette = "Set3") +  # Colocar paleta de colores
  theme_minimal() +  # Seleccionar un tema minimalista
  labs(x = "Especies", y = "Altura (cms)", title = "Altura por Especie") +
  theme(plot.title = element_text(hjust = 0.5))  # Centrar el titulo

Calcular la media y la varianza para todas las especies

st %>%
  na.omit(st$height) %>%
  group_by(species) %>%
  summarise(Media = mean(height), Desv_Est = sd(height)) 
## # A tibble: 11 × 3
##    species      Media Desv_Est
##    <chr>        <dbl>    <dbl>
##  1 Cerean        198     NA   
##  2 Ewok           88     NA   
##  3 Gungan        196     NA   
##  4 Human         179.    11.5 
##  5 Kel Dor       188     NA   
##  6 Mirialan      168      2.83
##  7 Mon Calamari  180     NA   
##  8 Trandoshan    190     NA   
##  9 Twi'lek       178     NA   
## 10 Wookiee       228     NA   
## 11 Zabrak        175     NA

Calcular la media y la varianza para los sexos de los humanos