smoother/0000755000176200001440000000000014603135363012116 5ustar liggesuserssmoother/NAMESPACE0000644000176200001440000000022212406236212013323 0ustar liggesusersexportPattern("^[[:alpha:]]+") export(smth.gaussian) export(smth) importFrom(TTR,SMA) importFrom(TTR,EMA) importFrom(TTR,DEMA) importFrom(TTR,WMA)smoother/man/0000755000176200001440000000000012406244061012664 5ustar liggesuserssmoother/man/smoother.Rd0000644000176200001440000000164614603107563015030 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/smoother-package.R \name{smoother} \alias{smoother} \title{Smooth Data in R} \description{ \code{smoother} Package for the Smoothing of Numerical Data } \details{ \code{smoother} is presently limited to a port of the Matlab 'Gaussian Window' Function, as well as a limited number of moving averages (\code{sma, ema, dema} and \code{'wma'}). Code for the gaussian window function has been written locally within this package, however, the moving averages are called from the \link{TTR} package (\url{https://CRAN.R-project.org/package=TTR}) and are included as a matter of convenience. For further information (and examples) with regards to utilizing the primary helper function, please refer to the \link{smth} function help file } \references{ The Gaussian Smoothing component of the \code{smoother} package has been loosley adapted from elsewhere } smoother/man/smth.gaussian.Rd0000644000176200001440000000204314603106621015736 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/smth-gaussian.R \name{smth.gaussian} \alias{smth.gaussian} \title{Smooth Using Gaussian Window} \usage{ smth.gaussian( x = stop("Numeric Vector 'x' is Required"), window = getOption("smoother.window"), alpha = getOption("smoother.gaussianwindow.alpha"), ..., tails = getOption("smoother.tails") ) } \arguments{ \item{x}{numeric vector of values to smooth, error will be thrown if not provided.} \item{window}{the length of the smoothing window, if an integer, represents number of items, else, if a value between \code{0} and \code{1}, represents the proportion of the input vector} \item{alpha}{parameter to determine the breadth of the gaussian window, yielding more or less sensitive smoothing characteristics} \item{...}{not used} \item{tails}{Logical value as to whether the tail regions should be included or not.} } \description{ The specific function for smoothing using the gaussian window function } \examples{ y = runif(100) ys = smth.gaussian(y) } smoother/man/smth.Rd0000644000176200001440000000343314603107446014137 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/smth.R \name{smth} \alias{smth} \alias{sma} \alias{ema} \alias{dema} \alias{wma} \alias{gaussian} \title{Smooth Numerical Data} \usage{ smth( x = stop("Numeric Vector 'x' is Required"), method = getOption("smoother.method"), ... ) } \arguments{ \item{x}{numeric vector of values to smooth} \item{method}{one of \code{'gaussian', 'sma', 'ema', 'dema'} or \code{'wma'}.} \item{...}{any other arguments to be passed to each specific smoothing methodology.} } \value{ a numeric vector of same length as input \code{'x'} vector } \description{ Helper function to smooth numerical data using methods specified by the user. } \details{ At this moment in time, the only method is the \code{'gaussian'} window function (similar to the Matlab Gaussian Window Smoothing Function) and a number of moving averages \code{'sma', 'ema', 'dema'} or \code{'wma'}. These are functions that allows the user to smooth an input vector, returning vector of the same length as the input. This can also be achieved using the specific \code{\link{smth.gaussian}} function. } \examples{ #Prepare Data n = 1000 x = seq(-pi,pi,length.out=n) y = sin(x) + (runif(length(x))*0.1) #NOISY DATA ys = smth(y,window = 0.1,method = "gaussian") #SMOOTHING plot(x,y,type="l",col="red") lines(x,ys,col="black",lwd=3) title("Example Smoothing of Noisy Data") } \references{ If the \code{'method'} argument is equal to \code{'gaussian'}, then this function is a port of the function described here: \url{https://goo.gl/HGM47U}, very loosly based of code which has also been ported to c++ elsewhere } \seealso{ Refer to specific man files: \code{\link{smth.gaussian}}, \code{\link[TTR]{SMA}}, \code{\link[TTR]{EMA}}, \code{\link[TTR]{DEMA}} or \code{\link[TTR]{WMA}} } smoother/man/smth.options.Rd0000644000176200001440000000175014603106621015623 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/onLoad.R \name{smth.options} \alias{smth.options} \title{Smoother Options} \description{ Several Global Options have been declared, as described in this help file. } \details{ The following global options can be modified, to alter the default calculation behaviour. \tabular{lll}{ \strong{NAME} \tab \strong{VALUE} \tab \strong{DESCRIPTION} \cr \code{smoother.gaussianwindow.alpha} \tab \code{2.5} \tab Alpha Value in Calculating Window \cr \code{smoother.window} \tab \code{0.1} \tab Width of Window \cr \code{smoother.method} \tab \code{'gaussian'} \tab Default Smoothing Method \cr \code{smoother.tails} \tab \code{FALSE} \tab Include tails in final vector \cr \code{smoother.verbose} \tab \code{FALSE} \tab Verbose Reporting \cr } } \examples{ #Tighten the alpha term for this session. options('smoother.gaussianwindow.alpha' = 1) #Include the Tails in Final Calculation options('smoother.tails' = TRUE) } smoother/DESCRIPTION0000644000176200001440000000130514603135363013623 0ustar liggesusersPackage: smoother Type: Package Title: Functions Relating to the Smoothing of Numerical Data Version: 1.3 Date: 2024-04-03 Author: Nicholas Hamilton Maintainer: Nicholas Hamilton Description: A collection of methods for smoothing numerical data, commencing with a port of the Matlab gaussian window smoothing function. In addition, several functions typically used in smoothing of financial data are included. License: GPL-2 Depends: TTR(>= 0.22) Collate: 'onLoad.R' 'smoother-package.R' 'functions.R' 'smth-gaussian.R' 'smth.R' RoxygenNote: 7.3.1 NeedsCompilation: no Packaged: 2024-04-02 23:09:56 UTC; nick Repository: CRAN Date/Publication: 2024-04-03 02:12:03 UTC smoother/NEWS0000644000176200001440000000324514603107766012627 0ustar liggesuserssmoother 1.3 ---------------------------------------------------------------- * Fix some documentation errors * Change maintainer email address smoother 1.1 ---------------------------------------------------------------- * Added alpha ('smoother.gaussianwindow.alpha') option which can be modified globally (default 2.5) * Added window ('smoother.window') option which can be modified globally (default 0.1, which is 10% of data) * Added verbose ('smoother.verbose') option which can be modified globally (default FALSE) * Added method ('smoother.method') option which can be modified globally (default 'gaussian') * Exposed/Exported smth.gaussian(...) function, rather than using as hidden function. It was previously included as .smth.gaussian(...) * Added trivial example to the smth.gaussian(...) function now that it has been exposed * Added elipsis (...) argument to smth function (see ?smth), which can pass variables down the line to the respective specific smoothing techniques. * Removed any sort of specific argument from the helper function (?smth), in favour of using the elipsis. Arguments are then specific to each function (for example, see ?smth.gaussian) * Moved all hidden functions to the functions.R file * Added help file for the global options, see ?smoother.options * Included some basic Moving Average functions (sma, ema, dema, wma), called from the TTR package and wrapped within the smth(...) helper function. * Removed 'donttest' directives in documentation BUG FIXES * Fixed typo (x not X) in the example provided in the ?smth help file. smoother 1.0.0.0 ---------------------------------------------------------------- * Initial Package Version for CRAN Submissionsmoother/R/0000755000176200001440000000000012406142705012314 5ustar liggesuserssmoother/R/functions.R0000644000176200001440000000151412757136037014462 0ustar liggesusers#SOME UTILITY FUNCTIONS FOR USE IN THIS PACKAGE #Function to determine the actual window length from possibly decimal (fractional) window length .determineWindowLength = function (x, w) { if (!is.numeric(x) | !is.numeric(w)) { stop("Arguments 'x' and 'w' must be numeric") } l = length(x) w.orig = w if (w > 0 && w < 1) { w = w * l } ret = as.integer(max(abs(w[1]), 1)) if (length(x) <= ret | ret < 1) { stop(paste("Resultant window length is out of range (", ret, "), must be >= 1", sep = ""), call. = FALSE) } return(ret) } .normalize = function(x){ if (!is.numeric(x)) { stop("argument 'x' must be numeric") } if (length(x) == 1) { return(1) } total = sum(x) if (total == 0) { stop("argument 'x' must not sum to zero") } return(x/total) }smoother/R/smth.R0000644000176200001440000000450714603107427013423 0ustar liggesusers#' Smooth Numerical Data #' #' @description Helper function to smooth numerical data using methods specified by the user. #' #' @details At this moment in time, the only method is the \code{'gaussian'} window function (similar to the Matlab #' Gaussian Window Smoothing Function) and a number of moving averages \code{'sma', 'ema', 'dema'} or \code{'wma'}. #' These are functions that allows the user to smooth an input vector, returning vector of the same length as the input. #' This can also be achieved using the specific \code{\link{smth.gaussian}} function. #' @param x numeric vector of values to smooth #' @param method one of \code{'gaussian', 'sma', 'ema', 'dema'} or \code{'wma'}. #' @param ... any other arguments to be passed to each specific smoothing methodology. #' @return a numeric vector of same length as input \code{'x'} vector #' @references If the \code{'method'} argument is equal to \code{'gaussian'}, then this function is a port of the function #' described here: \url{https://goo.gl/HGM47U}, very loosly based of code which has also been ported to c++ elsewhere #' @seealso Refer to specific man files: \code{\link{smth.gaussian}}, \code{\link[TTR]{SMA}}, \code{\link[TTR]{EMA}}, #' \code{\link[TTR]{DEMA}} or \code{\link[TTR]{WMA}} #' @exportMethod #' @examples #' #Prepare Data #' n = 1000 #' x = seq(-pi,pi,length.out=n) #' y = sin(x) + (runif(length(x))*0.1) #NOISY DATA #' ys = smth(y,window = 0.1,method = "gaussian") #SMOOTHING #' plot(x,y,type="l",col="red") #' lines(x,ys,col="black",lwd=3) #' title("Example Smoothing of Noisy Data") #' @name smth #' @rdname smth #' @aliases smth sma ema dema wma gaussian smth <- function(x = stop("Numeric Vector 'x' is Required"), method = getOption('smoother.method'), ...){ #Local Variables methods.local = c('gaussian') methods.ttr = c('sma','ema','dema','wma') args <- c(list(x),list(...)) #Process if(method %in% methods.local){ return(do.call(paste('smth.',method,sep=""),args)) } else if(method %in% methods.ttr){ return(do.call(toupper(method),args)) #IMPORTED FUNCTIONS FROM TTR PACKAGE. } else { stop(paste("The 'method' argument (",as.character(method),") can only be one of ['", paste(c(methods.local,methods.ttr),collapse="','"),"'] at this moment in time.",sep=""),call.=FALSE) } }smoother/R/smoother-package.R0000644000176200001440000000160114603107560015667 0ustar liggesusers#' Smooth Data in R #' #' @description \code{smoother} Package for the Smoothing of Numerical Data #' #' @details \code{smoother} is presently limited to a port of the Matlab 'Gaussian Window' Function, #' as well as a limited number of moving averages (\code{sma, ema, dema} and \code{'wma'}). Code for the gaussian window #' function has been written locally within this package, however, the moving averages are called from the \link{TTR} package #' (\url{https://CRAN.R-project.org/package=TTR}) and are included as a matter of convenience. #' #' For further information (and examples) with regards to utilizing the primary helper function, #' please refer to the \link{smth} function help file #' #' @references The Gaussian Smoothing component of the \code{smoother} package has been loosley adapted from elsewhere #' #' @name smoother #' @rdname smoother #' @aliases smoother NULL smoother/R/smth-gaussian.R0000644000176200001440000000366512757136233015244 0ustar liggesusers#' Smooth Using Gaussian Window #' #' @description The specific function for smoothing using the gaussian window function #' @param x numeric vector of values to smooth, error will be thrown if not provided. #' @param window the length of the smoothing window, if an integer, represents #' number of items, else, if a value between \code{0} and \code{1}, represents the #' proportion of the input vector #' @param alpha parameter to determine the breadth of the gaussian window, yielding more or less sensitive #' smoothing characteristics #' @param ... not used #' @param tails Logical value as to whether the tail regions should be included or not. #' @name smth.gaussian #' @rdname smth.gaussian #' @examples #' y = runif(100) #' ys = smth.gaussian(y) #' @export smth.gaussian <- function( x = stop("Numeric Vector 'x' is Required"), window = getOption('smoother.window'), alpha = getOption('smoother.gaussianwindow.alpha'), ..., tails = getOption('smoother.tails')){ if (!is.numeric(x) | !is.numeric(alpha)) { stop("argument 'x' and 'alpha' must be numeric", call. = FALSE) } windowLength = .determineWindowLength(x, window) makeWindow = function(w, a) { hw = abs(w/2) e = exp(1) a = abs(a) ret = sapply(c(0:(w - 1)), function(x) { n = x - as.integer(hw) k = -0.5 * (a * n/hw)^2 e^k }) ret } w = makeWindow(windowLength, alpha[1]) sizeW = length(w) sizeD = length(x) w = .normalize(w) hkwL = as.integer(sizeW/2) hkwR = sizeW - hkwL ret = sapply(c(1:sizeD), function(i) { ix.d = c((i - hkwL):(i + hkwR - 1)) ix.w = which(ix.d %in% 1:sizeD) ix.d = ix.d[ix.w] W.nm = if(length(ix.w) != sizeW){.normalize(w[ix.w])}else{w} D.nm = x[ix.d] as.numeric(D.nm %*% W.nm) }) if (!tails) { ret[c(1:hkwL, (sizeD - hkwR + 1):sizeD)] = NA } ret } smoother/R/onLoad.R0000644000176200001440000000226714603106645013666 0ustar liggesusers#' Smoother Options #' #' @description Several Global Options have been declared, as described in this help file. #' @details #' The following global options can be modified, to alter the default calculation behaviour. #' \tabular{lll}{ #' \strong{NAME} \tab \strong{VALUE} \tab \strong{DESCRIPTION} \cr #' \code{smoother.gaussianwindow.alpha} \tab \code{2.5} \tab Alpha Value in Calculating Window \cr #' \code{smoother.window} \tab \code{0.1} \tab Width of Window \cr #' \code{smoother.method} \tab \code{'gaussian'} \tab Default Smoothing Method \cr #' \code{smoother.tails} \tab \code{FALSE} \tab Include tails in final vector \cr #' \code{smoother.verbose} \tab \code{FALSE} \tab Verbose Reporting \cr #' } #' @examples #' #Tighten the alpha term for this session. #' options('smoother.gaussianwindow.alpha' = 1) #' #' #Include the Tails in Final Calculation #' options('smoother.tails' = TRUE) #' #' @rdname smth.options #' @name smth.options NULL .onLoad <- function(libname, pkgname){ options('smoother.gaussianwindow.alpha' = 2.5) options('smoother.window' = 0.1) options('smoother.verbose'= FALSE) options('smoother.method' ='gaussian') options('smoother.tails' = FALSE) }smoother/MD50000644000176200001440000000110114603135363012417 0ustar liggesusers7168cf8881311467a6c1cfb913a4bb90 *DESCRIPTION 1a1c5fbd37d39de72029289bce4dc6b2 *NAMESPACE 759493272ba00bed64f5446910e21322 *NEWS af4c77e7bf6de75c8257b2a0256fd903 *R/functions.R 455049665b863008b25b93e69695f024 *R/onLoad.R f6784f116008e35933282f090f4a381b *R/smoother-package.R 3357a6122f8bbe7b30da59ff9494f663 *R/smth-gaussian.R f0ff46d91929d4f933620a9f939d86c7 *R/smth.R 086309168d84c78c4908bdc11bce7f0a *man/smoother.Rd a9a9cf5d9100a77d78c70e9835d2b8a8 *man/smth.Rd c4adf0167e4515730f984372d4f89a6d *man/smth.gaussian.Rd 08a076acbd3fa7abc28a83157a5ba117 *man/smth.options.Rd