output: html_document: distill::distill_article: default theme: flatly highlight: haddock transition: concave center: true toc: true toc_float: collapsed: true smooth_scroll: true toc_depth: 2 fig_caption: yes code_folding: show number_sections: false fontsize: 15pt weight: 5 type: docs —

output: html_document: distill::distill_article: default theme: flatly highlight: haddock transition: concave center: true toc: true toc_float: collapsed: true smooth_scroll: true toc_depth: 2 fig_caption: yes code_folding: show number_sections: false fontsize: 15pt weight: 5 type: docs —

Importancia de la simulación contexto estadístico

Introducción Markdown

Campo de R -> Ctrl+Alt+i

set.seed(1234)
# Generación de datos 
diam = runif(n = 24,min = 4,max = 6)
diam
##  [1] 4.227407 5.244599 5.218549 5.246759 5.721831 5.280621 4.018992 4.465101
##  [9] 5.332168 5.028502 5.387183 5.089950 4.565467 5.846867 4.584632 5.674591
## [17] 4.572447 4.533642 4.373446 4.464452 4.633225 4.605387 4.318092 4.079992

Campo de ecuación \(\LARGE{\LaTeX}\)

\[t=\frac{\bar x-\mu}{s/\sqrt{n}}\]

La ecuación anterior representa un estadístico de prueba, t-student
# Redondear a 2 decimales
diam = round(diam,2)
orient = gl(n = 2,k = 12,length = 24,labels = c("ecuatorial","longitudinal"))
df = data.frame(orient,diam)
head(df) #cabeza
##       orient diam
## 1 ecuatorial 4.23
## 2 ecuatorial 5.24
## 3 ecuatorial 5.22
## 4 ecuatorial 5.25
## 5 ecuatorial 5.72
## 6 ecuatorial 5.28
tail(df) #cola
##          orient diam
## 19 longitudinal 4.37
## 20 longitudinal 4.46
## 21 longitudinal 4.63
## 22 longitudinal 4.61
## 23 longitudinal 4.32
## 24 longitudinal 4.08

Resumen estadístico descriptivo

# boxplot 
boxplot(diam~orient,horizontal = T,col=c("lightgreen","lightblue"))

# Distribución exponencial 

diam2 = rexp(n = 24,rate = 1/4)

# boxplot 
boxplot(diam2~orient,horizontal = T,col=c("lightgreen","lightblue"))

Agregarle promedios a las cajas

# Medias de diam
m1 = tapply(diam,orient,mean);m1
##   ecuatorial longitudinal 
##     5.022500     4.686667
# Medias de diam2
m2 = tapply(diam2,orient,mean);m2
##   ecuatorial longitudinal 
##     3.444301     3.396487
# boxplot + medias
boxplot(diam~orient,horizontal = T,col=c("lightgreen","lightblue"),xlab = "Diametro (cm)",ylab="Orientación")
points(y=1:2,x=m1,pch=16,col="red",cex=1.5)
rug(diam[which(orient=="ecuatorial")],lwd = 3,side = 3,col="darkgreen")
rug(diam[which(orient=="longitudinal")],lwd = 3,side = 1,col="blue")

# Instalando librerias 
library(ggplot2)
# Densidad 
set.seed(1234)
# Generación de datos 
diam = runif(n = 240,min = 4,max = 6)
orient = gl(n = 2,k = 120,length = 240,labels = c("ecuatorial","longitudinal"))
# Usando ggplot2 
# Grafico de densidades
ggplot(df,aes(x=diam,fill=orient))+
  geom_density(alpha=0.4)

# Grafico de histogramas
ggplot(df,aes(x=diam,fill=orient))+
  geom_histogram(alpha=0.4)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# Grafico de cajas o boxplot
ggplot(df,aes(x=diam,fill=orient))+
  geom_boxplot(alpha=0.4)

# Grafico de violines
ggplot(df,aes(x=diam,fill=orient,y=orient))+
  geom_violin(alpha=0.4)

df2=split(diam,orient)
df2 = data.frame(ecuatorial=df2$ecuatorial,
                 longitudinal = df2$longitudinal)
ggplot(df2,aes(x=ecuatorial,
               y=longitudinal))+  geom_point(size=2)

library(psych)
## 
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
describe(diam)
##    vars   n mean   sd median trimmed mad  min max range skew kurtosis   se
## X1    1 240 4.98 0.57   4.99    4.97 0.7 4.02   6  1.98 0.11    -1.14 0.04
describeBy(diam,group = orient)
## 
##  Descriptive statistics by group 
## group: ecuatorial
##    vars   n mean   sd median trimmed mad  min  max range skew kurtosis   se
## X1    1 120 4.85 0.57   4.65    4.83 0.6 4.02 5.98  1.97 0.34    -1.18 0.05
## ------------------------------------------------------------ 
## group: longitudinal
##    vars   n mean   sd median trimmed  mad  min max range  skew kurtosis   se
## X1    1 120  5.1 0.53   5.13    5.11 0.65 4.04   6  1.95 -0.05    -0.96 0.05

Coeficiente de variación

\[\%~CV = \frac{s}{\bar{x}}\times 100\]

# Mi primera funcion 

fun_cv= function(datos){
  media=mean(datos)
  desv = sd(datos)
  cv = 100*desv/media
  return(cv)
}

# evaluando la funcion 

cat("%CV ecuatorial",fun_cv(df2$ecuatorial))
## %CV ecuatorial 11.7698
cat("%CV longitudinal",fun_cv(df2$longitudinal))
## %CV longitudinal 10.44618
datos=rep(0,50)
fun_cv(datos)
## [1] NaN
# Validando inicialmente la funcion  

fun_cv= function(datos){
  media=mean(datos)
  desv = sd(datos)
  if(media==0 & desv==0){
    print("Indeterminación")
  }else{
  cv = 100*desv/media
  return(cv)
  }
}
fun_cv(datos)
## [1] "Indeterminación"