Last updated: 2021-07-15

Checks: 6 1

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.


The R Markdown file has unstaged changes. To know which version of the R Markdown file created these results, you’ll want to first commit it to the Git repo. If you’re still working on the analysis, you can ignore this warning. When you’re finished, you can run wflow_publish to commit the R Markdown file and build the HTML.

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 460935f. 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:  analysis/features_plots.Rmd
    Untracked:  analysis/findPlots.Rmd
    Untracked:  glmulti.analysis.modgen.back
    Untracked:  glmulti.analysis.mods.back

Unstaged changes:
    Modified:   analysis/dataNAO-SierraNevada.Rmd
    Modified:   analysis/evolucionTemporal.Rmd
    Modified:   data/coplas2019sn.csv

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-SierraNevada.Rmd) and HTML (docs/dataNAO-SierraNevada.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 0793eca Antonio J Perez-Luque 2021-05-27 Build site.
Rmd d5f92fb Antonio J Perez-Luque 2021-05-27 repeat NAO analysis for SN

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 Sierra Nevada

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

  • Computar valor para cada año

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

coplas2019 <- read_csv(here::here("data/coplas2019sn.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, na.rm = TRUE)*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_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_sylvestris) %>% 
  mutate(pct_high = replace_na(pct_high, 0))

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
0793eca Antonio J Perez-Luque 2021-05-27
null device 
          1 
pearson_plotr2 <- 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 = paste(..rr.label.., ..p.label.., sep = "~`,`~")), 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
0793eca Antonio J Perez-Luque 2021-05-27
null device 
          1 

Explorar patron temporal

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

df_plot_ts$especie <- factor(df_plot_ts$especie, 
                             levels = c("NA","All","P. sylvestris", "P. nigra",
                                         "P. pinaster","P. halepensis"))

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)) %>% 
  filter(especie != "All") %>%
  ggplot(aes(x=year, y=pct_high)) + 
  geom_hline(yintercept = a, color="gray") + 
  geom_point() + 
  facet_wrap(~especie, nrow=4) + 
  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") 

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.6     purrr_0.3.4    
 [9] readr_1.4.0     tidyr_1.1.3     tibble_3.1.2    ggplot2_3.3.3  
[13] tidyverse_1.3.1 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] utf8_1.1.4        R6_2.5.0          mgcv_1.8-33       DBI_1.1.1        
[13] colorspace_2.0-0  withr_2.4.1       tidyselect_1.1.0  curl_4.3         
[17] compiler_4.0.2    git2r_0.28.0      cli_2.5.0         rvest_1.0.0      
[21] xml2_1.3.2        officer_0.3.16    labeling_0.4.2    sass_0.3.1       
[25] scales_1.1.1      systemfonts_1.0.0 digest_0.6.27     foreign_0.8-81   
[29] rmarkdown_2.8     rio_0.5.16        base64enc_0.1-3   pkgconfig_2.0.3  
[33] htmltools_0.5.1.1 highr_0.8         dbplyr_2.1.1      htmlwidgets_1.5.3
[37] rlang_0.4.10      readxl_1.3.1      rstudioapi_0.13   farver_2.0.3     
[41] jquerylib_0.1.3   generics_0.1.0    jsonlite_1.7.2    zip_2.1.1        
[45] car_3.0-10        magrittr_2.0.1    Matrix_1.3-2      Rcpp_1.0.6       
[49] munsell_0.5.0     fansi_0.4.2       abind_1.4-5       gdtools_0.2.3    
[53] lifecycle_1.0.0   stringi_1.5.3     whisker_0.4       yaml_2.2.1       
[57] carData_3.0-4     grid_4.0.2        promises_1.2.0.1  crayon_1.4.1     
[61] lattice_0.20-41   splines_4.0.2     haven_2.3.1       hms_1.0.0        
[65] knitr_1.31        pillar_1.6.1      uuid_0.1-4        ggsignif_0.6.0   
[69] reprex_2.0.0      glue_1.4.2        evaluate_0.14     data.table_1.13.6
[73] modelr_0.1.8      vctrs_0.3.8       httpuv_1.5.5      cellranger_1.1.0 
[77] gtable_0.3.0      assertthat_0.2.1  xfun_0.23         openxlsx_4.2.3   
[81] broom_0.7.6       rstatix_0.6.0     later_1.1.0.1     ellipsis_0.3.2