Skip to content

Commit 53c9d25

Browse files
committed
Fix a bug when there are no data in middle years
1 parent 6127769 commit 53c9d25

2 files changed

Lines changed: 9 additions & 8 deletions

File tree

R/base.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#************************************************************************************
1+
#*******************************************************************************
22
# Description: Needed library and pre-defined functions
33
# Date: 2020-11-22
4-
#************************************************************************************
4+
#*******************************************************************************
55

66
.datatable.aware <- TRUE
77

@@ -62,9 +62,9 @@ FormatAvgData <- function(date_vec, vi_vec) {
6262
evi2 = vi_vec,
6363
avg_date = ""
6464
)
65-
vi_dt$avg_date <- as.Date(paste0("1970", substr(vi_dt$date, 5, 10)))
65+
vi_dt[, avg_date := as.Date(paste0("1970", substr(vi_dt$date, 5, 10)))]
6666
vi_dt <- stats::na.omit(vi_dt)
67-
vi_dt <- data.table::setorder(vi_dt, date)
67+
data.table::setorder(vi_dt, date)
6868

6969
# Find unique dates in the averaged year
7070
unique_dates <- unique(vi_dt$avg_date)

R/mod_fit.R

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,13 @@ FitBLSP <- function(date_vec, vi_vec,
115115
# year id vector
116116
if (is.null(start_yr) || is.null(end_yr)) {
117117
yr <- lubridate::year(date_vec) - lubridate::year(date_vec)[1] + 1
118-
years <- sort(unique(year(date_vec)))
119-
numYears <- length(unique(yr))
118+
tmp <- sort(unique(year(date_vec)))
119+
years <- tmp[1]:tmp[length(tmp)]
120+
numYears <- length(1:yr[length(yr)])
120121
} else {
121-
yr <- start_yr:end_yr - end_yr + 1
122+
yr <- lubridate::year(date_vec) - lubridate::year(date_vec)[1] + 1
122123
years <- start_yr:end_yr
123-
numYears <- length(yr)
124+
numYears <- length(years)
124125
}
125126
# If user specified weights
126127
if (is.null(weights_vec)) {

0 commit comments

Comments
 (0)