Realizar cálculos de probabilida bajo la distribución Normal
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
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)
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
*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
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
*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
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
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
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
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
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
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
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
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
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
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
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
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