Last updated: 2021-05-19

Checks: 7 0

Knit directory: booksn_ppm/

This reproducible R Markdown analysis was created with workflowr (version 1.6.2). 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(20210517) 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 09971e1. 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/
    Ignored:    data/.DS_Store
    Ignored:    data/data_raw/

Untracked files:
    Untracked:  glmulti.analysis.modgen.back
    Untracked:  glmulti.analysis.mods.back

Unstaged changes:
    Modified:   analysis/computeDisparity.Rmd
    Modified:   analysis/computeMannKendall.Rmd
    Modified:   analysis/evolucionTemporal.Rmd
    Modified:   data/coplas2019sn.csv
    Modified:   output/comparaD_especies.pdf

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/dataNAO.Rmd) and HTML (docs/dataNAO.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
html 36345c3 Antonio J Perez-Luque 2021-05-19 Build site.
Rmd dbcdcde Antonio J Perez-Luque 2021-05-19 add analysis NAO

library("tidyverse")
library("here")
library("flextable")
library("DT")
library("ggpubr")

Objetivo

El objetivo es generar un dataset con los datos de la NAO y los datos de infestación. - Utilizmaos el índice NAO de invierno (Hurrell’s winter NAO) cuyos datos podemos obtener en el National Center for Atmospheric Research. Descargamos los datos y los guardamos en data/nao_station_djfm.txt

  • Genero el dataset de NAO (con lag 1, 2, y 3) desde 1989 en adelante
nao <- read_csv(here::here("data/nao_station_djfm.csv"))

nao <- nao %>% filter(year > 1988) %>% 
  mutate(nao1 = lag(nao, n=1),
         nao2 = lag(nao, n=2),
         nao3 = lag(nao, n=3))
  • Siguiendo la aproximación de Hódar, Zamora, and Cayuela (2012) vamos a calcular el porcentaje de parcleas con infestación >= 3 para cada año.

  • Leer los datos para toda Andalucía

  • Agrupar en high infestacion (infestacion >= 3) y low infestacion (infestacion < 3)

  • Computar valor para cada año

  • Filtramos para las especies: pinea, pinaster, halepensis, sylvetris, nigra

coplas2019 <- read_csv(here::here("data/coplas2019.csv")) 

df <- coplas2019 %>% 
  filter(!is.na(sp)) %>% 
  dplyr::select(code, especie, `1993`:`2019`) %>% 
  pivot_longer(`1993`:`2019`, names_to = "year", values_to = "infesta") %>% 
  mutate(type = case_when(
    infesta >= 3 ~ "high",
    TRUE ~ "low" 
  )) %>% 
  mutate(year = as.numeric(year))


genera.dfnao <- function(x) {
  
  n_teorico <- x %>% group_by(year) %>% summarise(ntotal = length(infesta))
  n_real <- x %>% 
    filter(!is.na(infesta)) %>% 
    group_by(year) %>% 
    summarise(nreal = length(infesta)) %>% 
    mutate(year = as.numeric(year))  
  
  ppm <- x %>% 
    filter(!is.na(infesta)) %>% 
    group_by(year, type) %>% 
    summarise(n = n()) %>% 
    group_by(year) %>% 
    mutate(pct = n /sum(n)*100)
  
  ppm_year <- ppm %>% 
    pivot_wider(names_from = type,
                values_from = c(n, pct))
  
  out <- ppm_year %>% 
    left_join(n_real) %>% 
    left_join(n_teorico)
  
  return(out)
}
  
  
nao_all  <- genera.dfnao(df) %>% 
  mutate(especie = "All")

nao_halepensis <- df %>% 
  filter(especie == "P. halepensis") %>% 
  genera.dfnao() %>% 
  mutate(especie = "P. halepensis")

nao_pinea <- df %>% 
  filter(especie == "P. pinea") %>% 
  genera.dfnao() %>% 
  mutate(especie = "P. pinea")

nao_pinaster <- df %>% 
  filter(especie == "P. pinaster") %>% 
  genera.dfnao() %>% 
  mutate(especie = "P. pinaster")

nao_nigra <- df %>% 
  filter(especie == "P. nigra") %>% 
  genera.dfnao() %>% 
  mutate(especie = "P. nigra")

nao_sylvestris <- df %>% 
  filter(especie == "P. sylvestris") %>% 
  genera.dfnao() %>% 
  mutate(especie = "P. sylvestris")


dfnaos <- bind_rows(nao_all, nao_halepensis, nao_nigra, 
                    nao_pinaster, nao_pinea, nao_sylvestris)


nao_ppm <- nao %>% left_join(dfnaos)
  • Repetir figura 1 del trabajo de Hódar, Zamora, and Cayuela (2012), ampliando la serie hasta 2019
df_plot <- nao_ppm %>%  
  filter(!is.na(especie)) %>% 
  dplyr::select(year, nao:nao3, pct_high, especie) %>% 
  pivot_longer(nao:nao3, names_to = "nao_index") 

pearson_plot <- df_plot %>% 
  ggplot(aes(x=value, y=pct_high)) + 
  geom_point() + 
  geom_smooth(method = "lm", se=FALSE, size=.4, color="black") +
  theme_bw() +
  ylim(c(0,40)) + 
  xlab("") + 
  ylab("Percent of stands with defoliation >= 3") + 
  theme(panel.grid = element_blank(),
        strip.background = element_blank()) +
  stat_cor(aes(label = ..r.label..), color = "black", geom = "text") + facet_grid(fct_relevel(especie,"All", "P. pinea", 
         "P. pinaster", "P. halepensis",
         "P. nigra", "P. sylvestris")~nao_index, 
         labeller = labeller(nao_index = toupper))

Version Author Date
36345c3 Antonio J Perez-Luque 2021-05-19
null device 
          1 

Explorar patron temporal

df_plot_ts <- nao_ppm %>% 
  dplyr::select(year, nao, pct_high, especie) 

ylim.prim <- c(0,40)
ylim.sec <- c(-5,5)
b <- diff(ylim.prim)/diff(ylim.sec)
a <-  ylim.prim[1] - b*ylim.sec[1]


patron_nao_ppm <- df_plot_ts %>%  
  filter(!is.na(especie)) %>% 
  ggplot(aes(x=year, y=pct_high)) + 
  geom_hline(yintercept = a, color="gray") + 
  geom_point() + 
  facet_wrap(~especie) + 
  geom_line(stat="identity") + 
  geom_line(data = (
    df_plot_ts %>% select(year, nao)
  ), aes(x=year, y= a + nao*b), color="red") +
    scale_y_continuous(sec.axis = sec_axis(name="NAO", ~(.-a)/b)) + 

  scale_x_continuous(limits=c(1989,2019),
                     breaks = seq(1989,2019, by=1)) +
    theme_bw() + 
  theme(panel.grid = element_blank(), 
        axis.text.x = element_text(size=8, angle=90, vjust = 0.5),
        axis.title.y.right = element_text(color = "red"),
        strip.background = element_blank()) +
   xlab("") + 
  ylab("Percent of stands with defoliation >= 3") 

Version Author Date
36345c3 Antonio J Perez-Luque 2021-05-19
null device 
          1 

Referencias

Hódar, José A., Regino Zamora, and Luis Cayuela. 2012. “Climate Change and the Incidence of a Forest Pest in Mediterranean Ecosystems: Can the North Atlantic Oscillation Be Used as a Predictor?” Climatic Change 113 (3-4): 699–711. https://doi.org/10.1007/s10584-011-0371-7.

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] ggpubr_0.4.0    DT_0.17         flextable_0.6.3 here_1.0.1     
 [5] forcats_0.5.1   stringr_1.4.0   dplyr_1.0.4     purrr_0.3.4    
 [9] readr_1.4.0     tidyr_1.1.2     tibble_3.0.6    ggplot2_3.3.3  
[13] tidyverse_1.3.0 workflowr_1.6.2

loaded via a namespace (and not attached):
 [1] nlme_3.1-152      fs_1.5.0          lubridate_1.7.10  httr_1.4.2       
 [5] rprojroot_2.0.2   tools_4.0.2       backports_1.2.1   bslib_0.2.4      
 [9] R6_2.5.0          DBI_1.1.1         mgcv_1.8-33       colorspace_2.0-0 
[13] withr_2.4.1       tidyselect_1.1.0  curl_4.3          compiler_4.0.2   
[17] git2r_0.28.0      cli_2.3.0         rvest_0.3.6       xml2_1.3.2       
[21] officer_0.3.16    labeling_0.4.2    sass_0.3.1        scales_1.1.1     
[25] systemfonts_1.0.0 digest_0.6.27     foreign_0.8-81    rmarkdown_2.6.6  
[29] rio_0.5.16        base64enc_0.1-3   pkgconfig_2.0.3   htmltools_0.5.1.1
[33] highr_0.8         dbplyr_2.1.0      htmlwidgets_1.5.3 rlang_0.4.10     
[37] readxl_1.3.1      rstudioapi_0.13   farver_2.0.3      jquerylib_0.1.3  
[41] generics_0.1.0    jsonlite_1.7.2    zip_2.1.1         car_3.0-10       
[45] magrittr_2.0.1    Matrix_1.3-2      Rcpp_1.0.6        munsell_0.5.0    
[49] abind_1.4-5       gdtools_0.2.3     lifecycle_1.0.0   stringi_1.5.3    
[53] whisker_0.4       yaml_2.2.1        carData_3.0-4     grid_4.0.2       
[57] promises_1.2.0.1  crayon_1.4.1      lattice_0.20-41   haven_2.3.1      
[61] splines_4.0.2     hms_1.0.0         knitr_1.31        pillar_1.4.7     
[65] uuid_0.1-4        ggsignif_0.6.0    reprex_1.0.0      glue_1.4.2       
[69] evaluate_0.14     data.table_1.13.6 modelr_0.1.8      vctrs_0.3.6      
[73] httpuv_1.5.5      cellranger_1.1.0  gtable_0.3.0      assertthat_0.2.1 
[77] xfun_0.20         openxlsx_4.2.3    broom_0.7.4       rstatix_0.6.0    
[81] later_1.1.0.1     ellipsis_0.3.1