Last updated: 2022-01-18

Checks: 7 0

Knit directory: ms_mariposas_biodiversity/

This reproducible R Markdown analysis was created with workflowr (version 1.7.0). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20211228) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version 0050d21. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/

Unstaged changes:
    Modified:   analysis/index.Rmd
    Deleted:    analysis/prepara_datos_ambientales.Rmd

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/prepara_matrix_ambiental.Rmd) and HTML (docs/prepara_matrix_ambiental.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd 0050d21 ajpelu 2022-01-18 procesa var ambientales
Rmd a6eeff7 ajpelu 2022-01-18 update

Introduction

library(tidyverse)
library(readxl)
library(janitor)
library(here)
library(corrplot)
library(corrr)
library(factoextra)
library(kableExtra)
  • Prepare data
rawdata <- readxl::read_excel(here::here("data/Tabla_variables_2012_2020.xls")) %>% dplyr::select(-contains("LS_B"), 
                -contains("MT_"), 
                -LS_NDVI, 
                -FR_OLIVO, -FR_EUCAL, 
                -FR_URBAN, -FR_AGUA,
                -FR_VIASC, -FR_CULTI,
                -DS_VIASC, -DS_URBAN, 
                -DS_LAGIN, -DS_AGUA, 
                -DS_RIOS, -DS_CULTI) %>% 
  rename(Pp_oto = "Precipitación Otoños",
         Pp_inv = "Precipitación Inviernos",
         PP_pri = "Precipitación Primaveras",
         Pp_ver = "Precipitación Veranos",
         tmed_oto = "Temperatura media Otoños",
         tmed_inv = "Temperatura media Inviernos",
         tmed_pri = "Temperatura media Primaveras",
         tmed_ver = "Temperatura media Veranos") 


# Mean of Annual precipitation
pp_anu_year <- rawdata %>% 
  dplyr::select(Abreviatura, contains("Acumulado ANUAL")) %>% 
  rowwise() %>% 
  mutate(m = mean(c_across(where(is.numeric)))) %>% 
  dplyr::select(Abreviatura, Pp_anu = m)

# Average of Mean temperature
temp_anu_year <- rawdata %>% 
  dplyr::select(Abreviatura, contains("Temperatura media")) %>% 
  rowwise() %>% 
  mutate(m = mean(c_across(where(is.numeric)))) %>% 
  dplyr::select(Abreviatura, temp_anu = m)


env <- rawdata %>% 
  inner_join(temp_anu_year) %>% 
  inner_join(pp_anu_year) %>% 
  dplyr::select(-contains("Temperatura media")) %>% 
  dplyr::select(-contains("Acumulado ANUAL")) %>% 
  rename(elev = "Elevación")

Correlation bewteen variables

# Escalamos las variables 
env_selecccion <- env %>% 
  dplyr::select(-Transecto, -Id_transect, -Abreviatura, -Longitud)

env.scale <- scale(env_selecccion)

# correlaciones 
env.correlations <- cor(env.scale) # res
symnum(env.correlations , abbr.colnames = FALSE) %>% kbl()
elev TP_ES_OE TP_EXPO TP_PEND TP_RSD_A TP_RSD_I TP_RSD_O TP_RSD_P TP_RSD_V TP_RSH_I TP_RSH_P TP_RSH_V TP_SU_NO HIDRO_ACUM HIDRO_ICT HIDRO_ITH DS_ARBOL FR_ARBOL FR_CONIF FR_MATDE FR_MATDI FR_PASTO FR_QUERC Pp_oto Pp_inv PP_pri Pp_ver tmed_oto tmed_inv tmed_pri tmed_ver temp_anu Pp_anu
elev 1
TP_ES_OE . 1
TP_EXPO , . 1
TP_PEND . 1
TP_RSD_A . . , . 1
TP_RSD_I . . .
1
TP_RSD_O .
. .
, 1
TP_RSD_P . . . .
B , 1
TP_RSD_V . . , ,
, ,
1
TP_RSH_I , ,
,
1
TP_RSH_P . , , , , . ,
1
TP_RSH_V . , . . , 1
TP_SU_NO . .
B .
, , , 1
HIDRO_ACUM . . . . 1
HIDRO_ICT , . , . . 1
HIDRO_ITH , . , .
1
DS_ARBOL , . . . . . . . 1
FR_ARBOL . . . . . . , . . . . . 1
FR_CONIF . . . 1
FR_MATDE . . . . . . . . 1
FR_MATDI . . . . , . 1
FR_PASTO , . . .
. . . , 1
FR_QUERC . , . . . . . , . . . . . . . 1
Pp_oto . . . . . . . . . . . 1
Pp_inv . . , 1
PP_pri . . . . . . . . . , 1
Pp_ver . . . . . . . , . . . . , . . . . . 1
tmed_oto B . . . . . . . . , , . . , . . 1
tmed_inv B . . . . . . . . , , . . , . . B 1
tmed_pri B . . . . . . . . , , . . , . . B B 1
tmed_ver B . . . . . . . . , , . , . . B B B 1
temp_anu B . . . . . . . . , , . . , . . B B B B 1
Pp_anu . . . .
B , . . . 1
dfdis <- dist(env.correlations, method = "euclidean")
  • Generamos un plot de correlaciones
cor.mtest <- function(mat, ...) {
    mat <- as.matrix(mat)
    n <- ncol(mat)
    p.mat<- matrix(NA, n, n)
    diag(p.mat) <- 0
    for (i in 1:(n - 1)) {
        for (j in (i + 1):n) {
            tmp <- cor.test(mat[, i], mat[, j], ...)
            p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
        }
    }
  colnames(p.mat) <- rownames(p.mat) <- colnames(mat)
  p.mat
}

p.mat <- cor.mtest(env.scale)

corrplot::corrplot(env.correlations, type = "upper", tl.col = "black", tl.srt = 90, tl.cex = 0.4,
         p.mat = p.mat, sig.level = 0.05, 
         insig = "blank", 
         diag = FALSE)

pdf(here::here("figs/correlation_env_variables_numbers.pdf"))
corrplot::corrplot(env.correlations, method = "number",
                   type = "upper", tl.col = "black", 
                   col= "black", tl.srt = 90, tl.cex = 0.35, 
                   number.cex = .3, number.digits = 2,
         diag = FALSE)
dev.off()
quartz_off_screen 
                2 
pdf(here::here("figs/correlation_env_variables.pdf"))
corrplot::corrplot(env.correlations, type = "upper",
                   tl.col = "black", tl.srt = 90, tl.cex = 0.4,
         p.mat = p.mat, sig.level = 0.05, 
         insig = "blank", 
         diag = FALSE)
dev.off()
quartz_off_screen 
                2 
  • Table of correlations
dfscale_corrr <- correlate(env.scale)
dfscale_corrr %>% shave(upper = FALSE) %>%
  fashion() %>% kbl()
term elev TP_ES_OE TP_EXPO TP_PEND TP_RSD_A TP_RSD_I TP_RSD_O TP_RSD_P TP_RSD_V TP_RSH_I TP_RSH_P TP_RSH_V TP_SU_NO HIDRO_ACUM HIDRO_ICT HIDRO_ITH DS_ARBOL FR_ARBOL FR_CONIF FR_MATDE FR_MATDI FR_PASTO FR_QUERC Pp_oto Pp_inv PP_pri Pp_ver tmed_oto tmed_inv tmed_pri tmed_ver temp_anu Pp_anu
elev .42 .61 .34 .45 .37 .44 .41 .51 .25 .24 -.20 -.28 -.46 -.69 -.66 .60 -.38 .02 -.10 -.01 .70 -.36 .32 .16 -.07 .17 -.99 -.99 -.99 -.98 -.99 .20
TP_ES_OE .37 -.08 .58 .29 .82 .32 .40 .28 .32 .01 -.20 -.04 -.30 -.18 .26 -.45 -.11 .23 .03 .23 -.19 -.14 .04 -.16 .03 -.41 -.42 -.43 -.42 -.42 -.09
TP_EXPO -.23 .63 .54 .50 .59 .74 .62 .71 .39 -.45 -.30 -.64 -.37 .35 -.51 .04 -.29 .31 .33 -.61 .09 .10 .11 .37 -.57 -.58 -.56 -.53 -.56 .15
TP_PEND -.52 -.44 -.38 -.49 -.60 -.74 -.68 -.67 .38 -.40 -.44 -.72 .03 .39 -.08 .30 .02 .17 .44 .47 .41 .37 -.58 -.43 -.41 -.41 -.45 -.42 .44
TP_RSD_A .91 .86 .94 .86 .88 .74 .20 -.84 -.03 -.23 .04 .37 -.57 .04 -.21 .10 .26 -.45 -.15 -.19 -.35 .56 -.37 -.39 -.39 -.36 -.38 -.20
TP_RSD_I .60 .99 .75 .88 .69 .12 -.97 -.04 -.17 .04 .35 -.36 .04 -.32 .05 .21 -.31 .02 -.18 -.32 .56 -.27 -.30 -.30 -.26 -.29 -.12
TP_RSD_O .65 .66 .60 .51 .07 -.51 .01 -.23 .02 .36 -.68 -.05 .01 .09 .27 -.41 -.32 -.14 -.28 .43 -.40 -.42 -.42 -.39 -.41 -.24
TP_RSD_P .83 .92 .74 .19 -.95 -.05 -.19 .04 .36 -.41 .05 -.32 .06 .24 -.39 .00 -.19 -.35 .58 -.31 -.34 -.34 -.30 -.32 -.13
TP_RSD_V .91 .85 .51 -.63 -.06 -.23 .02 .35 -.55 .17 -.30 .09 .32 -.67 -.09 -.21 -.36 .53 -.42 -.44 -.43 -.40 -.42 -.19
TP_RSH_I .86 .46 -.79 .07 -.03 .24 .30 -.48 .08 -.39 .02 .19 -.54 -.11 -.28 -.40 .60 -.14 -.17 -.17 -.12 -.15 -.24
TP_RSH_P .70 -.63 -.06 -.18 .11 .09 -.34 .26 -.40 .19 .05 -.56 -.13 -.23 -.26 .47 -.14 -.17 -.15 -.12 -.15 -.20
TP_RSH_V -.06 .31 .10 .25 -.26 -.09 .36 -.31 .32 -.28 -.47 -.28 -.19 -.04 .18 .24 .23 .26 .27 .25 -.19
TP_SU_NO .09 .17 -.02 -.21 .21 -.09 .30 -.09 -.09 .14 -.03 .20 .32 -.53 .18 .21 .21 .18 .20 .13
HIDRO_ACUM .60 .50 .04 -.20 -.24 -.00 -.15 -.18 .09 -.36 -.21 -.22 .29 .48 .47 .47 .47 .47 -.28
HIDRO_ICT .90 -.19 .16 -.04 .07 -.35 -.30 .24 -.30 -.21 -.28 .11 .70 .70 .68 .68 .69 -.30
HIDRO_ITH -.12 -.05 -.03 -.10 -.31 -.25 .00 -.42 -.27 -.30 .35 .69 .69 .68 .69 .69 -.35
DS_ARBOL -.58 -.53 -.40 -.61 .92 -.37 .32 .15 -.15 .53 -.58 -.59 -.59 -.58 -.59 .21
FR_ARBOL .37 .07 .09 -.48 .54 .39 .27 .28 -.62 .32 .34 .33 .29 .32 .31
FR_CONIF -.11 .58 -.44 -.18 -.36 -.34 -.10 -.24 .01 .00 .01 .02 .01 -.36
FR_MATDE .25 -.38 .48 -.12 .07 -.04 -.46 .06 .07 .05 .03 .05 -.08
FR_MATDI -.61 -.13 -.36 -.04 .33 -.31 -.01 -.00 .03 .02 .01 -.09
FR_PASTO -.32 .42 .21 -.11 .37 -.69 -.69 -.70 -.69 -.70 .27
FR_QUERC .22 .09 -.07 -.36 .34 .35 .32 .29 .33 .07
Pp_oto .71 .37 -.24 -.38 -.36 -.38 -.40 -.38 .83
Pp_inv .79 -.30 -.29 -.26 -.26 -.31 -.28 .97
PP_pri -.38 -.07 -.04 -.01 -.05 -.04 .79
Pp_ver -.07 -.09 -.09 -.06 -.08 -.25
tmed_oto 1.00 1.00 1.00 1.00 -.32
tmed_inv 1.00 1.00 1.00 -.29
tmed_pri 1.00 1.00 -.28
tmed_ver 1.00 -.32
temp_anu -.30
Pp_anu
  • Hierarchical cluster
# library("pvclust")
# fit <- hclust(dfdis, method="ward")
# plot(fit, cex = .6)
# 33 Variables 
# Vamos a probar a reducir a 10
h <- hcut(dfdis, k=14)
dendro <- fviz_dend(h, rect = TRUE, horiz = TRUE, lwd = 0.5, cex = .5)
Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
"none")` instead.
dendro

ggsave(here::here("figs/dendrograma_env.pdf"), 
       height = 7, width = 5, device = "pdf")

Selección final de variables

variables_sel <- c("Pp_anu", "TP_PEND", "FR_MATDE", 
             "TP_SU_NO", "FR_QUERC", "FR_CONIF", 
             "TP_RSH_V", "HIDRO_ITH", "temp_anu",
             "TP_RSD_P", "Pp_ver", "TP_ES_OE", "TP_EXPO",
             "DS_ARBOL", "elev")

env_seleccionadas <- env %>% dplyr::select(Transecto,Id_transect,Abreviatura, matches(variables_sel)) 


write_csv(env_seleccionadas, here::here("data/matrix_env_variables_selected.csv"))

sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.3

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] kableExtra_1.3.1 factoextra_1.0.7 corrr_0.4.3      corrplot_0.84   
 [5] here_1.0.1       janitor_2.1.0    readxl_1.3.1     forcats_0.5.1   
 [9] stringr_1.4.0    dplyr_1.0.6      purrr_0.3.4      readr_1.4.0     
[13] tidyr_1.1.3      tibble_3.1.2     ggplot2_3.3.5    tidyverse_1.3.1 
[17] workflowr_1.7.0 

loaded via a namespace (and not attached):
 [1] fs_1.5.0          lubridate_1.7.10  webshot_0.5.2     httr_1.4.2       
 [5] rprojroot_2.0.2   tools_4.0.2       backports_1.2.1   bslib_0.2.4      
 [9] utf8_1.1.4        R6_2.5.1          DBI_1.1.1         colorspace_2.0-2 
[13] withr_2.4.1       tidyselect_1.1.1  gridExtra_2.3     processx_3.5.1   
[17] curl_4.3          compiler_4.0.2    git2r_0.28.0      textshaping_0.3.2
[21] cli_2.5.0         rvest_1.0.0       xml2_1.3.2        labeling_0.4.2   
[25] sass_0.3.1        scales_1.1.1.9000 callr_3.7.0       systemfonts_1.0.0
[29] digest_0.6.27     foreign_0.8-81    rmarkdown_2.8     rio_0.5.16       
[33] pkgconfig_2.0.3   htmltools_0.5.2   dbplyr_2.1.1      fastmap_1.1.0    
[37] highr_0.8         rlang_0.4.12      rstudioapi_0.13   farver_2.1.0     
[41] jquerylib_0.1.3   generics_0.1.0    jsonlite_1.7.2    zip_2.1.1        
[45] car_3.0-10        dendextend_1.14.0 magrittr_2.0.1    Rcpp_1.0.7       
[49] munsell_0.5.0     fansi_0.4.2       abind_1.4-5       viridis_0.5.1    
[53] lifecycle_1.0.1   stringi_1.7.4     whisker_0.4       yaml_2.2.1       
[57] carData_3.0-4     snakecase_0.11.0  grid_4.0.2        promises_1.2.0.1 
[61] ggrepel_0.9.1     crayon_1.4.1      haven_2.3.1       hms_1.0.0        
[65] knitr_1.31        ps_1.5.0          pillar_1.6.1      ggpubr_0.4.0     
[69] ggsignif_0.6.0    reprex_2.0.0      glue_1.4.2        evaluate_0.14    
[73] getPass_0.2-2     data.table_1.14.0 modelr_0.1.8      vctrs_0.3.8      
[77] httpuv_1.5.5      cellranger_1.1.0  gtable_0.3.0      assertthat_0.2.1 
[81] openxlsx_4.2.3    xfun_0.23         broom_0.7.9       rstatix_0.6.0    
[85] later_1.1.0.1     ragg_1.1.1        viridisLite_0.4.0 cluster_2.1.0    
[89] ellipsis_0.3.2