From 01c1291a244dc5de144040c2fa8c897f0fafab68 Mon Sep 17 00:00:00 2001
From: "claire.teillet_ird.fr" <claire.teillet@ird.fr>
Date: Wed, 19 Mar 2025 11:17:13 +0000
Subject: [PATCH] Upload sampling_biais_mtp

---
 sampling_biais_mtp.R | 76 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 76 insertions(+)
 create mode 100644 sampling_biais_mtp.R

diff --git a/sampling_biais_mtp.R b/sampling_biais_mtp.R
new file mode 100644
index 0000000..613e714
--- /dev/null
+++ b/sampling_biais_mtp.R
@@ -0,0 +1,76 @@
+# Script to calculate sampling distance with raster layers describing urban landscapes and presence data of larval presence in urban spaces
+# Author : Claire Teillet, Emmanuel Roux, Yi Moua
+# Example for Montpellier working but not for French Guiana : error with the end about one pixel in d_sampled...
+
+# Load package
+library("raster")
+library("FactoMineR")
+library(terra)
+library(ggplot2)
+library(dplyr)
+library(sf)
+
+# Set directory 
+# dir = "C:/Users/teill/Documents/0_PHD/MODELE/Git/correction_biais_echantillonnage-main/r_score"
+# setwd(dir)
+
+#### Load raster stack ####
+#####  for montpellier #####
+# env_raster_stack<- rast("C:/Users/teill/Documents/0_PHD/MODELE/Git/correction_biais_echantillonnage-main/data/tif/raster_stack_indices_spectraux_texturaux_res_202m_null.tif") 
+env_raster_stack<- rast("G:/0_PHD/RECHERCHE/AXE_2_BIS/data_montpellier/grid_MTP_complete_IS_IT.tif")
+
+#### Change env_raster_stack en data.frame ####
+
+env <- terra::as.data.frame(x=env_raster_stack, cells=TRUE) 
+
+#### Filtering and keep cell_num #### 
+ncolenv <- ncol(env)
+tmp=apply(env[,(2:ncolenv)],1, FUN=function(x){sum(is.na(x))}) # filters dataframe to keep only the rows without missing values
+cell_num <-env[tmp==0,1] # stores values from the first column of the valid rows in cell_num
+env<-env[tmp==0,(2:ncolenv)] # updates env by removing the rows containing NA and dropping the first column
+
+#### Load presence samples ####
+
+# presence_samples <- read.csv("../data/species/GU_potentiel_2154_clean.csv")
+presence_samples <- read.csv("C:/Users/teill/Documents/0_PHD/MODELE/Git/correction_biais_echantillonnage-main/data/species/GU_potentiel_2154_clean_filtering_by_type.csv")
+
+####  Extract environmental characteristics from Env_raster_stack associated with breeding sites.
+env_presence_samples=extract(x=env_raster_stack,y=presence_samples[,c(11,10)], ID=FALSE) # c(6,5) = attribut (long, lat)   c(11,10) = French guiana
+env_presence_samples=as.data.frame(env_presence_samples)
+tmp=apply(env_presence_samples, 1, FUN=function(x){sum(is.na(x))})
+env_presence_samples<-env_presence_samples[tmp==0,]
+
+number_of_sampling_mode=1
+NSampl=nrow(env_presence_samples)
+
+#### FAMD function #### 
+M=rbind(env,env_presence_samples)
+acp=PCA(X=M,ncp=100,ind.sup=((nrow(env)+1):(nrow(env)+number_of_sampling_mode*NSampl)),graph=FALSE) # FAMD <- PCA
+
+# environmental distance to sampled sites
+distance_env=function(ind=acp$ind$coord, ind.sup=acp$ind.sup$coord){ #famd>-pca
+  d=c()
+  for (p in (1:nrow(ind))){
+    d0=dist(rbind(ind[p,],ind.sup))[1:nrow(ind.sup)]
+    d=c(d,min(d0))
+    cat("\n",p/nrow(ind)*100)
+  }
+  d
+}
+
+# Function application
+d_sampled=distance_env(ind=acp$ind$coord, ind.sup=acp$ind.sup$coord)
+
+#### D_sampled to raster ####
+
+raster_d_sampled=env_raster_stack$NDVI_mean    # exemple of names of a column
+
+# Assign the good value to the right pixel
+# raster_d_sampled[cell_num]=d_sampled #cell_res est pour assigner la valeur au bon pixel!!! attention à cela
+values(raster_d_sampled)[cell_num] <- d_sampled   
+plot(raster_d_sampled) 
+
+#### Save results ####
+writeRaster(raster_d_sampled, filename = "G:/0_PHD/RECHERCHE/AXE_2_BIS/data_montpellier/d_sampled_raster_IS_IT_11_08.tif", overwrite = TRUE)
+
+
-- 
GitLab