From d46119c0e8e463e3b7851be2d1195bed9cf86d55 Mon Sep 17 00:00:00 2001
From: Maria Voigt <maria.voigt@idiv.de>
Date: Mon, 6 Feb 2017 15:40:33 +0100
Subject: [PATCH] saving all predictors in predictors_obs in abundMod

so that they can all be used in prediction
also saving the data-file for prediction
---
 src/model_fitting/abundance_model.R | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)

diff --git a/src/model_fitting/abundance_model.R b/src/model_fitting/abundance_model.R
index 638f606..cb6854e 100644
--- a/src/model_fitting/abundance_model.R
+++ b/src/model_fitting/abundance_model.R
@@ -110,6 +110,11 @@ PNB <- 0.88  #  proportion of nest builders from Spehar et al. 2010
 options("scipen" = 100, "digits" = 4)
 
 
+if (is.na(exclude_year)){
+  name_suffix <- ""} else {
+    name_suffix <- paste0(exclude_year, "_")
+  }
+
 #---------------#
 #  Import data  #
 #---------------#
@@ -159,7 +164,7 @@ predictor_names_for_scaling <- c( "dem", "slope", "temp_mean", "rain_dry", "rain
                                   "deforestation_gaveau", "plantation_distance", "pulp_distance", "palm_distance",
                                   "dom_T_OC", "dom_T_PH")
 # predictors used in model
-predictor_names <-c("year", "temp_mean", "rain_var", "rain_dry", "dom_T_OC",
+predictor_names <- c("year", "temp_mean", "rain_var", "rain_dry", "dom_T_OC",
                     "peatswamp", "lowland_forest",
                     "lower_montane_forest", "deforestation_hansen",
                     "human_pop_dens", "ou_killing_prediction",
@@ -202,7 +207,7 @@ geography <- dplyr::select(geography, -c(year))
 
 # Rename here1
 predictors_obs <- predictors %>%
-  dplyr::filter(predictor %in% predictor_names)%>%
+  dplyr::filter(predictor %in% predictor_names_for_scaling)%>%
   dcast(id + year ~ predictor,  value.var = "value") %>%
   inner_join(geography, by = "id")%>%
   dplyr::select(-group) %>%
@@ -211,11 +216,12 @@ predictors_obs <- predictors %>%
 
 
 predictors_obs_unscaled <- predictors %>%
-  dplyr::filter(predictor %in% predictor_names)%>%
+  dplyr::filter(predictor %in% predictor_names_for_scaling)%>%
   dcast(id + unscaled_year ~ predictor,  value.var = "unscaled_value")
 
 
-names(predictors_obs_unscaled)[-c(1,2)] <- paste0("unscaled_", names(predictors_obs_unscaled)[-c(1,2)])
+names(predictors_obs_unscaled)[-c(1,2)] <- paste0("unscaled_",
+                                                  names(predictors_obs_unscaled)[-c(1,2)])
 
 predictors_obs <- left_join(predictors_obs, predictors_obs_unscaled, by = "id")
 
@@ -258,7 +264,7 @@ predictors_obs <- aerial_predictors_obs %>%
   bind_rows(other_predictors_obs) %>%
   arrange(id) %>%
   dplyr::select(id, group, x_start:LU, length_km:nest_decay,
-                year, deforestation_hansen:temp_mean, x_center, y_center,
+                year, deforestation_gaveau:temp_mean, x_center, y_center,
                 unscaled_year:unscaled_temp_mean, unscaled_x_center,
                 unscaled_y_center, ou_dens, offset_term) %>%
   as.data.frame(.)
@@ -268,6 +274,13 @@ if(is_verbose){print("look at predictors_obs")
   str(predictors_obs)
   summary(predictors_obs)}
 
+# save this
+# save the relevant output for the prediction and the validation
+saveRDS(predictors_obs, file = file.path(outdir, paste0("predictors_obs_",
+                                                          name_suffix,
+                                                          Sys.Date(), ".rds")))
+
+
 # now exclude the year that needs to be excluded
 if (!is.na(exclude_year)){
     predictors_excluded_year <- predictors_obs[predictors_obs$unscaled_year == exclude_year, ]
@@ -463,11 +476,6 @@ c_set$w.aic <- NULL
 results_out <- right_join(results_res, c_set,  by="AIC")
 results_out <- results_out[order(results_out$AIC), ]
 
-if (is.na(exclude_year)){
-  name_suffix <- ""} else {
-    name_suffix <- paste0(exclude_year, "_")
-  }
-
 # save the relevant output for the prediction and the validation
 saveRDS(abundMod_results, file = file.path(outdir, paste0("abundMod_results_",
                                                           name_suffix,
-- 
GitLab