#Disimilariad: estadistico de logrank

library(spatstat)
library(spatial)



disiG4lg<-matrix(0,nrow=300,ncol=300)
disiF4lg<-matrix(0,nrow=300,ncol=300)


for(i in 1:299){

tipo<-ceiling(i/100)
numreplica<-i-(tipo-1)*100



if (tipo==1) fichero <- paste("st_0_0.07_",numreplica,".pp",sep="")
if (tipo==2) fichero<-paste("st_0.5_0.07_",numreplica,".pp",sep="")
if (tipo==3) fichero<-paste("st_1_0.07_",numreplica,".pp",sep="")  
  
  
  
  
  dat1<-read.table(file=fichero);



puntos1<-as.points(dat1[,1],dat1[,2])
  
  poly<-bbox(puntos1);
  ngrid <- sqrt(npts(puntos1))
  v1 <- poly[1,1]+c(1:ngrid)*(poly[3,1]-poly[1,1])/(ngrid+2)
  v2 <- poly[1,2]+c(1:ngrid)*(poly[3,2]-poly[1,2])/(ngrid+2)
  grid <- as.points(rep(v1[1],ngrid),v2)
                for(k in c(2:ngrid)){
            grid <- rbind(grid,as.points(rep(v1[k],ngrid),v2))
            }          
    distF1<-nndistF(puntos1,grid);
    distG1<-nndistG(puntos1)
    distG1<-distG1$dists
    xx1<-c(min(poly[,1]),max(poly[,1]))
    yy1<-c(min(poly[,2]),max(poly[,2]))

    puntos1<-ppp(dat1[,1],dat1[,2],xx1,yy1)
    front1<-bdist.points(puntos1)
    
 cr1go<-pmin(distG1,front1)
 cr1gd<-distG1<front1

    grid1<-ppp(grid[,1],grid[,2],xx1,yy1)
    front1<-bdist.points(grid1)
cr1fo<-pmin(distF1,front1)
 cr1fd<-distF1<front1



    rm(grid,poly,front1,puntos1,grid1);
    

    for(j in i:300){


 tipo<-ceiling(j/100)
    numreplica<-j-(tipo-1)*100


#tipo_ceiling(i/100)
#numreplica_i-(tipo-1)*100



if (tipo==1) fichero <- paste("st_0_0.07_",numreplica,".pp",sep="")
if (tipo==2) fichero<-paste("st_0.5_0.07_",numreplica,".pp",sep="")
if (tipo==3) fichero<-paste("st_1_0.07_",numreplica,".pp",sep="")  
  
  


 dat2<-read.table(file=fichero);

puntos2<-as.points(dat2[,1],dat2[,2])

poly<-bbox(puntos2);
 ngrid <- sqrt(npts(puntos2))
  v1 <- poly[1,1]+c(1:ngrid)*(poly[3,1]-poly[1,1])/(ngrid+2)
  v2 <- poly[1,2]+c(1:ngrid)*(poly[3,2]-poly[1,2])/(ngrid+2)
  grid <- as.points(rep(v1[1],ngrid),v2)
                for(k in c(2:ngrid)){
            grid <- rbind(grid,as.points(rep(v1[k],ngrid),v2))
            }
    distF2<-nndistF(puntos2,grid);
    distG2<-nndistG(puntos2)
    distG2<-distG2$dists

xx1<-c(min(poly[,1]),max(poly[,1]))
    yy1<-c(min(poly[,2]),max(poly[,2]))

    puntos2<-ppp(dat2[,1],dat2[,2],xx1,yy1)
    front2<-bdist.points(puntos2)
    
 cr2go<-pmin(distG2,front2)
 cr2gd<-distG2<front2

    grid2<-ppp(grid[,1],grid[,2],xx1,yy1)
    front2<-bdist.points(grid2)
cr2fo<-pmin(distF2,front2)
 cr2fd<-distF2<front2

rm(grid,poly,front2,puntos2,grid2);
    
   tiempog<-c(cr1go,cr2go)
  censurag<-as.numeric(c(as.logical(cr1gd),as.logical(cr2gd)))
  grupo<-c(rep(1,length(cr1go)),rep(2,length(cr2go)))
  
  kk<-survdiff(Surv(tiempog,censurag)~grupo,rho=0)

disiG4lg[i,j]<-kk$chi
disiG4lg[j,i]<-kk$chi

tiempof<-c(cr1fo,cr2fo)
  censuraf<-as.numeric(c(as.logical(cr1fd),as.logical(cr2fd)))
  grupo<-c(rep(1,length(cr1fo)),rep(2,length(cr2fo)))
  
  kk<-survdiff(Surv(tiempof,censuraf)~grupo,rho=0)
disiF4lg[i,j]<-kk$chi
disiF4lg[j,i]<-kk$chi


    
   
 
  #hace (i,i), aunque ya se sabe que da cero

  save(disiF4lg,file="disiF4lg.bin")
  save(disiG4lg,file="disiG4lg.bin")
  }
  }
