#Examen PyE 3 Unidad
#Elba María Ybarra

#2. Muestreo Aleatorio Simple
crimenes <- data.frame(crimtab)
dim(crimenes)
## [1] 924   3
n <- 50
crime <- sample(1:nrow(crimenes), size = n, replace = FALSE)
crime
##  [1] 246 907  53 170 580 663 803 573 523 202 858  99 645 263 355 257 722
## [18] 723 751 578 483 330 540 608 905 834 815 809 100 563 430 909 893 655
## [35]  83   8 368  14 712 498  57 380 487 860 729 408 618 767 880 383
crimes <- crimenes[crime,]
head(crimes)
##     Var1   Var2 Freq
## 246 12.9 154.94    0
## 907 11.8 195.58    0
## 53  10.4 144.78    0
## 170  9.5  152.4    0
## 580 12.7 175.26    5
## 663 12.6 180.34    6
#3. Muestreo Aleatorio sin Reemplazo
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.5.3
## 
## 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
crime.sin <- crimenes %>%
  sample_n(size = n, replace = FALSE)
head(crime.sin)
##   Var1   Var2 Freq
## 1  9.5 182.88    0
## 2   10 175.26    0
## 3 11.1 157.48   22
## 4  9.5  177.8    0
## 5 12.3 147.32    0
## 6  9.5 149.86    0
#4. Muestreo con Ponderaciones
crime.pesos <- crimenes %>%
  sample_frac(0.04)
head(crime.pesos); dim(crime.pesos)
##   Var1   Var2 Freq
## 1 11.1 187.96    0
## 2   11 157.48   15
## 3 13.4 147.32    0
## 4 11.9 193.04    0
## 5 10.2 180.34    0
## 6 12.6 147.32    0
## [1] 37  3
#5.Muestreo Estratificado
library(dplyr)
set.seed(1)
sample_iris <- iris %>%
  group_by(Species) %>%
  sample_n(10)
sample_iris
## # A tibble: 30 x 5
## # Groups:   Species [3]
##    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
##           <dbl>       <dbl>        <dbl>       <dbl> <fct>  
##  1          4.3         3            1.1         0.1 setosa 
##  2          5.7         3.8          1.7         0.3 setosa 
##  3          5.2         3.5          1.5         0.2 setosa 
##  4          4.4         3.2          1.3         0.2 setosa 
##  5          4.9         3.1          1.5         0.1 setosa 
##  6          5           3.5          1.3         0.3 setosa 
##  7          4.5         2.3          1.3         0.3 setosa 
##  8          5.2         3.4          1.4         0.2 setosa 
##  9          5           3.4          1.6         0.4 setosa 
## 10          4.7         3.2          1.3         0.2 setosa 
## # ... with 20 more rows
#6. Prueba de Hipotesis y Normalidad
library(readxl)
correlacion <- read_excel("correlacion.xlsx")
View(correlacion)
boxplot(correlacion$chino ~ correlacion$cerdo, col = "pink")

#Prueba de Normalidad Shapiro Wilk
shapiro.test(correlacion$cerdo)
## 
##  Shapiro-Wilk normality test
## 
## data:  correlacion$cerdo
## W = 0.76938, p-value = 1.228e-10