Objetivo

Realizar cálculos de probabilida bajo la distribución Normal

Las librerías

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(mosaic)
## Warning: package 'mosaic' was built under R version 3.6.3
## Loading required package: lattice
## Loading required package: ggformula
## Warning: package 'ggformula' was built under R version 3.6.3
## Loading required package: ggplot2
## Loading required package: ggstance
## Warning: package 'ggstance' was built under R version 3.6.3
## 
## Attaching package: 'ggstance'
## The following objects are masked from 'package:ggplot2':
## 
##     geom_errorbarh, GeomErrorbarh
## 
## New to ggformula?  Try the tutorials: 
##  learnr::run_tutorial("introduction", package = "ggformula")
##  learnr::run_tutorial("refining", package = "ggformula")
## Loading required package: mosaicData
## Warning: package 'mosaicData' was built under R version 3.6.3
## Loading required package: Matrix
## Registered S3 method overwritten by 'mosaic':
##   method                           from   
##   fortify.SpatialPolygonsDataFrame ggplot2
## 
## The 'mosaic' package masks several functions from core packages in order to add 
## additional features.  The original behavior of these functions should not be affected by this.
## 
## Note: If you use the Matrix package, be sure to load it BEFORE loading mosaic.
## 
## Have you tried the ggformula package for your plots?
## 
## Attaching package: 'mosaic'
## The following object is masked from 'package:Matrix':
## 
##     mean
## The following object is masked from 'package:ggplot2':
## 
##     stat
## The following objects are masked from 'package:dplyr':
## 
##     count, do, tally
## The following objects are masked from 'package:stats':
## 
##     binom.test, cor, cor.test, cov, fivenum, IQR, median, prop.test,
##     quantile, sd, t.test, var
## The following objects are masked from 'package:base':
## 
##     max, mean, min, prod, range, sample, sum
library(readr)
library(ggplot2)  # Para gráficos
library(knitr)    # Para formateo de datos

Realizar cálculos de probabilidad conforme a la distribución normal

Importar y etiquetar y mostrar estadísticos del conjunto de datos

datos= read.table("https://raw.githubusercontent.com/rpizarrog/probabilidad-y-estad-stica/master/datos/body.dat.txt", quote="\"", comment.char="")
datos = as.data.frame(datos)
colnames(datos)[23:25] = c("peso", "estatura", "sexo")
datos= select(datos, peso, estatura, sexo)
kable(head(datos))
peso estatura sexo
65.6 174.0 1
71.8 175.3 1
80.7 193.5 1
72.6 186.5 1
78.8 187.2 1
74.8 181.5 1
datos$sexo = as.factor(datos$sexo)
summary(datos)
##       peso           estatura     sexo   
##  Min.   : 42.00   Min.   :147.2   0:260  
##  1st Qu.: 58.40   1st Qu.:163.8   1:247  
##  Median : 68.20   Median :170.3          
##  Mean   : 69.15   Mean   :171.1          
##  3rd Qu.: 78.85   3rd Qu.:177.8          
##  Max.   :116.40   Max.   :198.1
ggplot(datos, aes(sexo, peso)) +
  geom_boxplot(colour = c("deeppink", "dodgerblue4"))

ggplot(datos, aes(sexo, estatura)) +
  geom_boxplot(colour = c("deeppink", "dodgerblue4"))

h= filter(datos, sexo==1)
m= filter(datos, sexo==0)

Estadísticos hombres

Media, mínimo, máximo, mediana y cuantiles

summary(h)
##       peso           estatura     sexo   
##  Min.   : 53.90   Min.   :157.2   0:  0  
##  1st Qu.: 70.95   1st Qu.:172.9   1:247  
##  Median : 77.30   Median :177.8          
##  Mean   : 78.14   Mean   :177.7          
##  3rd Qu.: 85.50   3rd Qu.:182.7          
##  Max.   :116.40   Max.   :198.1

Desviaciones estándar

*Peso

mhp=mean(h$peso)
shp=sd(h$peso)
shp
## [1] 10.51289

*Estatura

mhe=mean(h$estatura)
she=sd(h$estatura)
she
## [1] 7.183629

Estadísticos mujeres

Media, mínimo, máximo, mediana y cuantiles

summary(m)
##       peso          estatura     sexo   
##  Min.   : 42.0   Min.   :147.2   0:260  
##  1st Qu.: 54.5   1st Qu.:160.0   1:  0  
##  Median : 59.0   Median :164.5          
##  Mean   : 60.6   Mean   :164.9          
##  3rd Qu.: 65.6   3rd Qu.:169.5          
##  Max.   :105.2   Max.   :182.9

Desviaciones estándar

*Peso

mmp=mean(m$peso)
smp=sd(m$peso)
smp
## [1] 9.615699

*Estatura

mme=mean(m$estatura)
smh=sd(m$estatura)
smh
## [1] 6.544602

Cálculo de probabilidades

1. Probabilidad de que un hombre pese más de 60 kilos

plotDist("norm", mean = mhp, sd = shp, groups = x > 60, type = "h", xlab = "Peso Hombres", ylab = "Densidad" )

1-pnorm(q = 60, mean = mhp, sd = shp, )
## [1] 0.9578202

2. Probabilidad de que un hombre pese entre 70 y 80 kg

plotDist("norm", mean = mhp, sd = shp, groups = x > 70 & x<80, type = "h", xlab = "Peso Hombres", ylab = "Densidad" )

pnorm(q = 80, mean = mhp, sd = shp, )-pnorm(q = 70, mean = mhp, sd = shp, )
## [1] 0.3507943

3. Probabilidad de que un hombre pese menos de 75kg

plotDist("norm", mean = mhp, sd = shp, groups =x<75, type = "h", xlab = "Peso Hombres", ylab = "Densidad" )

pnorm(q = 75, mean = mhp, sd = shp, )
## [1] 0.3824272

4. Probabilidad de que una mujer pese más de 55kg

plotDist("norm", mean = mmp, sd = smp, groups = x > 55, type = "h", xlab = "Peso Mujeres", ylab = "Densidad" )

1-pnorm(q = 55, mean = mmp, sd = smp, )
## [1] 0.7198584

5. Probabilidad de que una mujer pese entre 50 y 60 kg

plotDist("norm", mean = mmp, sd = smp, groups = x > 50 & x<60, type = "h", xlab = "Peso Mujeres", ylab = "Densidad" )

pnorm(q = 60, mean = mmp, sd = smp, )-pnorm(q = 50, mean = mmp, sd = smp, )
## [1] 0.339964

6. Probabilidad de que una mujer pese menos de 45kg

plotDist("norm", mean = mmp, sd = smp, groups =  x<45, type = "h", xlab = "Peso Mujeres", ylab = "Densidad" )

pnorm(q = 45, mean = mmp, sd = smp, )
## [1] 0.05236025

7. Probabilidad de que un hombre mida más de 165cm

plotDist("norm", mean = mhe, sd = she, groups = x > 165, type = "h", xlab = "Estatura Hombres", ylab = "Densidad" )

1-pnorm(q = 165, mean = mhe, sd = she, )
## [1] 0.9619867

8. Probabilidad de que un hombre mida entre 175 y 185

plotDist("norm", mean = mhe, sd = she, groups = x > 175 & x<185, type = "h", xlab = "Estatura Hombres", ylab = "Densidad" )

pnorm(q = 185, mean = mhe, sd = she, )-pnorm(q = 175, mean = mhe, sd = she, )
## [1] 0.4925565

9. Probabilidad de que un hombre mida menos de 170cm

plotDist("norm", mean = mhe, sd = she, groups =x<170, type = "h", xlab = "Estatura Hombres", ylab = "Densidad" )

pnorm(q = 170, mean = mhe, sd = she, )
## [1] 0.1404736

10. Probabilidad de que una mujer mida más de 155cm

plotDist("norm", mean = mme, sd = smh, groups = x > 155, type = "h", xlab = "Estatura Mujeres", ylab = "Densidad" )

1-pnorm(q = 155, mean = mme, sd = smh, )
## [1] 0.9342823

11. Probabilidad de que una mujer mida entre 165 y 170

plotDist("norm", mean = mme, sd = smh, groups = x > 165 & x<170, type = "h", xlab = "Peso Mujeres", ylab = "Densidad" )

pnorm(q = 170, mean = mme, sd = smh, )-pnorm(q = 165, mean = mme, sd = smh, )
## [1] 0.2755498

12. Probabilidad de que una mujer mida menos de 155

plotDist("norm", mean = mme, sd = smh, groups =  x<155, type = "h", xlab = "Estatura Mujeres", ylab = "Densidad" )

pnorm(q = 155, mean = mme, sd = smh, )
## [1] 0.06571769