Skip to content

Commit 2c3b167

Browse files
committed
second test whittaker lambda formula
1 parent f74d91d commit 2c3b167

13 files changed

+374
-141
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Description: A remote sensing vegetation index phenology extraction package.
88
License: MIT | file LICENSE
99
Encoding: UTF-8
1010
LazyData: true
11-
RoxygenNote: 6.0.1.9000
11+
RoxygenNote: 6.0.1
1212
LinkingTo: Rcpp, RcppArmadillo
1313
Imports: Rcpp, RcppArmadillo,
1414
tibble, dplyr, purrr, stringr, tidyr, ggplot2, lubridate,

NEWS.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# phenofit 0.1.2
2+
3+
* Added a `NEWS.md` file to track changes to the package.
4+
5+
6+

R/tools.R

+28-7
Original file line numberDiff line numberDiff line change
@@ -199,22 +199,39 @@ cv_coef <- function(x, w){
199199
#' @param Y_sim Numeric vector, corresponding simulated values
200200
#' @param w Numeric vector, weights of every points
201201
#' @param include.cv If true, cv will be returned.
202+
#'
203+
#' @return
204+
#' \itemize{
205+
#' \item \code{RMSE} root mean square error
206+
#' \item \code{NSE} NASH coefficient
207+
#' \item \code{R2} correlation of determination
208+
#' \item \code{Bias} bias
209+
#' \item \code{Bias_perc} bias percentage
210+
#' \item \code{MAE} mean absolute error
211+
#' \item \code{pvalue} pvalue of \code{R}
212+
#' \item \code{n_sim} number of valid obs.
213+
#' \item \code{R} pearson correlation
214+
#' }
202215
#' @export
203216
GOF <- function(Y_obs, Y_sim, w, include.cv = FALSE){
204217
if (missing(w)) w <- rep(1, length(Y_obs))
205218

206-
# remove NA values in Y_sim, Y_obs and w
207-
I <- which(!(is.na(Y_sim) | is.na(Y_obs) | is.na(w)))
219+
# remove NA and Inf values in Y_sim, Y_obs and w
220+
valid <- function(x) !is.na(x) & is.finite(x)
221+
222+
I <- which(valid(Y_sim) & valid(Y_obs) & valid(w))
208223
# n_obs <- length(Y_obs)
209224
n_sim <- length(I)
210225

211226
Y_sim <- Y_sim[I]
212227
Y_obs <- Y_obs[I]
228+
w <- w[I]
213229

214230
if (include.cv) CV <- cv_coef(Y_obs, w)
215231
if (is_empty(Y_obs)){
216-
out <- c(Bias = NA, MAE = NA,RMSE = NA, NSE = NA, R2 = NA,
217-
pvalue = NA, n_sim = NA, R = NA)
232+
out <- c(RMSE = RMSE, NSE = NSE, R2 = R2, MAE = MAE,
233+
Bias = Bias, Bias_perc = Bias_perc,
234+
R = NA, pvalue = NA, n_sim = NA)
218235
if (include.cv) out <- c(out, CV)
219236
return(out) #R = R,
220237
}
@@ -228,10 +245,12 @@ GOF <- function(Y_obs, Y_sim, w, include.cv = FALSE){
228245

229246
SSR <- sum( (Y_sim - y_mean)^2 * w)
230247
SST <- sum( (Y_obs - y_mean)^2 * w)
231-
R2 <- SSR / SST
248+
# R2 <- SSR / SST
249+
R2 <- summary(lm(Y_sim ~ Y_obs))$r.squared
232250

233251
RE <- Y_sim - Y_obs
234252
Bias <- sum ( w*RE) /sum(w) # bias
253+
Bias_perc <- Bias/y_mean # bias percentage
235254
MAE <- sum ( w*abs(RE))/sum(w) # mean absolute error
236255
RMSE <- sqrt( sum(w*(RE)^2)/sum(w) ) # root mean sqrt error
237256

@@ -251,13 +270,15 @@ GOF <- function(Y_obs, Y_sim, w, include.cv = FALSE){
251270
message(e$message)
252271
})
253272

254-
out <- c(Bias = Bias, MAE = MAE,RMSE = RMSE, NSE = NSE, R2 = R2,
255-
pvalue = pvalue, n_sim = n_sim, R = R)
273+
out <- c(RMSE = RMSE, NSE = NSE, R2 = R2, MAE = MAE,
274+
Bias = Bias, Bias_perc = Bias_perc,
275+
R = R, pvalue = pvalue, n_sim = n_sim)
256276
if (include.cv) out <- c(out, CV)
257277
return(out)
258278
}
259279

260280

281+
261282
#' Determinated correlation critical value
262283
#'
263284
#' @param n length of observation.

man/GOF.Rd

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/07_whit/03_evaluate_Whittaker_GOF.R

+41-18
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ if (!file.exists(file)){
1919
}else{
2020
load(file)
2121
}
22+
2223
# load("D:/Documents/GoogleDrive/phenofit.rda")
2324

2425
methods <- c('AG', 'BECK', 'ELMORE', 'ZHANG', 'whit_R', 'whit_gee')[-5]
@@ -29,35 +30,58 @@ prefix <- c("phenoflux", "phenocam")[i]
2930
df <- if(i == 1) df_flux else df_cam
3031
st <- if(i == 1) st_flux else st_cam
3132

32-
df <- df[iters == "iter2"]
3333
st$IGBPname %<>% factor(IGBPnames_006)
34+
st <- st[order(IGBPname,site), ] # reorder according to IGBP
35+
st[site %in% sites_sel, titlestr := sprintf("(%s) %s, %s", letters[1:.N],site, IGBPname)]
3436

3537
# make sure different curve fitting methods have the same length fitting
36-
formula <- if(i == 1) formula(site+date+t+y+GPP_NT+GPP_DT+SummaryQA~meth) else
37-
formula(site+date+t+y+gcc+vci+SummaryQA~meth)
38-
39-
over_perform(df, formula, prefix)
38+
formula <- if(i == 1) formula(site+date+t+y+GPP_NT+GPP_DT+SummaryQA+iters~meth) else
39+
formula(site+date+t+y+gcc+vci+SummaryQA+iters~meth)
40+
IGBP.all = F
41+
if (i == 1){
42+
over_perform(df[iters == "iter2"], formula, prefix, IGBP.all = IGBP.all)
43+
} else{
44+
over_perform(df[iters == "iter2"], formula, prefix, ylim2 = c(64, 100), IGBP.all = IGBP.all)
45+
}
4046

4147
# site figure data input
42-
4348
df_trim <- dcast(df, formula, value.var = "value", fun.aggregate = mean) # %>% na.omit()
4449
df_trim$SummaryQA %<>% factor(qc_levels)
4550
# df_trim <- melt(df_trim, measure.vars = methods, variable.name = "meth")
4651

47-
sites <- unique(df$site)
52+
sites <- st$site
53+
# sites <- unique(df$site)
4854
sitename <- sites[100]
4955

50-
vars <- c("get_range", "save_pdf", "lgd_vci", "lgd_gpp",
51-
"qc_levels", "qc_colors", "qc_shapes", "methods")
52-
cl <- cluster_Init(pkgs = c("data.table", "ggplot2", "magrittr"))
53-
clusterExport(cl, vars)
54-
res <- parLapplyLB(cl, sites, plot_whit,
55-
df_trim, st, prefix_fig = paste0("whit_", prefix))
56-
for (i in seq_along(sites)){
57-
runningId(i)
58-
sitename <- sites[i]
59-
plot_whit(sitename, df_trim, st, prefix_fig = paste0("whit_", prefix))
56+
# vars <- c("get_range", "save_pdf", "lgd_vci", "lgd_gpp",
57+
# "qc_levels", "qc_colors", "qc_shapes", "methods")
58+
# cl <- cluster_Init(pkgs = c("data.table", "ggplot2", "magrittr"))
59+
# clusterExport(cl, vars)
60+
# res <- parLapplyLB(cl, sites, plot_whit,
61+
# df_trim, st, prefix_fig = paste0("whit_", prefix))
62+
63+
plot_whits <- function(sites, df_trim, file){
64+
ps <- list()
65+
for (i in seq_along(sites)){
66+
runningId(i)
67+
sitename <- sites[i]
68+
# for single method, ggplot obj return
69+
ps[[i]] <- plot_methods(sitename, df_trim, st, prefix_fig = paste0("whit_", prefix), methods = "whit_gee", show.legend = F)
70+
# print(p)
71+
# for all methods, grob obj return
72+
# p <- plot_methods(sitename, df_trim, st, prefix_fig = paste0("whit_", prefix), methods)
73+
}
74+
75+
ylab_r <- expression("GPP ( gC "*mm^-1*d^-1*" )")
76+
# ylab_r <- "VCI"
77+
FigsToPages(ps, lgd_gpp, ylab_r, file, width = 10)
6078
}
79+
plot_whits(sites, df_trim, "gee_whit_flux166.pdf") # all sites
80+
81+
## 2. select representative points for fluxnet
82+
sites_sel <- c("RU-Fyo", "FR-Pue", "US-UMB", "BE-Vie", "US-KS2", "US-Whs",
83+
"AU-How", "AU-Dry", "CH-Fru", "US-Los", "DE-Geb")
84+
plot_whits(sites_sel, df_trim, "gee_whit_flux11.pdf") # all sites
6185

6286
# merge_pdf('../whit_phenoflux166.pdf', indir = "./")
6387
# merge_pdf('whit_phenoflux166.pdf', indir = "Figure/")
@@ -77,6 +101,5 @@ merge_pdf('whit_phenoflux166.pdf', indir = "Figure", "whit_phenoflux.*.pdf", del
77101
# "snow/ice" = "#F8766D", "cloud" = "#C77CFF"), drop = F) +
78102
# scale_shape_manual(values = c(19, 15, 4, 17), drop = F) +
79103
# scale_y_continuous(lim = lim_raw)
80-
81104
# p3 <- ggplot_dual_axis(p1, p2) #%>% as.ggplot()
82105
# p <- ggplot_dual_axis(p3, p0, add_yaxis_r = F)

test/07_whit/04_read_GEE_whitMatrix.R

+3-26
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ lgd <- phenofit:::make_legend(linename = c("iter1", "iter2"),
2626
linecolor = c("blue", "red"))
2727

2828
file <- "whit_GEE.pdf"
29-
Cairo::CairoPDF(file, 10, 4)
29+
# Cairo::CairoPDF(file, 10, 4)
3030
# par(mfrow = c(4, 1), mar = c(1, 2, 3, 1), mgp = c(1.5, 0.6, 0))
3131
## sometimes sample and reduceRegions result is different
3232
ps <- list()
@@ -64,30 +64,7 @@ for (i in seq_along(sites)){
6464
# grid.draw(p2)
6565
}
6666

67-
dev.off()
68-
file.show(file)
67+
# dev.off()
68+
# file.show(file)
6969

7070
file = "Fig3_whit_point_example.pdf"
71-
Cairo::CairoPDF(file, 10, nrow*1.6)
72-
73-
nrow <- 6
74-
npage <- ceiling(length(ps)/nrow)
75-
for (i in 1:npage){
76-
runningId(i)
77-
78-
I_beg <- (i - 1) * nrow + 1
79-
I_end <- min(i*nrow, length(ps))
80-
81-
x <- c(ps[I_beg:I_end], list(lgd))
82-
nx <- length(x)
83-
x[[nx - 1]] <- x[[nx - 1]] +
84-
theme(axis.title.x = element_text(),
85-
axis.text.x = element_text())
86-
p2 <- gridExtra::arrangeGrob(grobs = x, nrow = nx, ncol = 1,
87-
heights = c(rep(5, nx - 2), 5.5, 1),padding = unit(1, "line"),
88-
left = textGrob("EVI", rot = 90, gp=gpar(fontsize=14)))
89-
if (i != 1) grid.newpage();
90-
grid.draw(p2)
91-
}
92-
dev.off()
93-
file.show(file)

0 commit comments

Comments
 (0)