From 2ed6e4ffe11fa52dc8a1f88a5eae78b5067629dd Mon Sep 17 00:00:00 2001
From: Maria Voigt <maria.voigt@idiv.de>
Date: Wed, 8 Feb 2017 10:17:57 +0100
Subject: [PATCH] adding an option to exclude aerial transects

when the offset is defined, only if include_aerial = T, then,
the aerial transects are joined together with other transects
---
 src/model_fitting/abundance_model.R | 35 ++++++++++++++++-------------
 1 file changed, 20 insertions(+), 15 deletions(-)

diff --git a/src/model_fitting/abundance_model.R b/src/model_fitting/abundance_model.R
index 2a5ec31..ca7423f 100644
--- a/src/model_fitting/abundance_model.R
+++ b/src/model_fitting/abundance_model.R
@@ -234,8 +234,21 @@ predictors_obs$y_center <- as.numeric(as.vector(scale(predictors_obs[ ,
                                                                       "unscaled_y_center"])))
 
 
-# work on aerial transects
+#  ou density and offset term for ground and absence transects
+other_predictors_obs <- filter(predictors_obs, group != "aerial")
+# density
+other_predictors_obs$ou_dens <- (other_predictors_obs$nr_nests/
+                                   (other_predictors_obs$length_km * ESW * 2))  *
+  (1/(other_predictors_obs$nest_decay * NCS * PNB))
+# offset
+other_predictors_obs$offset_term <- log(other_predictors_obs$length_km * ESW *
+                                          2 * other_predictors_obs$nest_decay *
+                                          NCS * PNB)
 
+
+#  ou density and offset term for aerial transects
+
+if (include_aerial == T){
 aerial_predictors_obs <- dplyr::filter(predictors_obs, group == "aerial")
 # density
 aerial_predictors_obs$ou_dens <- (aerial_predictors_obs$nr_nests/
@@ -246,22 +259,14 @@ aerial_predictors_obs$offset_term <- log(aerial_predictors_obs$length_km * ESW_a
                                            2 * aerial_predictors_obs$nest_decay *
                                            NCS * PNB)
 
-# work on ground transects
-other_predictors_obs <- filter(predictors_obs, group != "aerial")
-# density
-other_predictors_obs$ou_dens <- (other_predictors_obs$nr_nests/
-                                   (other_predictors_obs$length_km * ESW * 2))  *
-  (1/(other_predictors_obs$nest_decay * NCS * PNB))
-# offset
-other_predictors_obs$offset_term <- log(other_predictors_obs$length_km * ESW *
-                                          2 * other_predictors_obs$nest_decay *
-                                          NCS * PNB)
-names_predictors_obs <- names(other_predictors_obs)
-
+predictors_obs <- aerial_predictors_obs %>%
+  bind_rows(other_predictors_obs)
+}else{
+  predictors_obs <- other_predictors_obs
+}
 
 # bind the two together
-predictors_obs <- aerial_predictors_obs %>%
-  bind_rows(other_predictors_obs) %>%
+predictors_obs <- predictors_obs %>%
   arrange(id) %>%
   dplyr::select(id, group, x_start:LU, length_km:nest_decay,
                 year, deforestation_gaveau:temp_mean, x_center, y_center,
-- 
GitLab