Skip to content
Permalink
Browse files

0.00.0004 try 2

  • Loading branch information...
fcbarbi committed May 3, 2016
1 parent a328442 commit 8ad973cbb0f4974f71f9e884e8f1f1854f8700f4
Showing with 44 additions and 49 deletions.
  1. +5 −5 DESCRIPTION
  2. +7 −2 NAMESPACE
  3. +3 −1 R/dlog.R
  4. +11 −20 R/tsNumToIndex.R
  5. +4 −3 R/tsVarToIndex.R
  6. +0 −11 R/tsworkflow.R
  7. +7 −3 R/urTable.R
  8. +7 −4 man/urTable.Rd
@@ -1,13 +1,13 @@
Package: tsworkflow
Title: Functions to streamline workflow in time series analysis
Version: 0.000.002
Package: macror
Title: Functions for Macroeconomic time series analysis in R
Version: 0.00.004
Authors@R: person("Fernando", "Barbi", email = "fcbarbi@gmail.com", role = c("aut", "cre"))
Author: Fernando Barbi [aut,cre]
Maintainer: Fernando Barbi <fcbarbi@gmail.com>
Description: Functions for handling ts (time series) and zoo objects to: create index from percentage variation, create dummy series, complete series and generate Eviews-formatted dates.
URL: https://github.com/fcbarbi/tsworkflow
URL: http://www.macror.org
Depends: R (>= 3.2.4)
Imports: stats, zoo
Imports: stats, zoo, tseries, xtable
Suggests: testthat
License: GPL (>=2)
LazyData: true
@@ -2,9 +2,14 @@

export(dlog)
export(genEVtime)
export(numToIndex)
export(pVarToIndex)
export(tsComplete)
export(tsDummy)
export(tsMapOutliers)
export(tsNumToIndex)
export(tsVarToIndex)
export(urTable)
importFrom(stats,quantile)
importFrom(tseries,adf.test)
importFrom(tseries,kpss.test)
importFrom(tseries,pp.test)
importFrom(xtable,xtable)
@@ -13,7 +13,7 @@
# @importFrom zoo iszoo
# @importFrom zoo zooreg
#'
#' @seealso \code{\link{pVarToIndex}}
#' @seealso \code{\link{tsVarToIndex}}
#'
#' @examples
#' CPI <- ts( rnorm(10,mean=100) )
@@ -56,10 +56,12 @@ dlog <- function( data, lag = 1 ) {
# dpx <- deltaPX(x)
# all(abs(dlx-dpx)<1e-3)
deltaPX <- function(x,lag=1,fillup=NA) {

dx <- rep(fillup,length(x))
cx <- zoo::coredata(x)
for (i in (lag+1):length(x))
dx[i] <- (cx[i]-cx[i-lag])/cx[i-lag]

if (is.ts(x)) res <- ts( data=dx, start=start(x),frequency=frequency(x))
if (zoo::is.zoo(x)) res <- zoo::zooreg( data=dx, start=start(x),frequency=frequency(x))
res
@@ -1,5 +1,5 @@
#' @title Converts a numeric series to an index using the first observation as reference.
#' @name numToIndex
#' @name tsNumToIndex
#'
#' @description Converts a numeric series to an index using the first observation as reference.
#' @details If using a monetary series be sure to deflate it before indexing.
@@ -10,38 +10,29 @@
#' @return Time series (ts or zoo) with the index.
#'
#' @export
#' @seealso \code{\link{pVarToIndex}}
#' @seealso \code{\link{tsVarToIndex}}
#' @examples
#' # Production averages 1254 units per month
#' production <- ts( rnorm(30*12,mean=1254,sd=425),start=c(1980,1),freq=12)
#' ip <- numToIndex( production ) # industrial production index
#' ip <- tsNumToIndex( production ) # industrial production index
#' ip2 <- tsNumToIndex( production, reference = c(1992,6) ) # rebase for June 1992
#
# implements 'reference' but uses a proportional rule to calculate the result
numToIndex <- function( x, base=100, reference=1 ) {
tsNumToIndex <- function( x, base=100, reference=1 ) {

if (!is.ts(x) & !zoo::is.zoo(x))
stop("toIndex must be called with a ts or zoo object.")
stop("tsNumToIndex must be called with a ts or zoo object.")
if (!is.vector(reference) | !is.numeric(reference))
stop("reference is the numeric position in x or a date as in c(2008,1).")

# if (zoo::is.zoo(x)) {
# index_x <- zoo::index(x)
# frequency_x <- zoo::frequency(x)
# }
# else {
index_x <- zoo::index(x)
frequency_x <- frequency(x)
# }

position <- 1L
if (is.null(reference))
position <- which(x==min(x))
else {
if (length(reference)>=2L) {
# is the reference date inside x ?
refnum <- reference[1]+(reference[2]-1)/frequency_x
position <- which(abs(refnum-index_x)<1e-6)
#position <- window(x,start=reference,end=reference)
refnum <- reference[1]+(reference[2]-1)/frequency(x)
position <- which(abs(refnum-zoo::index(x))<1e-6)
if (length(position)==0L) {
warning("reference out of range, assuming the first")
position <- 1L
@@ -59,10 +50,10 @@ numToIndex <- function( x, base=100, reference=1 ) {
refx <- zoo::coredata(refx)

res <- base*(1+(x-refx)/refx)
res <- round( res,4 )
res <- round( res,4 ) # rounding to 4 decimal places
if (is.ts(x))
res <- ts( data=res, start=start(x),frequency=frequency(x))
res <- ts( data=res, start=start(x),frequency=frequency(x) )
else
res <- zoo::zooreg( data=res, start=start(x),frequency=frequency(x))
res <- zoo::zooreg( data=res, start=start(x),frequency=frequency(x) )
res
}
@@ -26,12 +26,13 @@
tsVarToIndex <- function( x, base=100, reference=1 ) {

if (!is.ts(x) & !zoo::is.zoo(x))
stop("toIndex must be called with a ts or zoo object.")
stop("tsVarToIndex must be called with a ts or zoo object.")
if (!is.vector(reference) | !is.numeric(reference))
stop("reference is the numeric position in x or a date as in c(2008,1).")

res <- base*c(1,cumprod(1+x[-length(x)]/100))
res <- round( res,4 ) # round to 4 decimal places
xc <- zoo::coredata(x)
res <- base*c(1,cumprod(1+xc[-length(x)]/100))
res <- round( res,4 ) # rounding to 4 decimal places
if (is.ts(x))
res <- ts( data=res, start=start(x),frequency=frequency(x))
else

This file was deleted.

@@ -7,8 +7,9 @@
#' @details Supports ADF, PP and KPSS unit root tests.
#'
#' @param df Dataframe with series to test
#' @param order Maximum order of integration, I(?), to test starting from I(0). It can be\code{c(0,1,2)}.
#' @param structure List of models to test, can be \code{c("c","ct","nc")}: \code{"c"} assumes model with intercept,"ct" assumes intercept and deterministic trend,"nc" assumes no intercept and no trend.
#' @param tests List of tests to be performed, by default tests are \code{c("adf","pp","kpss")}.
#' @param order Maximum order of integration to test, usually noted as I(?). It can be \code{c(0,1,2)}.
# @param structure List of models to test, can be \code{c("c","ct","nc")}: \code{"c"} assumes model with intercept,"ct" assumes intercept and deterministic trend,"nc" assumes no intercept and no trend.
#' @param file (Optional) Filename to be generated according to \code{format}.
#' @param format (Optional) Format of the output table, can be \code{c("csv","latex")}. Defaults to \code{"txt"}.
#'
@@ -21,7 +22,10 @@
#' @export
#'
#' @examples
#' data <- data.frame( USeconomic ) #from tseries
#'
#' require(tseries)
#' data(USeconomic,package="tseries")
#' data <- data.frame( USeconomic )
#' dft1 <- urTable( data, tests=c("adf","pp","kpss"), file="US.csv" )
#' dft2 <- urTable( data, tests=c("pp","kpss"), order=2, file="US.tex", format="latex" )
#'

Some generated files are not rendered by default. Learn more.

0 comments on commit 8ad973c

Please sign in to comment.
You can’t perform that action at this time.