Analisis de la funcion de densidad

#ColOpenData::climate_tags

hogares=fread("C:/Users/germa_30zwgix/OneDrive/Documentos/R/hogares.txt",header=TRUE)
glue::glue("Cantidad de Hogares {dim(hogares)[1]}")
## Cantidad de Hogares 23573893
names(hogares)
##  [1] "CNS"         "U_DPRTMNT"   "U_MNCP"      "U_CLS"       "P_SX"       
##  [6] "EDD_MYR_99"  "MRC_LEA"     "CNTDD_PRSNS" "LATI_MGN"    "LONG_MGN"
hogaresuniq=hogares %>% distinct(LATI_MGN,LONG_MGN, .keep_all = T)
glue::glue("Cantidad de coordenadas unicas {dim(hogaresuniq)[1]}")
## Cantidad de coordenadas unicas 12904346
#names(tumbas)

#hogares$paste=paste0(hogares$LATI_MGN,hogares$LONG_MGN)
#hogares$paste

cente=fread("C:/Users/germa_30zwgix/OneDrive/Documentos/R/puntoshogares.txt",header=TRUE)
centeuniq=cente %>% distinct(LATI_MGN,LONG_MGN, .keep_all = T)
glue::glue("Numero de coordenadas unicas con centenarios {dim(centeuniq)[1]}")
## Numero de coordenadas unicas con centenarios 11879
cente_base=merge(hogaresuniq,centeuniq,by=c("LATI_MGN","LONG_MGN"),all.x=T)

tumbaspp=ppp(cente_base$LONG_MGN,cente_base$LATI_MGN,c(min(cente_base$LONG_MGN)-1,max(cente_base$LONG_MGN)+1),       c(min(cente_base$LATI_MGN)-1,max(cente_base$LATI_MGN)+1))

#plot(tumbaspp)
casos=subset(tumbaspp,!is.na(cente_base$CNTDD_PRSNS_MYR_99))
controles=subset(tumbaspp,is.na(cente_base$CNTDD_PRSNS_MYR_99))

glue::glue("distribucion de puntos segun reporte de centenarios")
## distribucion de puntos segun reporte de centenarios
kbl(table(!is.na(cente_base$CNTDD_PRSNS_MYR_99)), escape = F) %>%
  kable_paper("hover", full_width = F)
Var1 Freq
FALSE 12892467
TRUE 11879
par(mfrow=c(1,2),mar=c(2,2,2,2)/2)
plot(casos)
plot(controles)

#par(mfrow=c(1,1),mar=c(2,2,2,2)/2)
#risk(casos,controles,doplot = T)

bw=seq(0.00001,0.5,length=10)
par(mfrow=c(4,4),mar=c(2,2,2,2)/2)

for (i in 1:length(bw))
{
plot(density.ppp(casos,sigma=bw[i]),cex.main=0.3,main=paste("casos, bw = ",round(bw[i],2)))
plot(density.ppp(controles,sigma=bw[i]),cex.main=0.3,main=paste("controles, bw = ",round(bw[i],2)))
}

#bw_fija=LSCV.risk(casos,controles)
bw_fija=0.06

f.tilde=bivariate.density(casos,h0=bw_fija)
g.tilde=bivariate.density(controles,h0=bw_fija)

rho.tilde=risk(f=f.tilde,g=g.tilde)

par(mfrow=c(1,1),mar=c(2,2,2,2)/2)

pval.tilde=tolerance(rs=rho.tilde,method="MC",ITER=200)
##   |                                                                              |                                                                      |   0%  |                                                                              |                                                                      |   1%  |                                                                              |=                                                                     |   1%  |                                                                              |=                                                                     |   2%  |                                                                              |==                                                                    |   3%  |                                                                              |==                                                                    |   4%  |                                                                              |===                                                                   |   4%  |                                                                              |===                                                                   |   5%  |                                                                              |====                                                                  |   5%  |                                                                              |====                                                                  |   6%  |                                                                              |=====                                                                 |   7%  |                                                                              |=====                                                                 |   8%  |                                                                              |======                                                                |   8%  |                                                                              |======                                                                |   9%  |                                                                              |=======                                                               |  10%  |                                                                              |=======                                                               |  11%  |                                                                              |========                                                              |  11%  |                                                                              |========                                                              |  12%  |                                                                              |=========                                                             |  13%  |                                                                              |=========                                                             |  14%  |                                                                              |==========                                                            |  14%  |                                                                              |==========                                                            |  15%  |                                                                              |===========                                                           |  15%  |                                                                              |===========                                                           |  16%  |                                                                              |============                                                          |  17%  |                                                                              |============                                                          |  18%  |                                                                              |=============                                                         |  18%  |                                                                              |=============                                                         |  19%  |                                                                              |==============                                                        |  20%  |                                                                              |==============                                                        |  21%  |                                                                              |===============                                                       |  21%  |                                                                              |===============                                                       |  22%  |                                                                              |================                                                      |  23%  |                                                                              |=================                                                     |  24%  |                                                                              |=================                                                     |  25%  |                                                                              |==================                                                    |  25%  |                                                                              |==================                                                    |  26%  |                                                                              |===================                                                   |  27%  |                                                                              |===================                                                   |  28%  |                                                                              |====================                                                  |  28%  |                                                                              |====================                                                  |  29%  |                                                                              |=====================                                                 |  30%  |                                                                              |=====================                                                 |  31%  |                                                                              |======================                                                |  31%  |                                                                              |======================                                                |  32%  |                                                                              |=======================                                               |  32%  |                                                                              |=======================                                               |  33%  |                                                                              |========================                                              |  34%  |                                                                              |========================                                              |  35%  |                                                                              |=========================                                             |  35%  |                                                                              |=========================                                             |  36%  |                                                                              |==========================                                            |  37%  |                                                                              |==========================                                            |  38%  |                                                                              |===========================                                           |  38%  |                                                                              |===========================                                           |  39%  |                                                                              |============================                                          |  40%  |                                                                              |============================                                          |  41%  |                                                                              |=============================                                         |  41%  |                                                                              |=============================                                         |  42%  |                                                                              |==============================                                        |  42%  |                                                                              |==============================                                        |  43%  |                                                                              |===============================                                       |  44%  |                                                                              |===============================                                       |  45%  |                                                                              |================================                                      |  45%  |                                                                              |================================                                      |  46%  |                                                                              |=================================                                     |  47%  |                                                                              |=================================                                     |  48%  |                                                                              |==================================                                    |  48%  |                                                                              |==================================                                    |  49%  |                                                                              |===================================                                   |  50%  |                                                                              |====================================                                  |  51%  |                                                                              |====================================                                  |  52%  |                                                                              |=====================================                                 |  52%  |                                                                              |=====================================                                 |  53%  |                                                                              |======================================                                |  54%  |                                                                              |======================================                                |  55%  |                                                                              |=======================================                               |  55%  |                                                                              |=======================================                               |  56%  |                                                                              |========================================                              |  57%  |                                                                              |========================================                              |  58%  |                                                                              |=========================================                             |  58%  |                                                                              |=========================================                             |  59%  |                                                                              |==========================================                            |  59%  |                                                                              |==========================================                            |  60%  |                                                                              |===========================================                           |  61%  |                                                                              |===========================================                           |  62%  |                                                                              |============================================                          |  62%  |                                                                              |============================================                          |  63%  |                                                                              |=============================================                         |  64%  |                                                                              |=============================================                         |  65%  |                                                                              |==============================================                        |  65%  |                                                                              |==============================================                        |  66%  |                                                                              |===============================================                       |  67%  |                                                                              |===============================================                       |  68%  |                                                                              |================================================                      |  68%  |                                                                              |================================================                      |  69%  |                                                                              |=================================================                     |  69%  |                                                                              |=================================================                     |  70%  |                                                                              |==================================================                    |  71%  |                                                                              |==================================================                    |  72%  |                                                                              |===================================================                   |  72%  |                                                                              |===================================================                   |  73%  |                                                                              |====================================================                  |  74%  |                                                                              |====================================================                  |  75%  |                                                                              |=====================================================                 |  75%  |                                                                              |=====================================================                 |  76%  |                                                                              |======================================================                |  77%  |                                                                              |=======================================================               |  78%  |                                                                              |=======================================================               |  79%  |                                                                              |========================================================              |  79%  |                                                                              |========================================================              |  80%  |                                                                              |=========================================================             |  81%  |                                                                              |=========================================================             |  82%  |                                                                              |==========================================================            |  82%  |                                                                              |==========================================================            |  83%  |                                                                              |===========================================================           |  84%  |                                                                              |===========================================================           |  85%  |                                                                              |============================================================          |  85%  |                                                                              |============================================================          |  86%  |                                                                              |=============================================================         |  86%  |                                                                              |=============================================================         |  87%  |                                                                              |==============================================================        |  88%  |                                                                              |==============================================================        |  89%  |                                                                              |===============================================================       |  89%  |                                                                              |===============================================================       |  90%  |                                                                              |================================================================      |  91%  |                                                                              |================================================================      |  92%  |                                                                              |=================================================================     |  92%  |                                                                              |=================================================================     |  93%  |                                                                              |==================================================================    |  94%  |                                                                              |==================================================================    |  95%  |                                                                              |===================================================================   |  95%  |                                                                              |===================================================================   |  96%  |                                                                              |====================================================================  |  96%  |                                                                              |====================================================================  |  97%  |                                                                              |===================================================================== |  98%  |                                                                              |===================================================================== |  99%  |                                                                              |======================================================================|  99%  |                                                                              |======================================================================| 100%
#plot(pval.tilde)
plot(rho.tilde,xlab="Este", ylab="Norte");tol.contour(pim=pval.tilde,levels=c(0.05,0.01),lty=1:2,add=TRUE)

#View(pval.tilde$v)