R Markdown

library(readr)
Maille_lézard_habitats <- read_csv(
  "Maille_lézard_habitats.csv",
  na = "NA"
)
## Rows: 389 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (14): id, left, top, right, bottom, row_index, col_index, Présence, Terr...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Maille_lézard_habitats
## # A tibble: 389 × 14
##       id     left      top    right bottom row_index col_index Présence Terriers
##    <dbl>    <dbl>    <dbl>    <dbl>  <dbl>     <dbl>     <dbl>    <dbl>    <dbl>
##  1   227 -155020. 5443002. -154920. 5.44e6        72         2        0        0
##  2   228 -155020. 5442902. -154920. 5.44e6        73         2        0        0
##  3   229 -155020. 5442802. -154920. 5.44e6        74         2        0        0
##  4   230 -155020. 5442702. -154920. 5.44e6        75         2        0        0
##  5   296 -154920. 5443802. -154820. 5.44e6        64         3        0        0
##  6   297 -154920. 5443702. -154820. 5.44e6        65         3        0        0
##  7   298 -154920. 5443602. -154820. 5.44e6        66         3        0        0
##  8   299 -154920. 5443502. -154820. 5.44e6        67         3        0        0
##  9   300 -154920. 5443402. -154820. 5.44e6        68         3        0        0
## 10   301 -154920. 5443302. -154820. 5.44e6        69         3        0        0
## # ℹ 379 more rows
## # ℹ 5 more variables: Refuge_art <dbl>, Bosquet <dbl>, `% dune gri` <dbl>,
## #   distance <dbl>, distance_2 <dbl>
data <- Maille_lézard_habitats

library(readr)

table(data$Présence)
## 
##   0   1 
## 329  60
#Distance aux terriers
boxplot(distance ~ Présence,
        data = data)

#Distance refuges artificiels
boxplot(distance_2 ~ Présence,
        data = data)

#Distance dune grise
boxplot(`% dune gri` ~ Présence,
        data = data)

#Test
table(data$Présence,
      data$Terriers)
##    
##       0   1
##   0 313  16
##   1  44  16
chisq.test(data$Présence,
           data$Terriers)
## Warning in chisq.test(data$Présence, data$Terriers): L’approximation du Chi-2
## est peut-être incorrecte
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  data$Présence and data$Terriers
## X-squared = 29.131, df = 1, p-value = 6.763e-08

Les mailles comportant au moins un terrier présentent environ 4 fois plus de chances d’être occupées par le Lézard ocellé que les mailles sans terrier. La présence de terriers est fortement associée à la présence du Lézard ocellé (χ² = 29,13 ; ddl = 1 ; p < 0,001).

library(epitools)

oddsratio(table(data$Présence,
                data$Terriers))
## Warning in chisq.test(xx, correct = correction): L’approximation du Chi-2 est
## peut-être incorrecte
## $data
##        
##           0  1 Total
##   0     313 16   329
##   1      44 16    60
##   Total 357 32   389
## 
## $measure
##    odds ratio with 95% C.I.
##     estimate    lower    upper
##   0 1.000000       NA       NA
##   1 7.052621 3.262809 15.30129
## 
## $p.value
##    two-sided
##       midp.exact fisher.exact   chi.square
##   0           NA           NA           NA
##   1 1.558015e-06  1.37964e-06 1.578457e-08
## 
## $correction
## [1] FALSE
## 
## attr(,"method")
## [1] "median-unbiased estimate & mid-p exact CI"

Les chances d’observer le Lézard ocellé sont environ 7 à 8 fois plus élevées dans les mailles comportant un terrier.

chisq.test(data$Présence,
           data$Refuge_art)
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  data$Présence and data$Refuge_art
## X-squared = 24.583, df = 1, p-value = 7.118e-07

Significatif

oddsratio(table(data$Présence,
                data$Refuge_art))
## $data
##        
##           0  1 Total
##   0     302 27   329
##   1      41 19    60
##   Total 343 46   389
## 
## $measure
##    odds ratio with 95% C.I.
##     estimate    lower    upper
##   0 1.000000       NA       NA
##   1 5.157144 2.603401 10.11699
## 
## $p.value
##    two-sided
##       midp.exact fisher.exact   chi.square
##   0           NA           NA           NA
##   1 5.305534e-06 4.501672e-06 2.273345e-07
## 
## $correction
## [1] FALSE
## 
## attr(,"method")
## [1] "median-unbiased estimate & mid-p exact CI"

chance 5 fois plus élevé dans les refuges art

chisq.test(data$Présence,
           data$Bosquet) 
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  data$Présence and data$Bosquet
## X-squared = 3.4982, df = 1, p-value = 0.06144

Pas significatif

wilcox.test(distance ~ Présence,
            data = data)
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  distance by Présence
## W = 14074, p-value = 1.543e-07
## alternative hypothesis: true location shift is not equal to 0

Significatif

wilcox.test(distance_2 ~ Présence,
            data = data)
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  distance_2 by Présence
## W = 14780, p-value = 8.814e-10
## alternative hypothesis: true location shift is not equal to 0

Hautement significatif la distance au refuge

wilcox.test(data$Présence, data$`% dune gri`)
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  data$Présence and data$`% dune gri`
## W = 66230, p-value = 1.841e-05
## alternative hypothesis: true location shift is not equal to 0
#Sélectionner uniquement les variables explicatives
vars <- data[,c("Terriers",
                "Refuge_art",
                "% dune gri",
                "distance",
                "distance_2")]

cor(vars)
##                Terriers   Refuge_art  % dune gri    distance  distance_2
## Terriers    1.000000000  0.006256684  0.48000069 -0.32760009 -0.17625463
## Refuge_art  0.006256684  1.000000000  0.06593862  0.17935837 -0.38499838
## % dune gri  0.480000694  0.065938621  1.00000000 -0.32484180 -0.24843079
## distance   -0.327600093  0.179358373 -0.32484180  1.00000000  0.08129464
## distance_2 -0.176254627 -0.384998385 -0.24843079  0.08129464  1.00000000
#premier GLM
mod1 <- glm(
  Présence ~ Terriers +
    Refuge_art +
    `% dune gri` +
    distance +
    distance_2,
  family = binomial,
  data = data
)

summary(mod1)
## 
## Call:
## glm(formula = Présence ~ Terriers + Refuge_art + `% dune gri` + 
##     distance + distance_2, family = binomial, data = data)
## 
## Coefficients:
##                Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  -1.3475019  0.3394940  -3.969 7.21e-05 ***
## Terriers      1.0390727  0.4804886   2.163 0.030577 *  
## Refuge_art    1.4642441  0.4417001   3.315 0.000916 ***
## `% dune gri`  0.0127312  0.0077120   1.651 0.098772 .  
## distance     -0.0014157  0.0006035  -2.346 0.018996 *  
## distance_2   -0.0008391  0.0003683  -2.278 0.022727 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 334.54  on 388  degrees of freedom
## Residual deviance: 266.01  on 383  degrees of freedom
## AIC: 278.01
## 
## Number of Fisher Scoring iterations: 6
#Sélection de modèles

library(MuMIn)

options(na.action = "na.fail")

mods <- dredge(mod1)
## Fixed term is "(Intercept)"
mods
## Global model call: glm(formula = Présence ~ Terriers + Refuge_art + `% dune gri` + 
##     distance + distance_2, family = binomial, data = data)
## ---
## Model selection table 
##      (Int) `%dungri`       dst      dst_2 Rfg_art    Trr df   logLik  AICc
## 32 -1.3480   0.01273 -0.001416 -0.0008391  1.4640 1.0390  6 -133.005 278.2
## 31 -1.1470           -0.001629 -0.0009340  1.4650 1.3380  5 -134.349 278.9
## 16 -1.1540   0.01896 -0.001703 -0.0008722  1.4210         5 -135.306 280.8
## 28 -1.8930   0.01673 -0.001593             2.0190 1.1210  5 -136.109 282.4
## 30 -1.7800   0.01736           -0.0009029  1.1680 1.3190  5 -136.197 282.6
## 27 -1.7010           -0.001898             2.0890 1.5320  4 -138.386 284.9
## 12 -1.6920   0.02371 -0.001952             1.9960         4 -138.739 285.6
## 15 -0.7177           -0.002223 -0.0010440  1.3760         4 -138.823 285.8
## 29 -1.5880                     -0.0010410  1.1150 1.8060  4 -138.857 285.8
## 24 -0.9368   0.01270 -0.000974 -0.0013780         0.9431  5 -138.668 287.5
## 26 -2.4400   0.02199                       1.7540 1.4650  4 -139.892 287.9
## 14 -1.6220   0.02659           -0.0009810  1.0460         4 -140.072 288.2
## 23 -0.7328           -0.001197 -0.0014800         1.2320  4 -140.118 288.3
## 22 -1.3040   0.01610           -0.0013540         1.1600  4 -140.232 288.6
## 8  -0.7708   0.01798 -0.001264 -0.0013920                 4 -140.689 289.5
## 21 -1.1450                     -0.0014720         1.6160  3 -142.642 291.3
## 6  -1.2130   0.02420           -0.0013800                 3 -143.411 292.9
## 25 -2.3140                                 1.7950 2.1240  3 -144.087 294.2
## 7  -0.3592           -0.001804 -0.0015530                 3 -144.140 294.3
## 11 -1.2500           -0.002699             2.0420         3 -144.168 294.4
## 10 -2.3240   0.03299                       1.6780         3 -144.643 295.3
## 13 -1.1740                     -0.0012670  0.8559         3 -147.973 302.0
## 5  -0.8622                     -0.0015950                 2 -150.458 304.9
## 20 -1.6670   0.01952 -0.001216                    1.0090  4 -148.608 305.3
## 18 -2.1050   0.02329                              1.2880  3 -150.411 306.9
## 4  -1.4600   0.02478 -0.001659                            3 -150.931 307.9
## 19 -1.4090           -0.001636                    1.4440  3 -152.092 310.2
## 2  -2.0200   0.03237                                      2 -154.422 312.9
## 17 -1.9620                                        1.9620  2 -155.466 315.0
## 9  -1.9970                                 1.6450         2 -156.722 317.5
## 3  -0.9462           -0.002641                            2 -157.563 319.2
## 1  -1.7020                                                1 -167.269 336.5
##    delta weight
## 32  0.00  0.416
## 31  0.63  0.304
## 16  2.54  0.117
## 28  4.15  0.052
## 30  4.32  0.048
## 27  6.65  0.015
## 12  7.35  0.011
## 15  7.52  0.010
## 29  7.59  0.009
## 24  9.26  0.004
## 26  9.66  0.003
## 14 10.02  0.003
## 23 10.11  0.003
## 22 10.34  0.002
## 8  11.25  0.001
## 21 13.12  0.001
## 6  14.65  0.000
## 25 16.01  0.000
## 7  16.11  0.000
## 11 16.17  0.000
## 10 17.12  0.000
## 13 23.78  0.000
## 5  26.72  0.000
## 20 27.09  0.000
## 18 28.66  0.000
## 4  29.69  0.000
## 19 32.02  0.000
## 2  34.65  0.000
## 17 36.73  0.000
## 9  39.24  0.000
## 3  40.93  0.000
## 1  58.32  0.000
## Models ranked by AICc(x)
best <- get.models(mods,1)[[1]]

summary(best)
## 
## Call:
## glm(formula = Présence ~ `% dune gri` + distance + distance_2 + 
##     Refuge_art + Terriers + 1, family = binomial, data = data)
## 
## Coefficients:
##                Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  -1.3475019  0.3394940  -3.969 7.21e-05 ***
## `% dune gri`  0.0127312  0.0077120   1.651 0.098772 .  
## distance     -0.0014157  0.0006035  -2.346 0.018996 *  
## distance_2   -0.0008391  0.0003683  -2.278 0.022727 *  
## Refuge_art    1.4642441  0.4417001   3.315 0.000916 ***
## Terriers      1.0390727  0.4804886   2.163 0.030577 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 334.54  on 388  degrees of freedom
## Residual deviance: 266.01  on 383  degrees of freedom
## AIC: 278.01
## 
## Number of Fisher Scoring iterations: 6
#Vérification de la performance prédictive
library(pROC)
## Warning: le package 'pROC' a été compilé avec la version R 4.5.3
## Type 'citation("pROC")' for a citation.
## 
## Attachement du package : 'pROC'
## Les objets suivants sont masqués depuis 'package:stats':
## 
##     cov, smooth, var
pred <- predict(best,
                type = "response")

roc_obj <- roc(data$Présence,
               pred)
## Setting levels: control = 0, case = 1
## Setting direction: controls < cases
auc(roc_obj)
## Area under the curve: 0.8013
plot(roc_obj)

exp(coef(best))
##  (Intercept) `% dune gri`     distance   distance_2   Refuge_art     Terriers 
##    0.2598887    1.0128126    0.9985853    0.9991613    4.3242734    2.8265948
#Validation croisé
library(caret)
## Warning: le package 'caret' a été compilé avec la version R 4.5.3
## Le chargement a nécessité le package : ggplot2
## Le chargement a nécessité le package : lattice
ctrl <- trainControl(
  method = "cv",
  number = 10,
  classProbs = TRUE,
  summaryFunction = twoClassSummary)
#Vérification de la colinéarité

cor(data[,c("Terriers","distance")])
##            Terriers   distance
## Terriers  1.0000000 -0.3276001
## distance -0.3276001  1.0000000
library(car)
## Le chargement a nécessité le package : carData
vif(best)
## `% dune gri`     distance   distance_2   Refuge_art     Terriers 
##     1.376568     1.320044     1.306099     1.375633     1.358826
#Production de la carte de favorabilité des habitats

data$favorabilite <- predict(best,
                             type = "response")
summary(data$favorabilite)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## 0.02353 0.03719 0.10552 0.15424 0.15843 0.84559
hist(data$favorabilite,
     breaks = 20)

#Enregistre données
write.csv(data,
          "favorabilite_ocelle.csv",
          row.names = FALSE)

write.csv(
  data[,c("id","favorabilite")],
  "favorabilite.csv",
  row.names = FALSE
)
getwd()
## [1] "C:/Users/mathi/Downloads"