smoother/0000755000175100001440000000000012513655133012134 5ustar hornikuserssmoother/NAMESPACE0000644000175100001440000000022212406236212013341 0ustar hornikusersexportPattern("^[[:alpha:]]+") export(smth.gaussian) export(smth) importFrom(TTR,SMA) importFrom(TTR,EMA) importFrom(TTR,DEMA) importFrom(TTR,WMA)smoother/NEWS0000644000175100001440000000302412513453101012621 0ustar hornikuserssmoother 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/0000755000175100001440000000000012406142705012332 5ustar hornikuserssmoother/R/onLoad.R0000644000175100001440000000231712513355222013673 0ustar hornikusers#' 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 #' @rdname 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/R/smoother-package.R0000644000175100001440000000167312513355632015721 0ustar hornikusers#' 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{http://cran.r-project.org/web/packages/TTR/index.html}) 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 the #' following works: \url{http://goo.gl/NK79bJ}. #' #' @name smoother #' @rdname smoother #' @aliases smoother NULL smoother/R/smth-gaussian.R0000644000175100001440000000500112513355272015240 0ustar hornikusers#' 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')){ #Check Numeric Arguments if(!is.numeric(x) | !is.numeric(alpha)){stop("argument 'x' and 'alpha' must be numeric",call.=FALSE)} #Determine the Window Length windowLength = .determineWindowLength(x,window) #Hidden Gaussian Window Function makeWindow = function(w,a){ hw = abs(w/2.0) #halfwidth e = exp(1) #eulers number a = abs(a) #alpha ret = sapply(c(0:(w - 1)),function(x){ n = x - as.integer(hw) k = -0.5*(a*n/hw)^2 e^k }) ret } #Continue with the Convolustion w = makeWindow(windowLength,alpha[1]) #Length of F & G Vectors sizeW = length(w) sizeD = length(x) #Normalize the window w = .normalize(w) #Prepare hkwL = as.integer(sizeW/2) #Half Kernel Width, for left hkwR = sizeW - hkwL #Half Kernel Width, rhs balance to total width #Now Do the Gaussian Smoothing. ret = sapply(c(1:sizeD),function(i){ ix.d = c((i-hkwL):(i+hkwR-1)) #The Ideal Range ix.w = which(ix.d %in% 1:sizeD) #Indexes of Values, Window ix.d = ix.d[ix.w] #Indexes of Values, Data #Determine Normalized Final Window and Data W.nm = .ifthenelse(length(ix.w) != sizeW,.normalize(w[ix.w]),w) #Re-Normalize if Needed D.nm = x[ix.d] #Dot Product as.numeric(D.nm %*% W.nm) }) #Remove Tail Regions if(!tails){ret[c(1:hkwL,(sizeD - hkwR + 1):sizeD)] = NA} #Done ret } smoother/R/functions.R0000644000175100001440000000207112406172660014470 0ustar hornikusers#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")} #Vars l = length(x) w.orig = w #If Fractional if(w > 0 && w < 1){w = w*l} #Positive Integer not less than 1 ret = as.integer(max(abs(w[1]),1)) #Just in case. if(length(x) <= ret | ret < 1){ stop(paste("Resultant window length is out of range (",ret,"), must be >= 1",sep=""),call.=FALSE) } #Done return(ret) } #Ifthenelse function .ifthenelse <- function(t,y,n){if(t){y}else{n}} #Hidden Normalization Function, sums weights to 1. .normalize <- function(x){ if(!is.numeric(x)){stop("argument 'x' must be numeric")} #If Ony One Value, return unity. if(length(x) == 1){return(1)} #Else determine the total total = sum(x) #Divide by Zero Error Check if(total == 0){stop("argument 'x' must not sum to zero")} #Done return(x/total) }smoother/R/smth.R0000644000175100001440000000454012513356121013431 0ustar hornikusers#' 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{http://goo.gl/HGM47U}, very loosly based of code which has also been ported to c++ here: #' \url{http://goo.gl/NK79bJ} #' @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/MD50000644000175100001440000000110112513655133012435 0ustar hornikusers9b92049989d1e9fbe5c48aaa19cee66f *DESCRIPTION 1a1c5fbd37d39de72029289bce4dc6b2 *NAMESPACE 11116be8c522144f5859739204a2e44f *NEWS 620320a904fbbf15da6037c6e8bf1764 *R/functions.R 5847b66e4a8deabf6b9653676369d735 *R/onLoad.R d7cc316b6c054f0c1c03d1ac60b12f8d *R/smoother-package.R cc45ad57c7c804d79bf1c37e71391611 *R/smth-gaussian.R 0ea12ff4fef8c5995e04c50143743e35 *R/smth.R 6f6aca14e34e23c279573225ec6aedf4 *man/smoother.Rd 8a8998702b296e96e732a6104cdfc900 *man/smth.Rd ac3a57cd5853e168c0f593a815b186d8 *man/smth.gaussian.Rd 3d17b0baf247a623fa835d09bcc07109 *man/smth.options.Rd smoother/DESCRIPTION0000644000175100001440000000125612513655133013646 0ustar hornikusersPackage: smoother Type: Package Title: Functions Relating to the Smoothing of Numerical Data Version: 1.1 Date: 2015-04-15 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' Packaged: 2015-04-15 21:41:45 UTC; nick NeedsCompilation: no Repository: CRAN Date/Publication: 2015-04-16 08:44:43 smoother/man/0000755000175100001440000000000012406244061012702 5ustar hornikuserssmoother/man/smth.options.Rd0000644000175100001440000000160712513355400015641 0ustar hornikusers\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/man/smth.gaussian.Rd0000644000175100001440000000172412513355400015760 0ustar hornikusers\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/smoother.Rd0000644000175100001440000000157012513355633015043 0ustar hornikusers\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{http://cran.r-project.org/web/packages/TTR/index.html}) 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 the following works: \url{http://goo.gl/NK79bJ}. } smoother/man/smth.Rd0000644000175100001440000000333412513356127014155 0ustar hornikusers\name{smth} \alias{dema} \alias{ema} \alias{gaussian} \alias{sma} \alias{smth} \alias{wma} \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{http://goo.gl/HGM47U}, very loosly based of code which has also been ported to c++ here: \url{http://goo.gl/NK79bJ} } \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}} }