Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gpp le evaluation #19

Open
wants to merge 79 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
a879813
Delete analysis directory
FrancescoGrossi-unimi Oct 1, 2024
ce57e71
Delete add_vignettes directory
FrancescoGrossi-unimi Oct 1, 2024
5e87a85
Create benchmark-ET.Rmd
FrancescoGrossi-unimi Oct 1, 2024
c694db8
Update benchmark-ET.Rmd
FrancescoGrossi-unimi Oct 1, 2024
d250ef7
Update align_events.R
FrancescoGrossi-unimi Oct 1, 2024
72d95e2
Update analyse_modobs2.R
FrancescoGrossi-unimi Oct 1, 2024
f8c9862
Update eval_droughtresponse.R
FrancescoGrossi-unimi Oct 1, 2024
3c635fd
Update eval_sofun.R
FrancescoGrossi-unimi Oct 1, 2024
e35312e
Update get_stats.R
FrancescoGrossi-unimi Oct 1, 2024
7099437
Update heatscatter_dependencies.R
FrancescoGrossi-unimi Oct 1, 2024
ee79107
Create New_dat_new_model.Rmd
FrancescoGrossi-unimi Oct 10, 2024
2f5bae5
Create New_data_Phydro
FrancescoGrossi-unimi Oct 10, 2024
e721d95
Create Old_data_New_model
FrancescoGrossi-unimi Oct 10, 2024
b5f989c
Create old_data_old_model
FrancescoGrossi-unimi Oct 10, 2024
6a3ebc6
Create New_data_Phydro_PML.Rmd
FrancescoGrossi-unimi Oct 10, 2024
8a97861
Rename New_data_Phydro to New_data_Phydro.Rmd
FrancescoGrossi-unimi Oct 10, 2024
f4d9608
Rename Old_data_New_model to Old_data_New_model.Rmd
FrancescoGrossi-unimi Oct 10, 2024
1d0248b
Rename New_dat_new_model.Rmd to New_data_Pmodel_v4.4l.Rmd
FrancescoGrossi-unimi Oct 10, 2024
cb287ce
Rename New_data_Pmodel_v4.4l.Rmd to New_data_Pmodel_v4.4.Rmd
FrancescoGrossi-unimi Oct 10, 2024
bda8a40
Rename Old_data_New_model.Rmd to Old_data_Pmodel_v4.4.Rmd
FrancescoGrossi-unimi Oct 10, 2024
38dcca8
Rename old_data_old_model to Old_data_Pmodel_v4.2.Rmd
FrancescoGrossi-unimi Oct 10, 2024
b17b818
Add files via upload
FrancescoGrossi-unimi Oct 11, 2024
00f5bff
Add files via upload
FrancescoGrossi-unimi Oct 11, 2024
a53786b
Update Old_data_Pmodel_v4.4.Rmd
FrancescoGrossi-unimi Oct 11, 2024
273eae4
Update New_data_Pmodel_v4.4.Rmd
FrancescoGrossi-unimi Oct 11, 2024
021e9ef
Update New_data_Phydro.Rmd
FrancescoGrossi-unimi Oct 11, 2024
20bd2a7
Update New_data_Phydro_PML.Rmd
FrancescoGrossi-unimi Oct 11, 2024
c89f946
Update New_data_Phydro.Rmd
FrancescoGrossi-unimi Oct 11, 2024
feecbea
Update New_data_Phydro.Rmd
FrancescoGrossi-unimi Oct 11, 2024
dd050cc
Update New_data_Phydro_PML.Rmd
FrancescoGrossi-unimi Oct 11, 2024
8978287
Update calibration_GensSa_blueprint.R
FrancescoGrossi-unimi Oct 11, 2024
e3c782f
Update calibration_GensSa_blueprint.R
FrancescoGrossi-unimi Oct 11, 2024
f156b9a
Update calibration_GensSa_blueprint.R
FrancescoGrossi-unimi Oct 11, 2024
a4010bf
Update calibration_GensSa_blueprint.R
FrancescoGrossi-unimi Oct 11, 2024
e0452e3
Update eval_sofun.R
FrancescoGrossi-unimi Oct 11, 2024
7cdafa4
Rename calibration_GensSa_blueprint.R to calibration_GensSa_blueprint.R
FrancescoGrossi-unimi Oct 11, 2024
dc54728
Create create_obs_eval.R
FrancescoGrossi-unimi Oct 11, 2024
f52c171
Create correct_eval_doriver_2015.R
FrancescoGrossi-unimi Oct 11, 2024
8cc3645
Update correct_eval_doriver_2015.R
FrancescoGrossi-unimi Oct 11, 2024
e18fc8a
Update and rename correct_eval_doriver_2015.R to correct_eval_driver_…
FrancescoGrossi-unimi Oct 11, 2024
5ee0605
Update Old_data_Pmodel_v4.2.Rmd
FrancescoGrossi-unimi Oct 11, 2024
55f67f2
Update Old_data_Pmodel_v4.4.Rmd
FrancescoGrossi-unimi Oct 11, 2024
60cecff
Add files via upload
FrancescoGrossi-unimi Oct 11, 2024
9831863
Rename create_obs_eval.R to create_obs_eval.R
FrancescoGrossi-unimi Oct 11, 2024
9728936
Update New_data_Phydro.Rmd
FrancescoGrossi-unimi Oct 11, 2024
96fff24
Update New_data_Phydro_PML.Rmd
FrancescoGrossi-unimi Oct 11, 2024
4a2b8ab
Update Old_data_Pmodel_v4.4.Rmd
FrancescoGrossi-unimi Oct 11, 2024
9fa23c9
Rename create_obs_eval.R to create_obs_eval.R
FrancescoGrossi-unimi Oct 11, 2024
b647de0
Rename correct_eval_driver_2015.R to correct_eval_driver_2015.R
FrancescoGrossi-unimi Oct 14, 2024
094ff10
Add files via upload
FrancescoGrossi-unimi Oct 14, 2024
9d288d3
Rename calibration_bayesian_new_data_master_50.R to calibration_gensa…
FrancescoGrossi-unimi Oct 14, 2024
e925ad5
Rename calibration_bayesian_new_data_phydro_50.R to calibration_gensa…
FrancescoGrossi-unimi Oct 14, 2024
7137211
Rename calibration_bayesian_new_data_master_old_whc.R to calibration_…
FrancescoGrossi-unimi Oct 14, 2024
eecbb2e
Update and rename calibration_GensSa_blueprint.R to calibration_gensa…
FrancescoGrossi-unimi Oct 14, 2024
afdf204
Delete data/calibration_new_data_25.rds
FrancescoGrossi-unimi Oct 14, 2024
cd11a3b
Delete data/calibration_new_data_50.rds
FrancescoGrossi-unimi Oct 14, 2024
83c8b7e
Delete data/calibration_new_data_75.rds
FrancescoGrossi-unimi Oct 14, 2024
201acca
Delete data/calibration_new_data_phydro_25.rds
FrancescoGrossi-unimi Oct 14, 2024
5dfe2ac
Delete data/calibration_old_data.rds
FrancescoGrossi-unimi Oct 14, 2024
fbae4ab
Delete data/train_site_newdata_25.rds
FrancescoGrossi-unimi Oct 14, 2024
0020282
Delete data/train_site_newdata_50.rds
FrancescoGrossi-unimi Oct 14, 2024
6845f35
Delete data/train_site_newdata_75.rds
FrancescoGrossi-unimi Oct 14, 2024
e054d95
Delete data/train_site_phydro_25.rds
FrancescoGrossi-unimi Oct 14, 2024
fa4b8f0
Add files via upload
FrancescoGrossi-unimi Oct 14, 2024
4ac1333
Update New_data_Pmodel_v4.4.Rmd
FrancescoGrossi-unimi Oct 14, 2024
8e856db
Update New_data_Phydro.Rmd
FrancescoGrossi-unimi Oct 14, 2024
4720862
Update New_data_Phydro_PML.Rmd
FrancescoGrossi-unimi Oct 14, 2024
7f8befd
Add files via upload
FrancescoGrossi-unimi Oct 14, 2024
7792340
Delete data/calibration_new_data_phydro_50.rds
FrancescoGrossi-unimi Oct 14, 2024
6dfcf62
Add files via upload
FrancescoGrossi-unimi Oct 14, 2024
a5188b5
Rename calibration_new_data_phydro_500.rds to calibration_new_data_ph…
FrancescoGrossi-unimi Oct 14, 2024
e976ae0
Add files via upload
FrancescoGrossi-unimi Oct 14, 2024
068413c
straithening
stineb Oct 14, 2024
21915fc
Merge branch 'francesco-ET' of github.com:geco-bern/sofunCalVal into …
stineb Oct 14, 2024
b859f2a
Update eval_sofun.R
FrancescoGrossi-unimi Oct 14, 2024
627a582
Update New_data_Pmodel_v4.4.Rmd
FrancescoGrossi-unimi Oct 14, 2024
2bf7cb9
Update New_data_Phydro.Rmd
FrancescoGrossi-unimi Oct 14, 2024
f7833e1
Update New_data_Phydro_PML.Rmd
FrancescoGrossi-unimi Oct 14, 2024
fe9b727
Update create_obs_eval.R
FrancescoGrossi-unimi Oct 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 77 additions & 40 deletions R/align_events.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,92 +44,98 @@
#' @examples df_alg <- align_events( df, truefalse, before=30, after=300 )

align_events <- function(
df,
df_isevent,
dovars,
leng_threshold,
before,
after,
do_norm = FALSE,
nbins = 6,
normbin = 2
){

df,
df_isevent,
dovars,
leng_threshold,
before,
after,
do_norm = FALSE,
nbins = 6,
normbin = 2
){
## merge df_isevent into df
df <- df %>%
left_join( df_isevent, by=c("site", "date")) %>%
mutate( idx_df = 1:n() )

##--------------------------------------------------------
## Identify events
##--------------------------------------------------------
events <- get_consecutive(
df$isevent,
leng_threshold = leng_threshold,
do_merge = FALSE
)

df$isevent,
leng_threshold = leng_threshold,
do_merge = FALSE #not needed now
)
##--------------------------------------------------------
## Re-arrange data, aligning by beginning of events
## Creates data frame where not all rows are retained from df
## and columns added for 'dday' (number of day relative to onset of event)
## and 'iinst' number of event to which row belongs.
##--------------------------------------------------------
if (nrow(events)>1){

df_dday <- tibble()
for ( iinst in 1:nrow(events) ){


after_inst <- min( after, events$len[iinst] )
dday <- seq( from=-before, to=after_inst, by=1 )
idxs <- dday + events$idx_start[iinst]
drophead <- which( idxs < 1 )
if (length(drophead)>0){
idxs <- idxs[ -drophead ]
dday <- dday[ -drophead ]

addrows <- df %>% slice( idxs )

addrows <- addrows[which(addrows$site %in% unique(sort(addrows$site))[table(sort(addrows$site)) == length(dday)]),]

if (dim(addrows )[1] > 0){
addrows <- addrows %>% mutate(dday=dday, inst=iinst )

}
addrows <- df %>% slice( idxs ) %>% mutate( dday=dday, inst=iinst )

df_dday <- df_dday %>% bind_rows( addrows )
}

##--------------------------------------------------------
## Normalise re-arranged data relative to a certain bin's median
##--------------------------------------------------------
if (do_norm){
## Bins for different variables
bins <- seq( from=-before, to=after, by=(after+before+1)/nbins )

## add bin information based on dday to expanded df
df_dday <- df_dday %>% mutate( inbin = cut( as.numeric(dday), breaks = bins ) )

tmp <- df_dday %>%
dplyr::filter(!is.na(inbin)) %>%
group_by( inbin ) %>%
summarise_at( vars(one_of(dovars)), funs(median( ., na.rm=TRUE )) )

norm <- slice(tmp, normbin)

## subtract from all values
df_dday <- df_dday %>% mutate_at( vars(one_of(dovars)), funs(. - norm$.) )

}

##--------------------------------------------------------
## Aggregate accross events
##--------------------------------------------------------
df_dday_aggbydday <- df_dday %>% group_by( dday ) %>%
summarise_at( vars(one_of(dovars)), funs( median = median( ., na.rm=TRUE), q33( ., na.rm=TRUE), q66( ., na.rm=TRUE) ) ) %>%
filter( !is.na( dday ) )

summarise_at( vars(one_of(dovars)), funs( median = median( ., na.rm=TRUE), q33( ., na.rm=TRUE), q66( ., na.rm=TRUE) ) ) %>%
filter( !is.na( dday ) )
} else {

df_dday <- NULL
df_dday_aggbydday <- NULL

}

out <- list( df_dday=df_dday, df_dday_aggbydday=df_dday_aggbydday )
return( out )

}


Expand All @@ -138,12 +144,42 @@ get_consecutive <- function( dry, leng_threshold=5, anom=NULL, do_merge=FALSE ){
## Returns a dataframe that contains information about events (starting index and length)
## of consecutive conditions (TRUE) in a boolean vector ('dry' - naming is a legacy).
##------------------------------------

## identifies periods where 'dry' true for consecutive days of length>leng_threshold and
## creates data frame holding each instance's info: start of drought by index in 'dry' and length (number of days thereafter)

## replace NAs with FALSE (no drought). This is needed because of NAs at head or tail
dry[ which(is.na(dry)) ] <- FALSE

## identifies periods where 'dry' true for consecutive days of length>leng_threshold and
## creates data frame holding each instance's info: start of drought by index in 'dry' and length (number of days thereafter)

# my implementation using rle

instances <- rle(dry)

# since rle calculate only the lenght of the series, I create the starting position with a loop

# TO DO: change with an apply function

# cumulative_idx <- c(instances$lengths[1])
#
# new_idx <- instances$lengths[1]
#
# for(i in 2:length(instances$lengths)){
#
# new_idx <- new_idx + instances$lengths[i]
#
# cumulative_idx <- append(cumulative_idx,new_idx)
# }
#
# # data frame creation
# instances <- data.frame(idx_start = cumulative_idx, len =instances$lengths, bool=instances$values)
#
# # filter
#
# instances <- instances[instances$bool == TRUE & instances$len > leng_threshold,]
#
# instances$bool <- NULL


instances <- data.frame( idx_start=c(), len=c() )
consecutive_dry <- rep( NA, length( dry ) )
ndry <- 0
Expand Down Expand Up @@ -228,6 +264,7 @@ get_consecutive <- function( dry, leng_threshold=5, anom=NULL, do_merge=FALSE ){

}


return( instances )
}

Expand Down
49 changes: 24 additions & 25 deletions R/analyse_modobs2.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,19 @@ analyse_modobs2 <- function(df,
# require(LSD)
require(ggthemes)
require(RColorBrewer)
source(here::here("R/heatscatter_dependencies.R"))


# if (identical(filnam, NA)) filnam <- "analyse_modobs.pdf"

## rename to 'mod' and 'obs' and remove rows with NA in mod or obs
df <- df %>%
as_tibble() %>%
ungroup() %>%
dplyr::select(mod = mod, obs = obs) %>%
tidyr::drop_na(mod, obs)

## get linear regression (coefficients)
linmod <- lm(obs ~ mod, data = df)

## construct metrics table using the 'yardstick' library
df_metrics <- df %>%
yardstick::metrics(obs, mod) %>%
Expand All @@ -54,7 +53,7 @@ analyse_modobs2 <- function(df,
)) %>%
dplyr::bind_rows(tibble(.metric = "bias", .estimator = "standard", .estimate = dplyr::summarise(df, mean((mod - obs), na.rm = TRUE)) %>% unlist())) %>%
dplyr::bind_rows(tibble(.metric = "pbias", .estimator = "standard", .estimate = dplyr::summarise(df, mean((mod - obs) / obs, na.rm = TRUE)) %>% unlist()))

rsq_val <- df_metrics %>%
dplyr::filter(.metric == "rsq") %>%
dplyr::select(.estimate) %>%
Expand Down Expand Up @@ -85,21 +84,21 @@ analyse_modobs2 <- function(df,
dplyr::select(.estimate) %>%
unlist() %>%
unname()

if (relative) {
rmse_val <- rmse_val / mean(df$obs, na.rm = TRUE)
bias_val <- bias_val / mean(df$obs, na.rm = TRUE)
}

rsq_lab <- format(rsq_val, digits = 2)
rmse_lab <- format(rmse_val, digits = 3)
mae_lab <- format(mae_val, digits = 3)
bias_lab <- format(bias_val, digits = 3)
slope_lab <- format(slope_val, digits = 3)
n_lab <- format(n_val, digits = 3)

results <- tibble(rsq = rsq_val, rmse = rmse_val, mae = mae_val, bias = bias_val, slope = slope_val, n = n_val)

if (shortsubtitle) {
subtitle <- bquote(italic(R)^2 == .(rsq_lab) ~ ~
RMSE == .(rmse_lab))
Expand All @@ -110,9 +109,9 @@ analyse_modobs2 <- function(df,
slope == .(slope_lab) ~ ~
italic(N) == .(n_lab))
}

if (type == "heat") {

# if (!identical(filnam, NA)) dev.off()
gg <- heatscatter(
df$mod,
Expand All @@ -122,20 +121,20 @@ analyse_modobs2 <- function(df,
main = "",
ggplot = TRUE
)

gg <- gg +
geom_abline(intercept = 0, slope = 1, linetype = "dotted") +
theme_classic() +
labs(x = mod, y = obs)

if (plot_linmod) gg <- gg + geom_smooth(method = "lm", color = "red", size = 0.5, se = FALSE)
if (plot_subtitle) gg <- gg + labs(subtitle = subtitle)

if (!identical(filnam, NA)) {
ggsave(filnam, width = 5, height = 5)
}
} else if (type == "hex") {

## ggplot hexbin
gg <- df %>%
ggplot2::ggplot(aes(x = mod, y = obs)) +
Expand All @@ -149,15 +148,15 @@ analyse_modobs2 <- function(df,
# ylim(0,NA) +
theme_classic() +
labs(x = mod, y = obs)

if (plot_subtitle) gg <- gg + labs(subtitle = subtitle)
if (plot_linmod) gg <- gg + geom_smooth(method = "lm", color = "red", size = 0.5, se = FALSE)

if (!identical(filnam, NA)) {
ggsave(filnam, width = 5, height = 5)
}
} else if (type == "points") {

## points
gg <- df %>%
ggplot(aes(x = mod, y = obs)) +
Expand All @@ -168,15 +167,15 @@ analyse_modobs2 <- function(df,
# ylim(0,NA) +
theme_classic() +
labs(x = mod, y = obs)

if (plot_subtitle) gg <- gg + labs(subtitle = subtitle)
if (plot_linmod) gg <- gg + geom_smooth(method = "lm", color = "red", size = 0.5, se = FALSE)

if (!identical(filnam, NA)) {
ggsave(filnam, width = 5, height = 5)
}
} else if (type == "density") {

## points
gg <- df %>%
ggplot(aes(x = mod, y = obs)) +
Expand All @@ -191,14 +190,14 @@ analyse_modobs2 <- function(df,
# ylim(0,NA) +
theme_classic() +
labs(x = mod, y = obs)

if (plot_subtitle) gg <- gg + labs(subtitle = subtitle)
if (plot_linmod) gg <- gg + geom_smooth(method = "lm", color = "red", size = 0.5, se = FALSE)

if (!identical(filnam, NA)) {
ggsave(filnam, width = 5, height = 5)
}
}

return(list(df_metrics = df_metrics, gg = gg, linmod = linmod, results = results))
}
Loading