DOI: 10.57745/i66yff
title: 1_Plot_pairs_selection-1.Rmd

description: R script for plot pairs selection.

To select our plot pairs based on five specific criteria, we developed an R script. The criteria were as follows: plots must be planted with the same grape variety (Criteria 1 - Same Variety); their geographic distance must not exceed 10 km (Criteria 2 - Closeness); the management type (organic vs. conventional viticulture) must be the same (Criteria 3 - Same Management); the untreated controls (UCs) within the two plots must have been monitored by IFV for at least 4 common years, including at least one recent year (2020 or 2021 for the 2022 sampling campaign, and 2021 or 2022 for the 2023 sampling campaign) (Criteria 4 - Common Records); and the epidemiological records must be contrasted (Criteria 5 - Contrasted Epidemiology). We first downloaded epidemiological data and metadata from https://www.vignevin-epicure.com/.
To meet criterion 5 and compare data over a common observation date, we used the observation unit in weeks rather than days. This approach was necessary because, although UCs are monitored weekly throughout the growing season, the actual observation days can vary between plots due to observer constraints and schedules.
We first transformed the coordinates (longitude and latitude) of the untreated controls into spatial data using the st_as_sf function from the sf v1.0.16 package [1, 2] and then set the coordinate reference system to WGS84 (EPSG:4326) using the st_set_crs function from the same package. 
Following steps were applied independently to the four monitoring variables: leaf symptoms frequency, leaf symptoms intensity, bunches symptoms frequency, and bunches symptoms intensity. Initially, the data were grouped by each combination of symptom observation week (with a maximum of one data per week) x symptom observation year. Geographic distances were calculated for each possible pair of UCs and each combination of week x year using the st_distance function from the sf package. Symptomatic distances were calculated using the outer function. The data were then ungrouped and regrouped by each combination of UCs pair x year of symptom observation, which allowed us to obtain the vector of symptom differences across the entire year for each pair (by calculating, for each combination of week x year x pair, the difference between the symptoms of UC 1 and UC 2). For each combination of UC pair x year of symptom observation, the area under the disease curve (AUDPC) of the vector of symptom differences for the entire year was calculated using the lintegrate function from the tis v1.39 package [3]. Only pairs for which the AUDPC remained either positive or negative for at least 80% of the common monitoring years were retained. From these, we further selected pairs where the UCs had been monitored for at least 4 common years, including the year 2020 or 2021. Finally, only pairs where the UCs had the same grape variety and were located less than 10 km apart were retained.
The UC pairs ultimately retained were those that met these conditions for the four symptom monitoring variables. This script was applied twice: first, for plots that were sampled in 2022, using epidemiological data collected between 2002 and 2021, then for plots sampled in 2023, using data collected between 2002 and 2022. After we obtained our potential plot pairs, we contacted the observers to 1) identify the management type, i.e. verify that it was common within the pair (either organic, conventional or biodynamic), 2) verify/complete the information provided by the Epicure database, such as the grape variety or the geographic coordinates of the plot and 3) we ensured that for each selected untreated controls, all symptom surveys had been carried out on the same plot (in some cases the UC changed plots each year without this being notified in the database). 
To draw the AUDPCs in Fig. 2A, and supplementary Fig. S6 and calculate the mean AUDPCs in Fig. 2B for each selected plot pair, we only kept the common monitoring weeks.

Bibliography
1. Pebesma E. Simple Features for R: Standardized Support for Spatial Vector Data. R J. 2018;10:439.
2. Pebesma E, Bivand R. Spatial Data Science: With Applications in R. New York: Chapman and Hall/CRC; 2023.
3. Hallman J. tis: Time Indexes and Time Indexed Series. 2021.