dt <- c(12.48712333, 28.62000058, 22.49583037, 18.76243756, 6.36852193, 6.36784657, 3.62634114, 26.25293208, 18.83122033, 21.82603218, 2.57636584, 29.15747586, 25.30839394, 7.9454951, 7.09109908, 7.13532628, 10.5187828, 16.69318009, 14.09446052, 10.15441593, 19.13188105, 5.9058281, 10.18005016, 12.25813161, 14.76995956, 23.98492692, 7.5908659, 16.39856428, 18.58760793, 3.30061156, 19.01125585, 6.77467546, 3.8214446, 28.56879504, 29.03769693, 24.63512575, 10.52918554, 4.73481919, 21.15852474, 14.32426982, 5.41707058, 15.86495348, 2.96287859, 27.46097126, 9.24583948, 20.55062396, 10.72791013, 16.56190459, 17.30788782, 7.17592475)
dt
##  [1] 12.487123 28.620001 22.495830 18.762438  6.368522  6.367847  3.626341
##  [8] 26.252932 18.831220 21.826032  2.576366 29.157476 25.308394  7.945495
## [15]  7.091099  7.135326 10.518783 16.693180 14.094461 10.154416 19.131881
## [22]  5.905828 10.180050 12.258132 14.769960 23.984927  7.590866 16.398564
## [29] 18.587608  3.300612 19.011256  6.774675  3.821445 28.568795 29.037697
## [36] 24.635126 10.529186  4.734819 21.158525 14.324270  5.417071 15.864953
## [43]  2.962879 27.460971  9.245839 20.550624 10.727910 16.561905 17.307888
## [50]  7.175925
mean(dt)
## [1] 14.48587
sd(dt)
## [1] 8.08873
coef_var <- function(x, na.rm = FALSE) {
  (sd(x, na.rm=na.rm) / mean(x, na.rm=na.rm))*100
}
coef_var(dt)
## [1] 55.83876
set.seed(123)
df <- data.frame(sample(dt,10), coef_var(dt,10))
df
##    sample.dt..10. coef_var.dt..10.
## 1       19.011256         55.83876
## 2        7.091099         55.83876
## 3        7.945495         55.83876
## 4       22.495830         55.83876
## 5       15.864953         55.83876
## 6        2.962879         55.83876
## 7       10.529186         55.83876
## 8       16.561905         55.83876
## 9       14.769960         55.83876
## 10      23.984927         55.83876
set.seed(123)
cv <- c(coef_var(sample(dt,2)), coef_var(sample(dt,3)),coef_var(sample(dt,4)),
        coef_var(sample(dt,5)), coef_var(sample(dt,6)), coef_var(sample(dt,7)),
        coef_var(sample(dt,8)), coef_var(sample(dt,9)), coef_var(sample(dt,10)),
        coef_var(sample(dt,11)), coef_var(sample(dt,12)),coef_var(sample(dt,13)),
        coef_var(sample(dt,14)), coef_var(sample(dt,15)),coef_var(sample(dt,16)),
        coef_var(sample(dt,17)), coef_var(sample(dt,18)),coef_var(sample(dt,19)),
        coef_var(sample(dt,20)), coef_var(sample(dt,21)), coef_var(sample(dt,22)),
        coef_var(sample(dt,23)), coef_var(sample(dt,24)), coef_var(sample(dt,25)),
        coef_var(sample(dt,26)), coef_var(sample(dt,27)),coef_var(sample(dt,28)),
        coef_var(sample(dt,29)), coef_var(sample(dt,30)),coef_var(sample(dt,31)),
        coef_var(sample(dt,32)), coef_var(sample(dt,33)),coef_var(sample(dt,34)),
        coef_var(sample(dt,35)), coef_var(sample(dt,36)), coef_var(sample(dt,37)),
        coef_var(sample(dt,38)), coef_var(sample(dt,39)), coef_var(sample(dt,40)),
        coef_var(sample(dt,41)), coef_var(sample(dt,42)),coef_var(sample(dt,43)),
        coef_var(sample(dt,44)), coef_var(sample(dt,45)),coef_var(sample(dt,46)),
        coef_var(sample(dt,47)), coef_var(sample(dt,48)),coef_var(sample(dt,49)),
        coef_var(sample(dt,50)))
cv
##  [1] 64.58286 47.19449 43.89948 51.80360 22.48820 51.42567 72.06585 60.66366
##  [9] 53.69531 73.43471 51.80064 54.35886 60.71246 59.33560 58.93418 46.68507
## [17] 64.58284 58.16586 53.54450 60.06213 53.13344 48.24625 44.86416 52.01933
## [25] 62.03518 62.78704 57.65620 52.09665 56.06765 60.13183 56.21602 56.45530
## [33] 57.83786 52.45928 57.46214 57.82029 55.25569 56.84635 56.50802 51.81090
## [41] 55.70801 53.89054 57.22240 53.01299 56.32260 57.33615 56.10896 56.53691
## [49] 55.83876
library(ggplot2)
dt1 <- c(2:50)
dt1
##  [1]  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
## [26] 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
df <- data.frame(dt1,cv)
df
##    dt1       cv
## 1    2 64.58286
## 2    3 47.19449
## 3    4 43.89948
## 4    5 51.80360
## 5    6 22.48820
## 6    7 51.42567
## 7    8 72.06585
## 8    9 60.66366
## 9   10 53.69531
## 10  11 73.43471
## 11  12 51.80064
## 12  13 54.35886
## 13  14 60.71246
## 14  15 59.33560
## 15  16 58.93418
## 16  17 46.68507
## 17  18 64.58284
## 18  19 58.16586
## 19  20 53.54450
## 20  21 60.06213
## 21  22 53.13344
## 22  23 48.24625
## 23  24 44.86416
## 24  25 52.01933
## 25  26 62.03518
## 26  27 62.78704
## 27  28 57.65620
## 28  29 52.09665
## 29  30 56.06765
## 30  31 60.13183
## 31  32 56.21602
## 32  33 56.45530
## 33  34 57.83786
## 34  35 52.45928
## 35  36 57.46214
## 36  37 57.82029
## 37  38 55.25569
## 38  39 56.84635
## 39  40 56.50802
## 40  41 51.81090
## 41  42 55.70801
## 42  43 53.89054
## 43  44 57.22240
## 44  45 53.01299
## 45  46 56.32260
## 46  47 57.33615
## 47  48 56.10896
## 48  49 56.53691
## 49  50 55.83876
ggplot(df, aes(x=dt1,y=cv))+
  geom_point()+
  stat_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE))+
  geom_vline(xintercept = 30,linetype = 2,color = 2)+
  labs(x="Número de muestras (n)", y= "Coeficiente de variación (%)",
             title= "Optimización de muestreo")+
  theme_minimal()

library(readxl)
data <- read_excel("~/MANEJOS/MANEJO INTEGRADO DE ENFERMEDADES/taller_1.xlsx")
data
## # A tibble: 50 × 4
##        N     Z     e     n
##    <dbl> <dbl> <dbl> <dbl>
##  1 10000    80    20    11
##  2 10000    85    15    23
##  3 10000    90    10    68
##  4 10000    95     5   370
##  5 10000    99     1  6247
##  6  9000    80    20    11
##  7  9000    85    15    23
##  8  9000    90    10    68
##  9  9000    95     5   369
## 10  9000    99     1  5842
## # ℹ 40 more rows
data$N=as.factor(data$N)
data
## # A tibble: 50 × 4
##    N         Z     e     n
##    <fct> <dbl> <dbl> <dbl>
##  1 10000    80    20    11
##  2 10000    85    15    23
##  3 10000    90    10    68
##  4 10000    95     5   370
##  5 10000    99     1  6247
##  6 9000     80    20    11
##  7 9000     85    15    23
##  8 9000     90    10    68
##  9 9000     95     5   369
## 10 9000     99     1  5842
## # ℹ 40 more rows
library(ggplot2)

ggplot(data=data,aes(x=Z, y=n, color=N))+
  geom_line()+
  geom_point()+
   scale_x_continuous("Z", sec.axis = sec_axis(~abs(.-100), name = "e"))+
  ggtitle(label='Tamaño de muestra en un muestreo estratificado variando\n el nivel de confianza y el error estándar')