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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Maria Voigt
manuscript_code
Commits
65b5379c
Commit
65b5379c
authored
8 years ago
by
Maria Voigt
Browse files
Options
Downloads
Patches
Plain Diff
changing random grid to random and simplifying the write in
parent
fb0d1b69
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/model_fitting/abundance_model.R
+34
-58
34 additions, 58 deletions
src/model_fitting/abundance_model.R
with
34 additions
and
58 deletions
src/model_fitting/abundance_model.R
+
34
−
58
View file @
65b5379c
...
@@ -43,17 +43,17 @@ option_list <- list (
...
@@ -43,17 +43,17 @@ option_list <- list (
default
=
NA
,
help
=
"year to exclude"
,
metavar
=
"2015"
),
default
=
NA
,
help
=
"year to exclude"
,
metavar
=
"2015"
),
make_option
(
"--exclude-grid"
,
dest
=
"exclude_grid"
,
type
=
"integer"
,
make_option
(
"--exclude-grid"
,
dest
=
"exclude_grid"
,
type
=
"integer"
,
default
=
NA
,
help
=
"index of grid-cells to exclude"
,
metavar
=
"1"
),
default
=
NA
,
help
=
"index of grid-cells to exclude"
,
metavar
=
"1"
),
make_option
(
"--exclude-
grid-
random"
,
dest
=
"exclude_
grid_
rand"
,
make_option
(
"--exclude-random"
,
dest
=
"exclude_rand"
,
type
=
"integer"
,
type
=
"integer"
,
default
=
NA
,
default
=
NA
,
help
=
"iteration of excluding the given percent randomly"
,
help
=
"iteration of excluding the given percent randomly"
,
metavar
=
"1"
),
metavar
=
"1"
),
make_option
(
"--exclude-
grid-
random-percent"
,
make_option
(
"--exclude-random-percent"
,
dest
=
"exclude_
grid_
rand_perc"
,
dest
=
"exclude_rand_perc"
,
type
=
"integer"
,
type
=
"integer"
,
default
=
NA
,
default
=
NA
,
help
=
"percent cells to be excluded"
,
help
=
"percent cells to be excluded"
,
metavar
=
"10"
),
metavar
=
"10"
),
make_option
(
c
(
"-q"
,
"--quiet"
),
dest
=
"verbose_script"
,
make_option
(
c
(
"-q"
,
"--quiet"
),
dest
=
"verbose_script"
,
action
=
"store_false"
,
action
=
"store_false"
,
default
=
TRUE
,
default
=
TRUE
,
...
@@ -81,7 +81,7 @@ if (!is.na(options$exclude_year) && !(options$exclude_year %in% exclude_year_pos
...
@@ -81,7 +81,7 @@ if (!is.na(options$exclude_year) && !(options$exclude_year %in% exclude_year_pos
if
(
!
is.na
(
options
$
exclude_
grid_
rand
)
&
is.na
(
options
$
exclude_
grid_
rand_perc
)){
if
(
!
is.na
(
options
$
exclude_rand
)
&
is.na
(
options
$
exclude_rand_perc
)){
stop
(
paste
(
"exclude-grid-random needs the percent cells that have to be excluded (exclude-grid-random-percent)"
))}
stop
(
paste
(
"exclude-grid-random needs the percent cells that have to be excluded (exclude-grid-random-percent)"
))}
...
@@ -112,11 +112,11 @@ if(is_verbose){print(paste("exclude year", exclude_year))}
...
@@ -112,11 +112,11 @@ if(is_verbose){print(paste("exclude year", exclude_year))}
exclude_grid
<-
options
$
exclude_grid
exclude_grid
<-
options
$
exclude_grid
if
(
is_verbose
){
print
(
paste
(
"exclude grid"
,
exclude_grid
))}
if
(
is_verbose
){
print
(
paste
(
"exclude grid"
,
exclude_grid
))}
exclude_
grid_
rand
<-
options
$
exclude_
grid_
rand
exclude_rand
<-
options
$
exclude_rand
if
(
is_verbose
){
print
(
paste
(
"exclude grid random"
,
exclude_
grid_
rand
))}
if
(
is_verbose
){
print
(
paste
(
"exclude grid random"
,
exclude_rand
))}
exclude_
grid_
rand_perc
<-
options
$
exclude_
grid_
rand_perc
exclude_rand_perc
<-
options
$
exclude_rand_perc
if
(
is_verbose
){
print
(
paste
(
"exclude_
grid_
rand_perc"
,
exclude_
grid_
rand_perc
))}
if
(
is_verbose
){
print
(
paste
(
"exclude_rand_perc"
,
exclude_rand_perc
))}
#---------#
#---------#
# Globals #
# Globals #
...
@@ -145,14 +145,14 @@ PNB <- 0.88 # proportion of nest builders from Spehar et al. 2010
...
@@ -145,14 +145,14 @@ PNB <- 0.88 # proportion of nest builders from Spehar et al. 2010
options
(
"scipen"
=
100
,
"digits"
=
4
)
options
(
"scipen"
=
100
,
"digits"
=
4
)
if
(
is.na
(
exclude_year
)
&
is.na
(
exclude_grid
)
&
is.na
(
exclude_
grid_
rand
)){
if
(
is.na
(
exclude_year
)
&
is.na
(
exclude_grid
)
&
is.na
(
exclude_rand
)){
name_suffix
<-
""
}
name_suffix
<-
""
}
if
(
!
is.na
(
exclude_year
)){
if
(
!
is.na
(
exclude_year
)){
name_suffix
<-
paste0
(
"year_"
,
exclude_year
,
"_"
)}
name_suffix
<-
paste0
(
"year_"
,
exclude_year
,
"_"
)}
if
(
!
is.na
(
exclude_grid
)){
if
(
!
is.na
(
exclude_grid
)){
name_suffix
<-
paste0
(
"gridcell_"
,
exclude_grid
,
"_"
)}
name_suffix
<-
paste0
(
"gridcell_"
,
exclude_grid
,
"_"
)}
if
(
!
is.na
(
exclude_
grid_
rand
)){
if
(
!
is.na
(
exclude_rand
)){
name_suffix
<-
paste0
(
"rand_"
,
exclude_
grid_
rand
,
"_"
)}
name_suffix
<-
paste0
(
"rand_"
,
exclude_rand
,
"_"
)}
#---------------#
#---------------#
# Import data #
# Import data #
...
@@ -301,30 +301,29 @@ saveRDS(predictors_obs, file = file.path(outdir, paste0("predictors_observation_
...
@@ -301,30 +301,29 @@ saveRDS(predictors_obs, file = file.path(outdir, paste0("predictors_observation_
# now exclude the year that needs to be excluded
# now exclude the year that needs to be excluded
if
(
!
is.na
(
exclude_year
)){
if
(
!
is.na
(
exclude_year
)){
predictors_excluded
_year
<-
predictors_obs
[
predictors_obs
$
unscaled_year
==
exclude_year
,
]
predictors_excluded
<-
predictors_obs
[
predictors_obs
$
unscaled_year
==
exclude_year
,
]
predictors_obs
<-
predictors_obs
[
predictors_obs
$
unscaled_year
!=
exclude_year
,
]
predictors_obs
<-
predictors_obs
[
predictors_obs
$
unscaled_year
!=
exclude_year
,
]
nr_excluded
<-
nrow
(
predictors_excluded
_year
)}
nr_excluded
<-
nrow
(
predictors_excluded
)}
# or the grid_cell
# or the grid_cell
if
(
!
is.na
(
exclude_grid
)){
if
(
!
is.na
(
exclude_grid
)){
predictors_exclude
d_grid
<-
predictors_obs
[
predictors_obs
$
grid_id
==
exclude_grid
,
]
predictors_exclude
<-
predictors_obs
[
predictors_obs
$
grid_id
==
exclude_grid
,
]
predictors_obs
<-
predictors_obs
[
predictors_obs
$
grid_id
!=
exclude_grid
,
]
predictors_obs
<-
predictors_obs
[
predictors_obs
$
grid_id
!=
exclude_grid
,
]
nr_excluded
<-
nrow
(
predictors_excluded
_grid
)
nr_excluded
<-
nrow
(
predictors_excluded
)
}
}
if
(
!
is.na
(
exclude_grid_rand
)){
if
(
!
is.na
(
exclude_rand
)){
#bin_id --> randomly exclude percentage given in grid_rand
ids_to_exclude
<-
sample
(
predictors_obs
$
id
,
ids_to_exclude
<-
sample
(
predictors_obs
$
bin_id
,
size
=
nrow
(
predictors_obs
)
/
100
*
exclude_rand_perc
,
size
=
nrow
(
predictors_obs
)
/
100
*
exclude_grid_rand_perc
,
replace
=
FALSE
)
replace
=
FALSE
)
predictors_excluded
_grid_rand
<-
predictors_obs
[
predictors_obs
$
bin_id
%in%
ids_to_exclude
,
]
predictors_excluded
<-
predictors_obs
[
predictors_obs
$
bin_id
%in%
ids_to_exclude
,
]
predictors_obs
<-
predictors_obs
[
!
predictors_obs
$
bin_id
%in%
ids_to_exclude
,
]
predictors_obs
<-
predictors_obs
[
!
predictors_obs
$
bin_id
%in%
ids_to_exclude
,
]
nr_excluded
<-
nrow
(
predictors_excluded
_grid_rand
)
nr_excluded
<-
nrow
(
predictors_excluded
)
}
}
# also we increase maxit for the two cases,
# also we increase maxit for the two cases,
# because then slightly less data
# because then slightly less data
if
(
is.na
(
exclude_grid
)
&
is.na
(
exclude_year
)
&
is.na
(
exclude_
grid_
rand
)){
if
(
is.na
(
exclude_grid
)
&
is.na
(
exclude_year
)
&
is.na
(
exclude_rand
)){
nr_maxit
<-
250
}
else
{
nr_maxit
<-
250
}
else
{
nr_maxit
<-
500
}
nr_maxit
<-
500
}
if
(
is_verbose
){
print
(
paste
(
"3. start making all_model_terms"
,
Sys.time
()))}
if
(
is_verbose
){
print
(
paste
(
"3. start making all_model_terms"
,
Sys.time
()))}
...
@@ -367,7 +366,7 @@ m_terms <- c("1",
...
@@ -367,7 +366,7 @@ m_terms <- c("1",
"I(rain_dry^2)"
)
"I(rain_dry^2)"
)
save.image
(
file.path
(
outdir
,
paste0
(
"image_before_model_"
,
exclude_
grid_
rand
,
".RData"
)))
save.image
(
file.path
(
outdir
,
paste0
(
"image_before_model_"
,
exclude_rand
,
".RData"
)))
# save model_terms here
# save model_terms here
model_terms
<-
names
(
glm.nb
(
as.formula
(
paste
(
"nr_nests~"
,
paste
(
m_terms
,
model_terms
<-
names
(
glm.nb
(
as.formula
(
paste
(
"nr_nests~"
,
paste
(
m_terms
,
...
@@ -423,7 +422,7 @@ if(is_verbose){print(paste("8. Start running models", Sys.time()))}
...
@@ -423,7 +422,7 @@ if(is_verbose){print(paste("8. Start running models", Sys.time()))}
results_res
<-
foreach
(
i
=
1
:
nrow
(
all_model_terms
),
results_res
<-
foreach
(
i
=
1
:
nrow
(
all_model_terms
),
.combine
=
rbind
)
%dopar%
{
.combine
=
rbind
)
%dopar%
{
# make results dataframe
# make results dataframe
if
(
is.na
(
exclude_year
)
&
is.na
(
exclude_grid
)
&
is.na
(
exclude_
grid_
rand
)){
if
(
is.na
(
exclude_year
)
&
is.na
(
exclude_grid
)
&
is.na
(
exclude_rand
)){
result
<-
as.data.frame
(
matrix
(
NA
,
ncol
=
3
*
result
<-
as.data.frame
(
matrix
(
NA
,
ncol
=
3
*
length
(
model_terms
)
+
6
,
length
(
model_terms
)
+
6
,
nrow
=
1
))
nrow
=
1
))
...
@@ -490,44 +489,21 @@ results_res <- foreach(i = 1:nrow(all_model_terms),
...
@@ -490,44 +489,21 @@ results_res <- foreach(i = 1:nrow(all_model_terms),
# if we are excluding years, this is the test of predicted data vs observed data
# if we are excluding years, this is the test of predicted data vs observed data
# for this year (with which the model wasn't fitted)
# for this year (with which the model wasn't fitted)
# probably I could code this bit similarly for all
# probably I could code this bit similarly for all
if
(
!
is.na
(
exclude_year
)){
predictors_excluded_year_pred
<-
predictors_excluded_year
predictors_excluded_year_pred
$
offset_term
<-
0
if
(
!
is.na
(
exclude_year
)
|
!
is.na
(
exclude_grid
)
|
!
is.na
(
exclude_rand
)){
prediction_transect_excluded_year
<-
predict.glm
(
res
,
predictors_excluded_pred
<-
predictors_excluded
newdata
=
predictors_excluded_year
,
predictors_excluded_pred
$
offset_term
<-
0
prediction_transect_excluded
<-
predict.glm
(
res
,
newdata
=
predictors_excluded
,
type
=
"response"
)
type
=
"response"
)
cross_lm
_year
=
lm
(
log
(
predictors_excluded
_year
$
ou_dens
+
1
)
~
cross_lm
=
lm
(
log
(
predictors_excluded
$
ou_dens
+
1
)
~
log
(
prediction_transect_excluded
_year
+
1
))
log
(
prediction_transect_excluded
+
1
))
result
[
,
"R2_cross"
]
<-
summary
(
cross_lm
_year
)
$
r.squared
result
[
,
"R2_cross"
]
<-
summary
(
cross_lm
)
$
r.squared
result
[
,
"nr_excluded"
]
<-
nr_excluded
result
[
,
"nr_excluded"
]
<-
nr_excluded
}
}
if
(
!
is.na
(
exclude_grid
)){
predictors_excluded_grid_pred
<-
predictors_excluded_grid
predictors_excluded_grid_pred
$
offset_term
<-
0
prediction_transect_excluded_grid
<-
predict.glm
(
res
,
newdata
=
predictors_excluded_grid
,
type
=
"response"
)
cross_lm_grid
=
lm
(
log
(
predictors_excluded_grid
$
ou_dens
+
1
)
~
log
(
prediction_transect_excluded_grid
+
1
))
result
[
,
"R2_cross"
]
<-
summary
(
cross_lm_grid
)
$
r.squared
result
[
,
"nr_excluded"
]
<-
nr_excluded
}
if
(
!
is.na
(
exclude_grid_rand
)){
predictors_excluded_grid_rand_pred
<-
predictors_excluded_grid_rand
predictors_excluded_grid_rand_pred
$
offset_term
<-
0
prediction_transect_excluded_grid_rand
<-
predict.glm
(
res
,
newdata
=
predictors_excluded_grid_rand
,
type
=
"response"
)
cross_lm_random
=
lm
(
log
(
predictors_excluded_grid_rand
$
ou_dens
+
1
)
~
log
(
prediction_transect_excluded_grid_rand
+
1
))
result
[
,
"R2_cross"
]
<-
summary
(
cross_lm_random
)
$
r.squared
result
[
,
"nr_excluded"
]
<-
nr_excluded
}
return
(
result
)
return
(
result
)
}
}
...
...
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