Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
manuscript_code
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Maria Voigt
manuscript_code
Commits
24d0cfe3
Commit
24d0cfe3
authored
Feb 13, 2017
by
Maria Voigt
Browse files
Options
Downloads
Patches
Plain Diff
editing scaling into function
parent
11f1afb3
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/prediction/abundance_prediction.R
+23
-50
23 additions, 50 deletions
src/prediction/abundance_prediction.R
with
23 additions
and
50 deletions
src/prediction/abundance_prediction.R
+
23
−
50
View file @
24d0cfe3
...
@@ -121,12 +121,17 @@ if (is.na(exclude_year)){
...
@@ -121,12 +121,17 @@ if (is.na(exclude_year)){
name_suffix
<-
paste0
(
exclude_year
,
"_"
)
name_suffix
<-
paste0
(
exclude_year
,
"_"
)
}
}
#----------------
---------------
#
#----------------#
# Load
and prepare coefficient
s #
# Load
function
s #
#----------------
---------------
#
#----------------#
# Load functions
# Load functions
source
(
file.path
(
indir_fun
,
"generic/path.to.current.R"
))
source
(
file.path
(
indir_fun
,
"generic/path.to.current.R"
))
source
(
file.path
(
indir_fun
,
"project_functions/scale.predictors.R"
))
#----------------------#
# Prepare coefficients #
#----------------------#
# Load coefficients and weights
# Load coefficients and weights
abundMod_results_path
<-
path.to.current
(
indir
,
"abundMod_results"
,
"rds"
)
abundMod_results_path
<-
path.to.current
(
indir
,
"abundMod_results"
,
"rds"
)
if
(
is_verbose
){
print
(
paste
(
"this is abundMod_results_path:"
,
abundMod_results_path
))}
if
(
is_verbose
){
print
(
paste
(
"this is abundMod_results_path:"
,
abundMod_results_path
))}
...
@@ -192,12 +197,7 @@ predictor_names_for_scaling <- c( "dem", "slope", "temp_mean", "rain_dry", "rain
...
@@ -192,12 +197,7 @@ predictor_names_for_scaling <- c( "dem", "slope", "temp_mean", "rain_dry", "rain
"deforestation_gaveau"
,
"plantation_distance"
,
"pulp_distance"
,
"palm_distance"
,
"deforestation_gaveau"
,
"plantation_distance"
,
"pulp_distance"
,
"palm_distance"
,
"dom_T_OC"
,
"dom_T_PH"
)
"dom_T_OC"
,
"dom_T_PH"
)
# predictors used in model
predictor_names_add
<-
c
(
"year"
,
"x_center"
,
"y_center"
)
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"
,
"perc_muslim"
)
predictors
<-
rename
(
predictors
,
unscaled_value
=
value
)
predictors
<-
rename
(
predictors
,
unscaled_value
=
value
)
...
@@ -207,52 +207,25 @@ geography$unscaled_y_center <- rowMeans(cbind(geography$y_start, geography$y_end
...
@@ -207,52 +207,25 @@ geography$unscaled_y_center <- rowMeans(cbind(geography$y_start, geography$y_end
for
(
predictor_name
in
predictor_names_for_scaling
){
# function here
mean_predictor_obs
<-
mean
(
predictors_obs
[
,
paste0
(
"unscaled_"
,
predictor_name
)],
na.rm
=
T
)
predictors_grid
<-
scale.predictors.grid
(
predictor_names_for_scaling
,
sd_predictor_obs
<-
mean
(
predictors_obs
[
,
paste0
(
"unscaled_"
,
predictor_name
)],
na.rm
=
T
)
predictor_names_add
,
predictors
[
predictors
$
predictor
==
predictor_name
,
predictors
,
"value"
]
<-
(
predictors
[
predictors
$
predictor
==
predictor_name
,
predictors_obs
,
"unscaled_value"
]
-
mean_predictor_obs
)
/
geography
)
sd_predictor_obs
}
# cast it to wide
predictors_grid
<-
dplyr
::
filter
(
predictors
,
predictor
%in%
predictor_names_for_scaling
)
%>%
dcast
(
id
+
year
~
predictor
,
value.var
=
"value"
)
%>%
dplyr
::
select
(
-
year
)
predictors_grid_unscaled
<-
dplyr
::
filter
(
predictors
,
predictor
%in%
predictor_names_for_scaling
)
%>%
saveRDS
(
predictors_grid
,
file.path
(
outdir
,
paste0
(
"predictors_grid_scaled_"
,
name_suffix
,
dcast
(
id
+
year
~
predictor
,
value.var
=
"unscaled_value"
)
%>%
year_to_predict
,
"_"
,
Sys.Date
(),
".rds"
)))
rename
(
unscaled_year
=
year
)
names
(
predictors_grid_unscaled
)[
-
c
(
1
,
2
)]
<-
paste0
(
"unscaled_"
,
names
(
predictors_grid_unscaled
)[
-
c
(
1
,
2
)])
# join with geography to have x and y-center
predictors_grid
<-
predictors_grid
%>%
left_join
(
predictors_grid_unscaled
,
by
=
"id"
)
%>%
left_join
(
geography
,
by
=
"id"
)
# year and x- and y-center
additional_predictors
<-
c
(
"year"
,
"x_center"
,
"y_center"
)
for
(
predictor_name
in
additional_predictors
){
mean_predictor_obs
<-
mean
(
predictors_obs
[
,
paste0
(
"unscaled_"
,
predictor_name
)],
na.rm
=
T
)
sd_predictor_obs
<-
mean
(
predictors_obs
[
,
paste0
(
"unscaled_"
,
predictor_name
)],
na.rm
=
T
)
predictors_grid
[
,
predictor_name
]
<-
(
predictors_grid
[
,
paste0
(
"unscaled_"
,
predictor_name
)
]
-
mean_predictor_obs
)
/
sd_predictor_obs
}
saveRDS
(
predictors_grid
,
file.path
(
outdir
,
paste0
(
"predictors_grid_pred_"
,
name_suffix
,
# predictors used in model
year_to_predict
,
"_"
,
Sys.Date
(),
".rds"
)))
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"
,
"perc_muslim"
)
#--------------------------#
#--------------------------#
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment