library(readxl)
Datos_chipaque <- read.delim("~/MANEJOS/MANEJO INTEGRADO DE ENFERMEDADES/PROYECTO INDIVIDUAL/Datos chipaque.csv", sep = ";")

Datos_chipaque
##    muestra muestreo  longitud  latitud    inc   sev
## 1        1        1 -73.99945 4.446047  40.00  1.00
## 2        2        1 -73.99921 4.446315   0.00  0.00
## 3        3        1 -73.99938 4.446385  25.00  1.00
## 4        4        1 -73.99947 4.446292  37.50  1.00
## 5        5        1 -73.99961 4.446222  87.50  1.00
## 6        6        1 -73.99970 4.446238  25.00  1.00
## 7        7        1 -73.99972 4.446445  50.00  1.00
## 8        8        1 -73.99963 4.446558   0.00  0.00
## 9        9        1 -73.99998 4.447025  41.60  1.00
## 10      10        1 -74.00010 4.446742   0.00  0.00
## 11       1        2 -73.99934 4.446038  25.00  1.00
## 12       2        2 -73.99925 4.446345  25.00  1.00
## 13       3        2 -73.99940 4.446562  25.00  1.00
## 14       4        2 -73.99952 4.446370  37.50  1.00
## 15       5        2 -73.99956 4.446158  62.50  5.00
## 16       6        2 -73.99989 4.446360  12.50  1.00
## 17       7        2 -73.99977 4.446583  50.00  5.00
## 18       8        2 -73.99967 4.446725  50.00  5.00
## 19       9        2 -74.00015 4.447033  87.50 10.00
## 20      10        2 -74.00023 4.446912 100.00  5.00
## 21       1        3 -73.99941 4.446080  85.71 24.16
## 22       2        3 -73.99926 4.446353  71.42 10.00
## 23       3        3 -73.99949 4.446540 100.00 23.00
## 24       4        3 -73.99964 4.446443  85.71 11.00
## 25       5        3 -73.99971 4.446340  85.71 33.33
## 26       6        3 -73.99991 4.446542 100.00  8.71
## 27       7        3 -73.99982 4.446665  85.71  8.57
## 28       8        3 -73.99970 4.446732 100.00 24.28
## 29       9        3 -74.00004 4.446867 100.00 12.14
## 30      10        3 -74.00006 4.446702  85.71  6.83
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
dt1 <- filter(Datos_chipaque, muestreo=="1")
dt2 <- filter(Datos_chipaque, muestreo=="2")
dt3 <- filter(Datos_chipaque, muestreo=="3")
dt <- rbind(dt1$inc,dt2$inc, dt3$inc)
dt
##       [,1]  [,2] [,3]  [,4]  [,5]  [,6]  [,7] [,8]  [,9]  [,10]
## [1,] 40.00  0.00   25 37.50 87.50  25.0 50.00    0  41.6   0.00
## [2,] 25.00 25.00   25 37.50 62.50  12.5 50.00   50  87.5 100.00
## [3,] 85.71 71.42  100 85.71 85.71 100.0 85.71  100 100.0  85.71
dt <- colMeans(dt)
dt
##  [1] 50.23667 32.14000 50.00000 53.57000 78.57000 45.83333 61.90333 50.00000
##  [9] 76.36667 61.90333
# Función para calcular el coeficiente de variación acumulado
coef_variacion_acumulado <- function(dt) {
  # Inicializar un vector para almacenar los coeficientes de variación
  cv_acumulado <- numeric(length(dt))
  
  # Calcular el coeficiente de variación acumulado
  for (i in 1:length(dt)) {
    media_acumulada <- mean(dt[1:i])
    sd_acumulada <- sd(dt[1:i])
    cv_acumulado[i] <- (sd_acumulada / media_acumulada) * 100  # Multiplicamos por 100 para obtener porcentaje
  }
  
  return(cv_acumulado)
}
resultado <- coef_variacion_acumulado(dt)
print(resultado)
##  [1]       NA 31.06772 23.52485 20.87124 31.42973 29.28849 26.99301 25.26871
##  [9] 26.61676 25.07317
library(ggplot2)
dt <- c(1:10)
dt
##  [1]  1  2  3  4  5  6  7  8  9 10
df <- data.frame(dt,resultado)
df
##    dt resultado
## 1   1        NA
## 2   2  31.06772
## 3   3  23.52485
## 4   4  20.87124
## 5   5  31.42973
## 6   6  29.28849
## 7   7  26.99301
## 8   8  25.26871
## 9   9  26.61676
## 10 10  25.07317
ggplot(df, aes(x=dt,y=resultado))+
  geom_point()+
  stat_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE))+
  geom_vline(xintercept = 7,linetype = 2,color = 2)+
  labs(x="Número de muestras (n)", y= "Coeficiente de variación (%)",
             title= "Optimización de muestreo en función de la incidencia")+
  theme_minimal()
## Warning: Removed 1 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 1 rows containing missing values (`geom_point()`).

st <- rbind(dt1$sev,dt2$sev, dt3$sev)
st
##       [,1] [,2] [,3] [,4]  [,5] [,6] [,7]  [,8]  [,9] [,10]
## [1,]  1.00    0    1    1  1.00 1.00 1.00  0.00  1.00  0.00
## [2,]  1.00    1    1    1  5.00 1.00 5.00  5.00 10.00  5.00
## [3,] 24.16   10   23   11 33.33 8.71 8.57 24.28 12.14  6.83
st <- colMeans(st)
st
##  [1]  8.720000  3.666667  8.333333  4.333333 13.110000  3.570000  4.856667
##  [8]  9.760000  7.713333  3.943333
# Función para calcular el coeficiente de variación acumulado
coef_variacion_acumulado <- function(st) {
  # Inicializar un vector para almacenar los coeficientes de variación
  cv_acumulado <- numeric(length(st))
  
  # Calcular el coeficiente de variación acumulado
  for (i in 1:length(st)) {
    media_acumulada <- mean(st[1:i])
    sd_acumulada <- sd(st[1:i])
    cv_acumulado[i] <- (sd_acumulada / media_acumulada) * 100  # Multiplicamos por 100 para obtener porcentaje
  }
  
  return(cv_acumulado)
}
resultado <- coef_variacion_acumulado(st)
print(resultado)
##  [1]       NA 57.69504 40.72261 42.02783 50.01354 54.57336 53.40972 49.25338
##  [9] 45.69847 47.45119
library(ggplot2)
st <- c(1:10)
st
##  [1]  1  2  3  4  5  6  7  8  9 10
df <- data.frame(st,resultado)
df
##    st resultado
## 1   1        NA
## 2   2  57.69504
## 3   3  40.72261
## 4   4  42.02783
## 5   5  50.01354
## 6   6  54.57336
## 7   7  53.40972
## 8   8  49.25338
## 9   9  45.69847
## 10 10  47.45119
ggplot(df, aes(x=st,y=resultado))+
  geom_point()+
  stat_smooth(method = "lm", formula = y ~ poly(x, 2, raw = TRUE))+
  geom_vline(xintercept = 8,linetype = 2,color = 2)+
  labs(x="Número de muestras (n)", y= "Coeficiente de variación (%)",
             title= "Optimización de muestreo en función de la severidad")+
  theme_minimal()
## Warning: Removed 1 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 1 rows containing missing values (`geom_point()`).