You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A plotting function ts_sma_plot gets a simple moving average of a provided order and plots it against the original data. This function should also allow for plotting multiple moving averages and should return a plotly::ggplotly if the parameter .interactive is set to TRUE
library(timetk)
library(dplyr)
ts_sma_plot<-function(.data, .sma_order, .func=mean, .align="center",
.partial=FALSE, .multi_plot=FALSE,
.interactive=FALSE) {
# * Tidyeval ----# slidify_vec parameterssma_vec<- as.vector(.sma_order)
sma_fun<-.funcsma_align<-stringr::str_to_lower(as.character(.align))
sma_partial<- as.logical(.partial)
multi_plot<- as.logical(.multi_plot)
interactive<- as.logical(.interactive)
# * Checks ----if(!sma_align%in% c("center","left","right")){
stop(call.=FALSE, "(.align) must be either 'center','left', or 'right'")
}
if(!is.numeric(sma_vec)){
stop(call.=FALSE, "(.sma_order) must be all numeric values, c(1,2,3,...)")
}
if(!is.logical(sma_partial) &!is.logical(multi_plot) &!is.logical(interactive)){
stop(call.=FLASE, "(.partial) (.multi_plot) and (.interactive) must all be logical values.")
}
# Get data objectts_obj<-.data# Get data and try to coerce to tibble# We do this because we use timetk::slidify_vecif(stats::is.ts(ts_obj) |stats::is.mts(ts_obj) |xts::is.xts(ts_obj) |zoo::is.zoo(ts_obj)){
message("Attempting to coerce to a tibble.")
ts_tbl<-timetk::tk_tbl(ts_obj) # change to internal ts_to_tbl() func
} else {
ts_tbl<-ts_obj
}
# * Loop through periods ----df<-data.frame(matrix(ncol=0, nrow=0))
for(iinsma_vec){
ret_tmp<-ts_tbl %>%
dplyr::mutate(sma_order=i) %>%
dplyr::mutate(sma_value=timetk::slidify_vec(
.x=value,
.f=sma_fun,
.period=i,
.align=sma_align,
.partial=sma_partial
))
df<-base::rbind(df, ret_tmp)
}
# * Plots ----# * Return ----return(df)
}
A plotting function
ts_sma_plot
gets a simple moving average of a provided order and plots it against the original data. This function should also allow for plotting multiple moving averages and should return aplotly::ggplotly
if the parameter.interactive
is set toTRUE
The text was updated successfully, but these errors were encountered: