Skip to content

Commit

Permalink
Import and export
Browse files Browse the repository at this point in the history
  • Loading branch information
FinYang committed Mar 12, 2024
1 parent c842ae3 commit 1e269e9
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 20 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Imports:
dplyr (>= 1.0.0),
future.apply,
generics,
ggplot2,
lubridate,
magrittr,
Matrix,
Expand All @@ -47,7 +48,6 @@ RoxygenNote: 7.3.1
Suggests:
testthat (>= 3.0.0),
knitr,
rmarkdown,
ggplot2
rmarkdown
Language: en-AU
Config/testthat/edition: 3
18 changes: 18 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,48 @@ importFrom(dplyr,all_of)
importFrom(dplyr,any_of)
importFrom(dplyr,arrange)
importFrom(dplyr,bind_rows)
importFrom(dplyr,case_when)
importFrom(dplyr,distinct)
importFrom(dplyr,ends_with)
importFrom(dplyr,filter)
importFrom(dplyr,group_by)
importFrom(dplyr,group_split)
importFrom(dplyr,lag)
importFrom(dplyr,lead)
importFrom(dplyr,left_join)
importFrom(dplyr,mutate)
importFrom(dplyr,n)
importFrom(dplyr,pull)
importFrom(dplyr,rename)
importFrom(dplyr,rename_with)
importFrom(dplyr,rowwise)
importFrom(dplyr,select)
importFrom(dplyr,ungroup)
importFrom(generics,augment)
importFrom(ggplot2,aes)
importFrom(ggplot2,autoplot)
importFrom(ggplot2,facet_grid)
importFrom(ggplot2,geom_line)
importFrom(ggplot2,ggplot)
importFrom(lubridate,"day<-")
importFrom(lubridate,day)
importFrom(lubridate,days)
importFrom(lubridate,month)
importFrom(lubridate,wday)
importFrom(lubridate,years)
importFrom(lubridate,ymd)
importFrom(magrittr,"%>%")
importFrom(rlang,"!!")
importFrom(rlang,"%||%")
importFrom(rlang,":=")
importFrom(rlang,.data)
importFrom(rlang,enexpr)
importFrom(rlang,enexprs)
importFrom(rlang,sym)
importFrom(rlang,syms)
importFrom(stats,runif)
importFrom(stats,var)
importFrom(tibble,as_tibble)
importFrom(tibble,tibble)
importFrom(tidyr,unnest)
useDynLib(ycevo)
19 changes: 11 additions & 8 deletions R/augment.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

#' Augment data with predicted discount function and yield curve
#' @param ... Additional arguments required for generic consistency. Currently not used.
#' Warning: A misspelled argument will not raise an error.
#' The misspelled argument will be either disregarded, or the default value will be applied if one exists.
Expand All @@ -23,7 +24,7 @@ augment.ycevo <- function(

# newdata <- tibble(qdatetime, tupq, tau)
if(!loess) {
norow <- anti_join(newdata, df_flat, by = setdiff(c("tau"), colnames(newdata)))
norow <- dplyr::anti_join(newdata, df_flat, by = setdiff(c("tau"), colnames(newdata)))
if(nrow(norow)>0) {
stop("If loess = FALSE, newdata should be a subset of the xgrid and tau used to fit ycevo (e.g. the default).")
}
Expand Down Expand Up @@ -62,7 +63,7 @@ interpolate <- function(object, newdata, qdate_label){
stats::loess(.discount ~ tau,
# interpolate on the log of discount to avoid negative values
data = mutate(data, .discount = log(.discount)),
control = loess.control(surface = "direct")))) %>%
control = stats::loess.control(surface = "direct")))) %>%
select(-.est)

# list of unique values of covariates (including time)
Expand All @@ -77,7 +78,7 @@ interpolate <- function(object, newdata, qdate_label){
find_near <- function(x){
# x <- seq(ymd("2023-02-01"), ymd("2023-07-01"), by = "1 month")
# browser()
target <- getElement(ls_x, cur_column())
target <- getElement(ls_x, dplyr::cur_column())
l <- length(target)

int <- match(x, target)
Expand Down Expand Up @@ -108,14 +109,14 @@ interpolate <- function(object, newdata, qdate_label){

df_predict <- df_near %>%
# nest by loess to speed up prediction
nest(.by = ends_with(near.)) %>%
tidyr::nest(.by = ends_with(near.)) %>%
# rename back to match loess name
rename_with(function(x) gsub(near.,"", x), ends_with(near.)) %>%
# match loess
left_join(df_loess, by = c(qdate_label, xnames)) %>%
# predict discount rate
mutate(.discount = mapply(function(data, loess){
predict(loess, data$tau)
stats::predict(loess, data$tau)
}, data = data, loess = loess, SIMPLIFY = FALSE)) %>%
# drop loess
select(-loess) %>%
Expand All @@ -126,13 +127,13 @@ interpolate <- function(object, newdata, qdate_label){

df_temp <- df_predict %>%
group_by(!!!syms(c(ax, "tau"))) %>%
summarise(.discount = interp(list(!!!syms(ax.)),
dplyr::summarise(.discount = interp(list(!!!syms(ax.)),
.discount,
lapply(list(!!!syms(ax)), unique)),
.groups = "drop") %>%
# the interpolation was done on the log of discount
# to prevent negative values
mutate(.discount = exp(.discount)) %>%
mutate(.discount = exp(.data$.discount)) %>%
mutate(.yield = discount2yield(.discount, tau))
left_join(newdata, df_temp, by = c(qdate_label, xnames, "tau"))
}
Expand Down Expand Up @@ -181,7 +182,9 @@ interp2 <- function(x, y, xout) {
if(is.vector(g)) g <- t(g)
# skip when there is only one value for that dimension
if(dim(g)[[ds[[d]]]] == 1) next
g <- apply(g, d, function(y) approx(x = ux[[ds[[d]]]], y = y, xout = xout[[ds[[d]]]], rule = 2)$y)
g <- apply(g, d, function(y) stats::approx(x = ux[[ds[[d]]]], y = y, xout = xout[[ds[[d]]]], rule = 2)$y)
}
unname(as.vector(g))
}


4 changes: 2 additions & 2 deletions R/autoplot.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' @importFrom ggplot2 autoplot
#' @importFrom ggplot2 autoplot ggplot aes geom_line facet_grid
#' @export
autoplot.ycevo <- function(
object, est = c("both", "discount", "yield"),
Expand All @@ -14,7 +14,7 @@ autoplot.ycevo <- function(
df_plot <- augment(object, loess = loess) %>%
mutate(!!sym(qdate_label)) %>%
select(!all_of(which_drop)) %>%
pivot_longer(any_of(c(".discount", ".yield")),
tidyr::pivot_longer(any_of(c(".discount", ".yield")),
names_to = ".est",
values_to = ".value")
against <- match.arg(against)
Expand Down
7 changes: 3 additions & 4 deletions R/simulation.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#' @importFrom lubridate wday day month day<- years
#' @export
ycevo_data <- function() {
first_qdate <- ymd("20230101")
last_qdate <- first_qdate
month(last_qdate) <- 12
day(last_qdate) <- 31
last_qdate <- ymd("20231231")

ad <- seq(ymd("2023-01-01"), ymd("2023-12-31"), by = "1 day")
wd <- ad[wday(ad, week_start = 1) < 6]
Expand Down Expand Up @@ -39,7 +38,7 @@ ycevo_data <- function() {
}
out
}
bond_meta <- tribble(~ type, ~ n, ~ arg_range_issued, ~ maturity,
bond_meta <- tibble::tribble(~ type, ~ n, ~ arg_range_issued, ~ maturity,
1, 40, c(20, -1), 20,
2, 40, c(2, -1), 2,
2, 40, c(3, -1), 3,
Expand Down
10 changes: 9 additions & 1 deletion R/ycevo-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,29 @@
#' @importFrom dplyr any_of
#' @importFrom dplyr arrange
#' @importFrom dplyr bind_rows
#' @importFrom dplyr case_when
#' @importFrom dplyr distinct
#' @importFrom dplyr ends_with
#' @importFrom dplyr filter left_join mutate select group_by lead lag group_split ungroup
#' @importFrom dplyr n
#' @importFrom dplyr pull
#' @importFrom dplyr rename
#' @importFrom dplyr rename_with
#' @importFrom dplyr rowwise
#' @importFrom lubridate ymd
#' @importFrom magrittr %>%
#' @importFrom Matrix colSums rowSums sparseMatrix t
#' @importFrom Rcpp evalCpp
#' @importFrom Rcpp sourceCpp
#' @importFrom rlang :=
#' @importFrom rlang !! sym .data
#' @importFrom rlang %||%
#' @importFrom rlang :=
#' @importFrom rlang syms
#' @importFrom stats runif
#' @importFrom stats var
#' @importFrom tibble as_tibble
#' @importFrom tibble tibble
#' @importFrom tidyr unnest
## usethis namespace: end
#' @useDynLib ycevo
#' @references Koo, B., La Vecchia, D., & Linton, O. (2021). Estimation of a nonparametric model for bond prices from cross-section and time series information. Journal of Econometrics, 220(2), 562-588.
Expand Down
5 changes: 2 additions & 3 deletions R/ycevo.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
#'
#' @references Koo, B., La Vecchia, D., & Linton, O. (2021). Estimation of a nonparametric model for bond prices from cross-section and time series information. Journal of Econometrics, 220(2), 562-588.
#' @order 1
#' @importFrom rlang enexpr
#' @importFrom rlang enexpr enexprs
#' @importFrom lubridate days
#' @export
ycevo <- function(data,
Expand Down Expand Up @@ -154,7 +154,7 @@ ycevo <- function(data,
stop(paste0(names(dots)[temp], collapse = ", "), " column(s) not found in the data")
}

xgrid <- ecdf(data$qdate)(x)
xgrid <- stats::ecdf(data$qdate)(x)

# Handle interest rate
interest <- NULL
Expand Down Expand Up @@ -275,7 +275,6 @@ check_hx <- function(xgrid, hx, data){
hx
}

#' @export
seq_tau <- function(max_tau) {
tau <- c(seq(30, 6 * 30, 30), # Monthly up to six months
seq(240, 2 * 365, 60), # Two months up to two years
Expand Down

0 comments on commit 1e269e9

Please sign in to comment.