#STEPS TO PREVENT PSEUDOREPLICATION library(dplyr) library(SDMTune) library(sp) library(rgdal) # FINDING CLUSTERS OF POINTS BASED ON A DISTANCE RULE # Having coordinate data of the observation points xy<-SpatialPointsDataFrame(coords = data [,2:3], # indicate the columns with information for longitude and latitude data=data.frame(id=data$id), #data to keep from the original dataframe proj4string=crs("+init=EPSG:32738") # Project the spatial object in the right SRC before calculating distances ) chc <- hclust(dist(data.frame(rownames=rownames(xy@data), x=coordinates(xy)[,1], y=coordinates(xy)[,2]))) # Distance with a 15km threshold chc<- cutree(chc, h=15000) # Join results to meuse sp points xy@data <- data.frame(xy@data, Clust=chc) puntos<-data.frame(xy@data)# get a data frame with coordinates and cluster identification ##FUNCTIONS TO ITERATE THE PROCESS OF RANDOM SELECTION OF POINTS WITHIN A CLUSTER AND ANALYSIS #preparation of different sets of combinations of points from each buffer #puntos is the data frame with the coordinates and cluster id #iter the number of combinations to be generated pasteiterations<-function (puntos,iter){ result<-puntos %>% group_by(Clust) %>% slice_sample(n=1) itera<-2 repeat { res<-puntos %>% group_by(Clust) %>% slice_sample(n=1) result<-rbind(result,res) itera<-itera+1 if (itera==iter+1) break } result$iteration<-rep(seq(1,iter),each=83) result } #RUNNING MAXENT #presences come from the data frame of random combinations #backgrounds is the background data used for MaxEnt #rasters are the layers for MaxEnt #iter the number of iterations = number of columns with random combinations generated in the previous step multianalysis<-function (presences,backgrounds,rasters,iter){ results<-list() itera<-1 repeat { pres<-filter(presences,iteration==itera) data_T <- prepareSWD(species = "Bushpig", p = pres[,2:3], a = backgrounds, env = rasters) folds <- randomFolds(data_T, k = 5, only_presence = TRUE, seed = 50) Fmodel<- train("Maxent", data = data_T, folds = folds, fc="lq",reg=0.4) predictions<-predict(Fmodel, data = rasters, type = "cloglog") results[itera]<-predictions itera<-itera+1 if (itera==iter+1) break } results }