## `summarise()` has grouped output by 'MICRORED'. You can override using the
## `.groups` argument.
## `summarise()` has grouped output by 'MICRORED'. You can override using the
## `.groups` argument.

## `summarise()` has grouped output by 'MICRORED', 'dia_de_semana'. You can
## override using the `.groups` argument.
## `summarise()` has grouped output by 'MICRORED', 'dia_de_semana'. You can
## override using the `.groups` argument.

## `summarise()` has grouped output by 'MICRORED', 'punto_inicio.factor'. You can
## override using the `.groups` argument.

## `summarise()` has grouped output by 'MICRORED'. You can override using the
## `.groups` argument.
## `summarise()` has grouped output by 'MICRORED', 'mov_inicial.factor'. You can
## override using the `.groups` argument.

## `summarise()` has grouped output by 'MICRORED', 'tipo_de_punto'. You can
## override using the `.groups` argument.
## `summarise()` has grouped output by 'MICRORED', 'tipo_de_punto'. You can
## override using the `.groups` argument.
## `summarise()` has grouped output by 'MICRORED', 'tipo_de_punto'. You can
## override using the `.groups` argument.

## `summarise()` has grouped output by 'MICRORED'. You can override using the
## `.groups` argument.

## `summarise()` has grouped output by 'MICRORED'. You can override using the
## `.groups` argument.
## `summarise()` has grouped output by 'MICRORED'. You can override using the
## `.groups` argument.

##cantidad de minutos en formato horas
horasxpunto<-boxpuntos %>%
  group_by(MICRORED,tipo_de_punto) %>%
  summarise(horas_sum = sum(total_horas, na.rm = TRUE))
## `summarise()` has grouped output by 'MICRORED'. You can override using the
## `.groups` argument.
horasxpunto$horas <- paste(floor(horasxpunto$horas_sum / 60))
horasxpunto<-left_join(horasxpunto, pob,by="MICRORED")
horasxpunto<-merge(horasxpunto,perrosmean, by=c("MICRORED","tipo_de_punto"))

horasxpunto<-horasxpunto %>% mutate(cobertura = ( vacunados/VACCINATED_DOGS) * 100)

horasxpunto$horas<-as.numeric(horasxpunto$horas)

#plot covertura y horas por tipo de punto
ggplot(horasxpunto, aes(x = horas, y = cobertura)) +
  geom_point(alpha= 0.7,  aes(color=tipo_de_punto, size=vacunados))+
  scale_size_continuous("Perros Vacunados",
    range = c(0, 20),
    breaks = seq(0, 10000, by = 1000),  # Define los valores de escala que deseas
    labels = c(0, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000))+
  labs(title = "Cobertura de Perros Vacunados",
       x = "Horas de vacunación",
       y = "Cobertura vacunación") +
  theme_bw()+scale_y_continuous(limits = c(0, 40), breaks = seq(0, 100, by = 5))+
  scale_x_continuous(limits = c(0, 600), breaks = seq(0, 600, by = 25))+
  theme(plot.title = element_text(size = 16),
        axis.text.x = element_text( size  = 13),axis.title.x = element_text(size = 18))+
  theme(axis.text.y = element_text( size  = 13),axis.title.y = element_text(size = 18),
        legend.text = element_text(size = 14),  
        legend.title = element_text(size = 15)) +
  geom_text_repel(aes(label = MICRORED),size=2.6)

#plot horas y perros vacunados por tipo de punto
ggplot(horasxpunto, aes(x = horas, y = vacunados)) +
  geom_point(alpha= 0.6,  aes(color=tipo_de_punto, size=cobertura))+
  scale_size_continuous("Cobertura de
Vacunación",
                        range = c(0, 10),
                        breaks = seq(0, 35, by = 5),
                        labels = c(0, 5, 10, 15, 20, 25, 30, 35))+
  labs(title = "Perros Vacunados por tipo de punto",
       x = "Horas de vacunación",
       y = "Perros vacunados") +
  theme_bw()+scale_y_continuous(limits = c(0, 12000), breaks = seq(0, 12000, by = 1000))+
  scale_x_continuous(limits = c(0, 600), breaks = seq(0, 600, by = 50))+
  theme(plot.title = element_text(size = 16),
        axis.text.x = element_text( size  = 15),axis.title.x = element_text(size = 18))+
  theme(axis.text.y = element_text( size  = 15),axis.title.y = element_text(size = 18),
        legend.text = element_text(size = 14),  
        legend.title = element_text(size = 15)) +
  geom_text_repel (aes(label = MICRORED), size=2.6)

####regresion para ver relacion entre tipo de punto, horas en campo y los perros vacunados####
horasindividual<-select(boxpuntos, id,tipo_de_punto,horas,total_horas,MICRORED, date)
pfii <- pfi  %>% rename( vacunados=perros_punto_fijo)
pmii <- pmi  %>% rename( vacunados=perros_punto_movil)
pppi <- ppp  %>% rename( vacunados=perros_punto_puerta)

coverindividual<-rbind(pfii,pmii,pppi)

coverindividual<-select(coverindividual, id,tipo_de_punto,MICRORED,
                        date,vacunados,VACCINATED_DOGS)

modvac<-left_join(horasindividual,coverindividual,by=c("id","MICRORED","date","tipo_de_punto") )

#plot individual por tipo de punto y perros vacunados
ggplot(modvac)+
  geom_point(aes(x= total_horas, y=vacunados,color=tipo_de_punto),alpha=0.7)+
  theme_bw() +   labs(title = "Perros Vacunados por tiempo de vacunación",
                      x = "Minutos de vacunación",
                      y = "Perros vacunados")+
  scale_x_continuous(limits = c(0, 400), breaks = seq(0, 400, by = 25))+
  scale_y_continuous(limits = c(0, 200), breaks = seq(0, 200, by = 25))

# Crear el gráfico interactivo con etiquetas
plot_ly(modvac, x = ~total_horas, y = ~vacunados, text = ~id, mode = "markers",
        color = ~tipo_de_punto, alpha = 0.8,
                   marker = list(size = 6, color = "grey50"))
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
#histograma vacunados
par(mfrow=c(1,2))
hist(modvac$vacunados)

hist(modvac$total_horas)

##difrencia entre puntos moviles, puerta a puerta y fijos
kruskal.test(vacunados ~ tipo_de_punto, data=modvac)
## 
##  Kruskal-Wallis rank sum test
## 
## data:  vacunados by tipo_de_punto
## Kruskal-Wallis chi-squared = 37.753, df = 2, p-value = 6.341e-09
pairwise.wilcox.test(modvac$vacunados, modvac$tipo_de_punto, p.adjust.method = "bonferroni")
## 
##  Pairwise comparisons using Wilcoxon rank sum test with continuity correction 
## 
## data:  modvac$vacunados and modvac$tipo_de_punto 
## 
##        fijo    movil
## movil  2.6e-09 -    
## puerta 1       1    
## 
## P value adjustment method: bonferroni
#modelos
#binomial negativa
modelbn<-glm.nb(vacunados ~ total_horas * tipo_de_punto, data=modvac)

summary(modelbn)
## 
## Call:
## glm.nb(formula = vacunados ~ total_horas * tipo_de_punto, data = modvac, 
##     init.theta = 3.469971808, link = log)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -4.7945  -0.7718  -0.1276   0.4510   6.0659  
## 
## Coefficients:
##                                   Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                      2.5465880  0.0539004  47.246  < 2e-16 ***
## total_horas                      0.0057514  0.0002252  25.541  < 2e-16 ***
## tipo_de_puntomovil               0.5473470  0.0941790   5.812 6.18e-09 ***
## tipo_de_puntopuerta              1.0356208  0.4041542   2.562   0.0104 *  
## total_horas:tipo_de_puntomovil  -0.0015732  0.0003957  -3.976 7.02e-05 ***
## total_horas:tipo_de_puntopuerta -0.0039712  0.0019612  -2.025   0.0429 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for Negative Binomial(3.47) family taken to be 1)
## 
##     Null deviance: 2312.2  on 1398  degrees of freedom
## Residual deviance: 1573.5  on 1393  degrees of freedom
## AIC: 13085
## 
## Number of Fisher Scoring iterations: 1
## 
## 
##               Theta:  3.470 
##           Std. Err.:  0.145 
## 
##  2 x log-likelihood:  -13070.714
par(mfrow=c(2,2))
plot(modelbn)

exp(modelbn$coefficients)
##                     (Intercept)                     total_horas 
##                      12.7634799                       1.0057680 
##              tipo_de_puntomovil             tipo_de_puntopuerta 
##                       1.7286609                       2.8168544 
##  total_horas:tipo_de_puntomovil total_horas:tipo_de_puntopuerta 
##                       0.9984280                       0.9960367