[Returnanalytics-commits] r3553 - in pkg/FactorAnalytics: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Nov 19 11:00:09 CET 2014


Author: pragnya
Date: 2014-11-19 11:00:09 +0100 (Wed, 19 Nov 2014)
New Revision: 3553

Added:
   pkg/FactorAnalytics/R/CornishFisher.R
Removed:
   pkg/FactorAnalytics/R/dCornishFisher.R
   pkg/FactorAnalytics/R/pCornishFisher.R
   pkg/FactorAnalytics/R/qCornishFisher.R
   pkg/FactorAnalytics/R/rCornishFisher.R
Modified:
   pkg/FactorAnalytics/DESCRIPTION
   pkg/FactorAnalytics/R/fitTsfm.R
   pkg/FactorAnalytics/R/paFm.r
   pkg/FactorAnalytics/R/plot.pafm.r
   pkg/FactorAnalytics/R/plot.tsfm.r
   pkg/FactorAnalytics/R/print.pafm.r
   pkg/FactorAnalytics/R/summary.pafm.r
   pkg/FactorAnalytics/R/summary.tsfm.r
   pkg/FactorAnalytics/man/CornishFisher.Rd
   pkg/FactorAnalytics/man/fitTsfm.Rd
Log:
Better handling of some dependencies (import vs depends), combined CornishFisher functions into one script

Modified: pkg/FactorAnalytics/DESCRIPTION
===================================================================
--- pkg/FactorAnalytics/DESCRIPTION	2014-11-15 22:54:45 UTC (rev 3552)
+++ pkg/FactorAnalytics/DESCRIPTION	2014-11-19 10:00:09 UTC (rev 3553)
@@ -16,17 +16,17 @@
     are included. 
 License: GPL-2
 Depends:
-    R (>= 2.14.0),
-    robust,
-    leaps,
+    R (>= 3.0.0),
+    xts (>= 0.9),
+    PerformanceAnalytics(>= 1.1.0),
+    lmtest,
+    sandwich
+Imports: 
+    corrplot,  
+    robust, 
+    leaps, 
     lars,
-    lmtest,
-    PerformanceAnalytics (>= 1.1.0),
-    sn,
-    tseries,
-    strucchange,
-    ellipse
-Imports: corrplot
+    strucchange
 Suggests:
     testthat, quantmod, knitr
 LazyLoad: yes

Added: pkg/FactorAnalytics/R/CornishFisher.R
===================================================================
--- pkg/FactorAnalytics/R/CornishFisher.R	                        (rev 0)
+++ pkg/FactorAnalytics/R/CornishFisher.R	2014-11-19 10:00:09 UTC (rev 3553)
@@ -0,0 +1,114 @@
+#' @title Density, distribution function, quantile function and random 
+#' generation for the Cornish-Fisher distribution.
+#'
+#' @details CDF(q) = Pr(sqrt(n)*(x_bar-mu)/sigma < q)
+#' \itemize{
+#' \item \code{dCornishFisher} Computes Cornish-Fisher density from two term 
+#' Edgeworth expansion given mean, standard deviation, skewness and excess 
+#' kurtosis.
+#' \item \code{pCornishFisher} Computes Cornish-Fisher CDF from two term 
+#' Edgeworth expansion given mean, standard deviation, skewness and excess 
+#' kurtosis.
+#' \item \code{qCornishFisher} Computes Cornish-Fisher quantiles from two term 
+#' Edgeworth expansion given mean, standard deviation, skewness and excess 
+#' kurtosis.
+#' \item \code{rCornishFisher} simulate observations based on Cornish-Fisher 
+#' quantile expansion given mean, standard deviation, skewness and excess 
+#' kurtosis.}
+#'
+#' @param n scalar; number of simulated values in random simulation, sample 
+#' length in density, distribution and quantile functions.
+#' @param sigma scalar standard deviation.
+#' @param skew scalar; skewness.
+#' @param ekurt scalar; excess kurtosis.
+#' @param seed scalar; set seed. Default is \code{NULL}.
+#' @param x,q vector of standardized quantiles.
+#' @param p vector of probabilities.
+#' 
+#' @return 
+#' \code{dCornishFisher} gives the density, \code{pCornishFisher} gives the 
+#' distribution function, \code{qCornishFisher} gives the quantile function, 
+#' and \code{rCornishFisher} generates \code{n} random simulations.
+#' 
+#' @author Eric Zivot and Yi-An Chen.
+#' 
+#' @references 
+#' DasGupta, A. (2008). Asymptotic theory of statistics and probability. 
+#' Springer.
+#' Severini, T. A., (2000). Likelihood Methods in Statistics. Oxford University 
+#' Press.
+#'  
+#' @examples
+#' \dontrun{
+#' # generate 1000 observation from Cornish-Fisher distribution
+#' rc <- rCornishFisher(1000,1,0,5)
+#' hist(rc, breaks=100, freq=FALSE, 
+#'      main="simulation of Cornish Fisher Distribution", xlim=c(-10,10))
+#' lines(seq(-10,10,0.1), dnorm(seq(-10,10,0.1), mean=0, sd=1), col=2)
+#' # compare with standard normal curve
+#'
+#' # exponential example from A.dasGupta p.188
+#' # x is iid exp(1) distribution, sample size = 5
+#' # then x_bar is Gamma(shape=5, scale=1/5) distribution
+#' q <- c(0,0.4,1,2)
+#' # exact cdf
+#' pgamma(q/sqrt(5)+1, shape=5, scale=1/5)
+#' # use CLT
+#' pnorm(q)
+#' # use edgeworth expansion
+#' pCornishFisher(q, n=5, skew=2, ekurt=6)
+#' }
+#'
+#' @rdname CornishFisher
+#' @export
+
+dCornishFisher <- function(x, n,skew, ekurt) {
+  density <- dnorm(x) + 
+    1/sqrt(n)*(skew/6*(x^3 - 3*x))*dnorm(x) +
+    1/n*( (skew)^2/72*(x^6 - 15*x^4 + 45*x^2 - 15) + ekurt/24*(x^4 - 6*x^2 + 3) )*dnorm(x)
+  return(density)
+}
+
+#' @rdname CornishFisher
+#' @export
+
+pCornishFisher <- function(q, n, skew, ekurt) {
+  zq <- q 
+  CDF <- pnorm(zq) + 
+    1/sqrt(n)*(skew/6 * (1-zq^2))*dnorm(zq) + 
+    1/n*( (ekurt)/24*(3*zq-zq^3) + (skew)^2/72*(10*zq^3-15*zq-zq^5) )*dnorm(zq)
+  return(CDF)
+}
+
+#' @rdname CornishFisher
+#' @export
+
+qCornishFisher <- function(p,n,skew, ekurt) {
+  zq <- qnorm(p)
+  q.cf <- zq  + 
+    1/sqrt(n)*(((zq^2 - 1) * skew)/6) + 
+    1/n*( (((zq^3 - 3*zq) * ekurt)/24) - ((((2*zq^3) - 5*zq) * skew^2)/36) )
+  return(q.cf) 
+}
+
+#' @rdname CornishFisher
+#' @export
+
+rCornishFisher <- function(n, sigma, skew, ekurt, seed=NULL) {
+  
+  ## inputs:
+  ## n          scalar, number of simulated values
+  ## sigma      scalar, standard deviation
+  ## skew       scalar, skewness
+  ## ekurt      scalar, excess kurtosis
+  ## outputs:
+  ## n simulated values from Cornish-Fisher distribution
+  
+  if (!is.null(seed)) set.seed(seed)
+  zc <- rnorm(n)
+  z.cf <- zc + 
+    (((zc^2 - 1) * skew)/6) + 
+    (((zc^3 - 3*zc)*ekurt)/24) - 
+    ((((2*zc^3) - 5*zc)*skew^2)/36)
+  return(sigma*z.cf)
+}

Deleted: pkg/FactorAnalytics/R/dCornishFisher.R
===================================================================
--- pkg/FactorAnalytics/R/dCornishFisher.R	2014-11-15 22:54:45 UTC (rev 3552)
+++ pkg/FactorAnalytics/R/dCornishFisher.R	2014-11-19 10:00:09 UTC (rev 3553)
@@ -1,15 +0,0 @@
-#'@name  CornishFisher
-#'@aliases CornishFisher
-#'@aliases rCornishFisher 
-#'@aliases dCornishFisher
-#'@aliases qCornishFisher
-#'@aliases pCornishFisher
-#' @export
-dCornishFisher <-
-function(x, n,skew, ekurt) {
-
-density <- dnorm(x) + 1/sqrt(n)*(skew/6*(x^3-3*x))*dnorm(x) +
-    1/n *( (skew)^2/72*(x^6 - 15*x^4 + 45*x^2 -15) + ekurt/24 *(x^4-6*x^2+3) )*dnorm(x)
-return(density)
-}
-

Modified: pkg/FactorAnalytics/R/fitTsfm.R
===================================================================
--- pkg/FactorAnalytics/R/fitTsfm.R	2014-11-15 22:54:45 UTC (rev 3552)
+++ pkg/FactorAnalytics/R/fitTsfm.R	2014-11-19 10:00:09 UTC (rev 3553)
@@ -15,7 +15,7 @@
 #' Estimation method "OLS" corresponds to ordinary least squares using 
 #' \code{\link[stats]{lm}}, "DLS" is discounted least squares (weighted least 
 #' squares with exponentially declining weights that sum to unity), and, 
-#' "Robust" is robust regression (useing \code{\link[robust]{lmRob}}). 
+#' "Robust" is robust regression (using \code{\link[robust]{lmRob}}). 
 #' 
 #' If \code{variable.selection="none"}, all chosen factors are used in the 
 #' factor model. Whereas, "stepwise" performs traditional forward/backward 
@@ -261,7 +261,7 @@
     reg.list <- SelectStepwise(dat.xts, asset.names, factor.names, fit.method, 
                                lm.args, lmRob.args, step.args, decay)
   } else if (variable.selection == "subsets") {
-    reg.list <- SelectAllSubsets(dat.xts, asset.names, factor.names,fit.method, 
+    reg.list <- SelectAllSubsets(dat.xts, asset.names, factor.names, fit.method, 
                                  lm.args, lmRob.args, regsubsets.args, 
                                  subset.size, decay)
   } else if (variable.selection == "lars") {
@@ -317,8 +317,8 @@
       lm.args$weights <- WeightsDLS(nrow(reg.xts), decay)
       reg.list[[i]] <- do.call(lm, c(list(fm.formula,data=reg.xts),lm.args))
     } else if (fit.method == "Robust") {
-      reg.list[[i]] <- do.call(lmRob, c(list(fm.formula,data=reg.xts),
-                                        lmRob.args))
+      reg.list[[i]] <- do.call(robust::lmRob, c(list(fm.formula,data=reg.xts),
+                                                lmRob.args))
     } 
   } 
   reg.list  
@@ -349,8 +349,10 @@
       lm.fit <- do.call(lm, c(list(fm.formula,data=reg.xts),lm.args))
       reg.list[[i]] <- do.call(step, c(list(lm.fit),step.args))
     } else if (fit.method == "Robust") {
-      lmRob.fit <- do.call(lmRob, c(list(fm.formula,data=reg.xts),lmRob.args))
-      reg.list[[i]] <- do.call(step.lmRob, c(list(lmRob.fit),step.args))
+      lmRob.fit <- do.call(robust::lmRob, c(list(fm.formula,data=reg.xts), 
+                                            lmRob.args))
+      reg.list[[i]] <- do.call(robust::step.lmRob, c(list(lmRob.fit), 
+                                                     step.args))
     } 
   }
   reg.list
@@ -379,8 +381,8 @@
     }
     
     # choose best subset of factors depending on specified subset size
-    fm.subsets <- do.call(regsubsets, c(list(fm.formula,data=reg.xts),
-                                        regsubsets.args))
+    fm.subsets <- do.call(leaps::regsubsets, c(list(fm.formula,data=reg.xts),
+                                               regsubsets.args))
     sum.sub <- summary(fm.subsets)
     names.sub <- names(which(sum.sub$which[as.character(subset.size),-1]==TRUE))
     reg.xts <- na.omit(dat.xts[,c(i,names.sub)])
@@ -392,8 +394,8 @@
       lm.args$weights <- WeightsDLS(nrow(reg.xts), decay)
       reg.list[[i]] <- do.call(lm, c(list(fm.formula,data=reg.xts),lm.args))
     } else if (fit.method == "Robust") {
-      reg.list[[i]] <- do.call(lmRob, c(list(fm.formula,data=reg.xts),
-                                        lmRob.args))
+      reg.list[[i]] <- do.call(robust::lmRob, c(list(fm.formula,data=reg.xts), 
+                                                lmRob.args))
     } 
   }
   reg.list
@@ -422,11 +424,12 @@
     # convert to matrix
     reg.mat <- as.matrix(reg.xts)
     # fit lars regression model
-    lars.fit <- 
-      do.call(lars, c(list(x=reg.mat[,factor.names],y=reg.mat[,i]),lars.args))
+    lars.fit <- do.call(lars::lars, c(list(x=reg.mat[,factor.names],
+                                           y=reg.mat[,i]),lars.args))
     lars.sum <- summary(lars.fit)
-    lars.cv <- do.call(cv.lars, c(list(x=reg.mat[,factor.names],y=reg.mat[,i], 
-                                       mode="step"),cv.lars.args))
+    lars.cv <- 
+      do.call(lars::cv.lars, c(list(x=reg.mat[,factor.names],y=reg.mat[,i],
+                                    mode="step"),cv.lars.args))
     # including plot.it=FALSE to cv.lars strangely gives an error: "Argument s 
     # out of range". And, specifying index=seq(nrow(lars.fit$beta)-1) resolves 
     # the issue, but care needs to be taken for small N

Deleted: pkg/FactorAnalytics/R/pCornishFisher.R
===================================================================
--- pkg/FactorAnalytics/R/pCornishFisher.R	2014-11-15 22:54:45 UTC (rev 3552)
+++ pkg/FactorAnalytics/R/pCornishFisher.R	2014-11-19 10:00:09 UTC (rev 3553)
@@ -1,16 +0,0 @@
-#'@name  CornishFisher
-#'@aliases CornishFisher
-#'@aliases rCornishFisher 
-#'@aliases dCornishFisher
-#'@aliases qCornishFisher
-#'@aliases pCornishFisher
-#' @export 
-
-pCornishFisher <-
-function(q,n,skew, ekurt) {
-zq = q 
-CDF = pnorm(zq)  +   1/sqrt(n) *(skew/6 * (1-zq^2))*dnorm(zq) + 
-     1/n *( (ekurt)/24*(3*zq-zq^3)+ (skew)^2/72*(10*zq^3 - 15*zq -zq^5))*dnorm(zq)
-return(CDF)
-}
-

Modified: pkg/FactorAnalytics/R/paFm.r
===================================================================
--- pkg/FactorAnalytics/R/paFm.r	2014-11-15 22:54:45 UTC (rev 3552)
+++ pkg/FactorAnalytics/R/paFm.r	2014-11-19 10:00:09 UTC (rev 3553)
@@ -105,7 +105,6 @@
   
   if (class(fit)=="ffm" ) {
     # if benchmark is provided
-    #       
     #       if (!is.null(benchmark)) {
     #         stop("use fitFundamentalFactorModel instead")
     #       }

Modified: pkg/FactorAnalytics/R/plot.pafm.r
===================================================================
--- pkg/FactorAnalytics/R/plot.pafm.r	2014-11-15 22:54:45 UTC (rev 3552)
+++ pkg/FactorAnalytics/R/plot.pafm.r	2014-11-19 10:00:09 UTC (rev 3553)
@@ -41,8 +41,8 @@
 #' @export
 #' 
 plot.pafm <- function(x, which.plot=c("none","1L","2L","3L"),max.show=6,
-                                date=NULL,plot.single=FALSE,fundName,
-                                which.plot.single=c("none","1L","2L","3L"),...) {
+                      date=NULL,plot.single=FALSE,fundName,
+                      which.plot.single=c("none","1L","2L","3L"),...) {
   # ... for  chart.TimeSeries
   if (is.null(date)){
     date = index(x[[3]][[1]])[1]
@@ -54,24 +54,26 @@
     which.plot.single<-which.plot.single[1]
     
     if (which.plot.single=="none")
-      which.plot.single<-menu(c("attributed cumulative returns",
-                                paste("attributed returns","on",date,sep=" "),
-                                "Time series of attributed returns"),
-                              title="performance attribution plot \nMake a plot selection (or 0 to exit):\n")
+      which.plot.single <- menu(c("attributed cumulative returns",
+                                  paste("attributed returns","on",date,sep=" "),
+                                  "Time series of attributed returns"), 
+                                title="performance attribution plot 
+                                \nMake a plot selection (or 0 to exit):\n")
     switch(which.plot.single,
-           "1L" =  {  
+           "1L" = {  
              bar <- c(x$cum.spec.ret[fundName],x$cum.ret.attr.f[fundName,])
              names(bar)[1] <- "specific.returns"
-             barplot(bar,horiz=TRUE,main="cumulative attributed returns",las=1)
+             barplot(bar, horiz=TRUE, main="cumulative attributed returns", las=1)
            },
            "2L" ={
              bar <- coredata(x$attr.list[[fundName]][as.Date(date)])
-             tryCatch( {barplot(bar,horiz=TRUE,main=fundName,las=1)
-                        },error=function(e){cat("\nthis date is not available for this assets.\n")})
+             tryCatch({barplot(bar, horiz=TRUE, main=fundName, las=1)},
+                      error=function(e){cat("\this date is not available for this asset.\n")})
            },
            "3L" = {
              chart.TimeSeries(x$attr.list[[fundName]],
-                              main=paste("Time series of attributed returns of ",fundName,sep=""),... )
+                              main=paste("Time series of attributed returns of", 
+                                         fundName, sep=" "), ...)
            },
            invisible())
   }
@@ -82,13 +84,14 @@
     n <- length(fundnames)
     
     if(which.plot=='none') 
-      which.plot<-menu(c("attributed cumulative returns",
-                         paste("attributed returns","on",date,sep=" "),
-                         "time series of attributed returns"),
-                       title="performance attribution plot \nMake a plot selection (or 0 to exit):\n") 
+      which.plot <- menu(c("attributed cumulative returns",
+                           paste("attributed returns","on",date,sep=" "),
+                           "time series of attributed returns"),
+                         title="performance attribution plot 
+                         \nMake a plot selection (or 0 to exit):\n") 
     if (n >= max.show) {
-      cat(paste("numbers of assets are greater than",max.show,", show only first",
-                max.show,"assets",sep=" "))
+      cat(paste("numbers of assets are greater than ", max.show, 
+                "; showing only first ", max.show, " assets.", sep=""))
       n <- max.show 
     }
     switch(which.plot,
@@ -96,9 +99,9 @@
            "1L" = {
              par(mfrow=c(2,n/2))
              for (i in fundnames[1:n]) {
-               bar <- c(x$cum.spec.ret[i],x$cum.ret.attr.f[i,])
+               bar <- c(x$cum.spec.ret[i], x$cum.ret.attr.f[i,])
                names(bar)[1] <- "specific.returns"
-               barplot(bar,horiz=TRUE,main=i,las=1)  
+               barplot(bar, horiz=TRUE, main=i, las=1)  
              }
              par(mfrow=c(1,1))
            },
@@ -106,24 +109,23 @@
              par(mfrow=c(2,n/2))
              for (i in fundnames[1:n]) {
                tryCatch({
-               bar <- coredata(x$attr.list[[i]][as.Date(date)])
-               barplot(bar,horiz=TRUE,main=i,las=1)
+                 bar <- coredata(x$attr.list[[i]][as.Date(date)])
+                 barplot(bar, horiz=TRUE, main=i, las=1)
                }, error=function(e) {
-                cat("\nDate for some assets returns is not available.\n")
-                dev.off()
-                } )
-               }
+                 cat("\nDate for some assets returns is not available.\n")
+                 dev.off()
+               } )
+             }
              par(mfrow=c(1,1))
            }, 
            "3L" = {
              par(mfrow=c(2,n/2))
              for (i in fundnames[1:n]) {
-               chart.TimeSeries(x$attr.list[[i]],main=i,...)
+               chart.TimeSeries(x$attr.list[[i]], main=i, ...)
              }
              par(mfrow=c(1,1))
            },     
            invisible()
     )
-    
   }
 }

Modified: pkg/FactorAnalytics/R/plot.tsfm.r
===================================================================
--- pkg/FactorAnalytics/R/plot.tsfm.r	2014-11-15 22:54:45 UTC (rev 3552)
+++ pkg/FactorAnalytics/R/plot.tsfm.r	2014-11-19 10:00:09 UTC (rev 3553)
@@ -219,7 +219,8 @@
                if (!x$fit.method=="OLS") {
                  stop("CUSUM analysis applicable only for 'OLS' fit.method.")
                }
-               cusum.rec <- efp(formula(fit), type="Rec-CUSUM", data=fit$model)
+               cusum.rec <- strucchange::efp(formula(fit), type="Rec-CUSUM", 
+                                             data=fit$model)
                plot(cusum.rec, main=paste("Recursive CUSUM test:",i), las=las, 
                     col=colorset, ...)
              }, "11L" = {
@@ -227,7 +228,8 @@
                if (!x$fit.method=="OLS") {
                  stop("CUSUM analysis applicable only for 'OLS' fit.method.")
                }
-               cusum.ols <- efp(formula(fit), type="OLS-CUSUM", data=fit$model)
+               cusum.ols <- strucchange::efp(formula(fit), type="OLS-CUSUM", 
+                                             data=fit$model)
                plot(cusum.ols, main=paste("OLS-based CUSUM test:",i), las=las, 
                     col=colorset, ...)
              }, "12L" = {
@@ -235,7 +237,8 @@
                if (!x$fit.method=="OLS") {
                  stop("CUSUM analysis applicable only for 'OLS' fit.method.")
                }        
-               cusum.est <- efp(formula(fit), type="RE", data=fit$model)
+               cusum.est <- strucchange::efp(formula(fit), type="RE", 
+                                             data=fit$model)
                plot(cusum.est, functional=NULL, col=colorset, las=0,
                     main=paste("RE test (Recursive estimates test):",i), ...)
              }, "13L" = {
@@ -266,7 +269,8 @@
                                         width=24, by.column=FALSE, align="right")
                } else if (x$fit.method=="Robust") {
                  rollReg.Rob <- function(data.z, formula) {
-                   coef(lmRob(formula=formula, data=as.data.frame(data.z)))  
+                   coef(robust::lmRob(formula=formula, 
+                                      data=as.data.frame(data.z)))  
                  }
                  reg.z <- zoo(fit$model, as.Date(rownames(fit$model)))
                  rollReg.z <- rollapply(reg.z, width=24, FUN=rollReg.Rob, 

Modified: pkg/FactorAnalytics/R/print.pafm.r
===================================================================
--- pkg/FactorAnalytics/R/print.pafm.r	2014-11-15 22:54:45 UTC (rev 3552)
+++ pkg/FactorAnalytics/R/print.pafm.r	2014-11-19 10:00:09 UTC (rev 3553)
@@ -20,8 +20,8 @@
 #' @method print pafm  
 #' @export   
 #' 
-print.pafm <- function(x,...) {
+print.pafm <- function(x, ...) {
   cat("\nMean of returns attributed to factors
       \n")
-  print(sapply(x[[3]],function(x) apply(x,2,mean)))
+  print(sapply(x[[3]], function(x) apply(x,2,mean)))
  }

Deleted: pkg/FactorAnalytics/R/qCornishFisher.R
===================================================================
--- pkg/FactorAnalytics/R/qCornishFisher.R	2014-11-15 22:54:45 UTC (rev 3552)
+++ pkg/FactorAnalytics/R/qCornishFisher.R	2014-11-19 10:00:09 UTC (rev 3553)
@@ -1,18 +0,0 @@
-#'@name  CornishFisher
-#'@aliases CornishFisher
-#'@aliases rCornishFisher 
-#'@aliases dCornishFisher
-#'@aliases qCornishFisher
-#'@aliases pCornishFisher
-#' @export 
-
-qCornishFisher <-
-function(p,n,skew, ekurt) {
-zq = qnorm(p)
-q.cf = zq  + 1/sqrt(n)* (((zq^2 - 1) * skew)/6) + 1/n*((((zq^3 - 3 * zq) *
-      ekurt)/24) - ((((2 * zq^3) - 5 * zq) * skew^2)/36) )
-return(q.cf)
-  
-  
-}
-

Deleted: pkg/FactorAnalytics/R/rCornishFisher.R
===================================================================
--- pkg/FactorAnalytics/R/rCornishFisher.R	2014-11-15 22:54:45 UTC (rev 3552)
+++ pkg/FactorAnalytics/R/rCornishFisher.R	2014-11-19 10:00:09 UTC (rev 3553)
@@ -1,88 +0,0 @@
-#' Functions for Cornish-Fisher density, CDF, random number simulation and
-#' quantile.
-#'
-#'@name  CornishFisher
-#'@aliases CornishFisher
-#'@aliases rCornishFisher 
-#'@aliases dCornishFisher
-#'@aliases qCornishFisher
-#'@aliases pCornishFisher
-#'
-#'
-#'@description 
-#' \itemize{
-#' \item \code{rCornishFisher} simulate observations based on
-#' Cornish-Fisher quantile expansion given mean, standard
-#' deviation, skewness and excess kurtosis.
-#' \item \code{dCornishFisher} Computes Cornish-Fisher density
-#' from two term Edgeworth expansion given mean, standard
-#' deviation, skewness and excess kurtosis.
-#' \item \code{pCornishFisher} Computes Cornish-Fisher CDF from
-#' two term Edgeworth expansion given mean, standard
-#' deviation, skewness and excess kurtosis.
-#' \item \code{qCornishFisher} Computes Cornish-Fisher quantiles
-#' from two term Edgeworth expansion given mean, standard
-#' deviation, skewness and excess kurtosis.
-#'}
-#'
-#'@param n Scalar, number of simulated values in rCornishFisher. Sample length in
-#' density,distribution,quantile function.
-#' @param sigma Scalar, standard deviation.
-#' @param skew Scalar, skewness.
-#' @param ekurt Scalar, excess kurtosis.
-#' @param seed Set seed here. Default is \code{NULL}.
-#' @param x,q Vector of standardized quantiles. See detail.
-#' @param p Vector of probabilities.
-#' 
-#' @return n Simulated values from Cornish-Fisher distribution.
-#' @author Eric Zivot and Yi-An Chen.
-#' @references 
-#' \enumerate{
-#' \item A.DasGupta, "Asymptotic Theory of Statistics and
-#' Probability", Springer Science+Business Media,LLC 2008
-#' \item   Thomas A.Severini, "Likelihood Methods in Statistics",
-#'  Oxford University Press, 2000 
-#'  }
-#'  @export
-#'  
-#'  @details CDF(q) = Pr(sqrt(n)*(x_bar-mu)/sigma < q)
-#'  
-#'  @examples
-#'  \dontrun{
-#'  # generate 1000 observation from Cornish-Fisher distribution
-#' rc <- rCornishFisher(1000,1,0,5)
-#'hist(rc,breaks=100,freq=FALSE,main="simulation of Cornish Fisher Distribution",
-#'     xlim=c(-10,10))
-#'lines(seq(-10,10,0.1),dnorm(seq(-10,10,0.1),mean=0,sd=1),col=2)
-#' # compare with standard normal curve
-#'
-#' # example from A.dasGupta p.188 exponential example
-#' # x is iid exp(1) distribution, sample size = 5
-#' # then x_bar is Gamma(shape=5,scale=1/5) distribution
-#' q <- c(0,0.4,1,2)
-#' # exact cdf
-#' pgamma(q/sqrt(5)+1,shape=5,scale=1/5)
-#' # use CLT
-#' pnorm(q)
-#' # use edgeworth expansion
-#' pCornishFisher(q,n=5,skew=2,ekurt=6)
-#' }
-
-
-
-rCornishFisher <-
-function(n, sigma, skew, ekurt, seed=NULL) {
-## inputs:
-## n          scalar, number of simulated values
-## sigma      scalar, standard deviation
-## skew       scalar, skewness
-## ekurt      scalar, excess kurtosis
-## outputs:
-## n simulated values from Cornish-Fisher distribution
-if (!is.null(seed)) set.seed(seed)
-zc = rnorm(n)
-z.cf = zc  + (((zc^2 - 1) * skew)/6) + (((zc^3 - 3 * zc) *
-      ekurt)/24) - ((((2 * zc^3) - 5 * zc) * skew^2)/36)
-ans = sigma*z.cf
-ans
-}

Modified: pkg/FactorAnalytics/R/summary.pafm.r
===================================================================
--- pkg/FactorAnalytics/R/summary.pafm.r	2014-11-15 22:54:45 UTC (rev 3552)
+++ pkg/FactorAnalytics/R/summary.pafm.r	2014-11-19 10:00:09 UTC (rev 3553)
@@ -22,15 +22,15 @@
 #' 
 #' @method summary pafm  
 #' @export   
-#' 
-summary.pafm <- function(object ,digits = max(3, .Options$digits - 3),...) {
-#   n <- dim(fm.attr[[1]])[1]
-#   k <- dim(fm.attr[[1]])[2]+1 
-# table.mat <- matrix(rep(NA,n*k*2),ncol=n)
-  cat("\nMean of returns attributed to factors
-      \n")
-  print(sapply(object[[3]],function(x) apply(x,2,mean)),digits = digits,...)
-  cat("\nStandard Deviation of returns attributed to factors
-      \n")
-  print(sapply(object[[3]],function(x) apply(x,2,sd)),digits = digits,...)  
+
+summary.pafm <- function(object, digits=max(3, .Options$digits - 3), ...) {
+  
+  #   n <- dim(fm.attr[[1]])[1]
+  #   k <- dim(fm.attr[[1]])[2]+1 
+  # table.mat <- matrix(rep(NA,n*k*2),ncol=n)
+  
+  cat("\nMean of returns attributed to factors \n")
+  print(sapply(object[[3]], function(x) apply(x,2,mean)), digits=digits, ...)
+  cat("\nStandard Deviation of returns attributed to factors \n")
+  print(sapply(object[[3]], function(x) apply(x,2,sd)), digits=digits, ...)  
 }

Modified: pkg/FactorAnalytics/R/summary.tsfm.r
===================================================================
--- pkg/FactorAnalytics/R/summary.tsfm.r	2014-11-15 22:54:45 UTC (rev 3552)
+++ pkg/FactorAnalytics/R/summary.tsfm.r	2014-11-19 10:00:09 UTC (rev 3553)
@@ -74,9 +74,11 @@
   # extract coefficients separately for "lars" variable.selection method
   for (i in object$asset.names) {
     if (se.type=="HC") {
-      sum[[i]]$coefficients <- coeftest(object$asset.fit[[i]], vcovHC)[,1:4]
+      sum[[i]]$coefficients <- coeftest(object$asset.fit[[i]], 
+                                        vcov.=vcovHC)[,1:4]
     } else if (se.type=="HAC") {
-      sum[[i]]$coefficients <- coeftest(object$asset.fit[[i]], vcovHAC)[,1:4]
+      sum[[i]]$coefficients <- coeftest(object$asset.fit[[i]], 
+                                        vcov.=vcovHAC)[,1:4]
     }
   }
   

Modified: pkg/FactorAnalytics/man/CornishFisher.Rd
===================================================================
--- pkg/FactorAnalytics/man/CornishFisher.Rd	2014-11-15 22:54:45 UTC (rev 3552)
+++ pkg/FactorAnalytics/man/CornishFisher.Rd	2014-11-19 10:00:09 UTC (rev 3553)
@@ -1,12 +1,11 @@
 % Generated by roxygen2 (4.0.1): do not edit by hand
-\name{CornishFisher}
-\alias{CornishFisher}
+\name{dCornishFisher}
 \alias{dCornishFisher}
 \alias{pCornishFisher}
 \alias{qCornishFisher}
 \alias{rCornishFisher}
-\title{Functions for Cornish-Fisher density, CDF, random number simulation and
-quantile.}
+\title{Density, distribution function, quantile function and random
+generation for the Cornish-Fisher distribution.}
 \usage{
 dCornishFisher(x, n, skew, ekurt)
 
@@ -17,73 +16,74 @@
 rCornishFisher(n, sigma, skew, ekurt, seed = NULL)
 }
 \arguments{
-\item{n}{Scalar, number of simulated values in rCornishFisher. Sample length in
-density,distribution,quantile function.}
+\item{n}{scalar; number of simulated values in random simulation, sample
+length in density, distribution and quantile functions.}
 
-\item{sigma}{Scalar, standard deviation.}
+\item{sigma}{scalar standard deviation.}
 
-\item{skew}{Scalar, skewness.}
+\item{skew}{scalar; skewness.}
 
-\item{ekurt}{Scalar, excess kurtosis.}
+\item{ekurt}{scalar; excess kurtosis.}
 
-\item{seed}{Set seed here. Default is \code{NULL}.}
+\item{seed}{scalar; set seed. Default is \code{NULL}.}
 
-\item{x,q}{Vector of standardized quantiles. See detail.}
+\item{x,q}{vector of standardized quantiles.}
 
-\item{p}{Vector of probabilities.}
+\item{p}{vector of probabilities.}
 }
 \value{
-n Simulated values from Cornish-Fisher distribution.
+\code{dCornishFisher} gives the density, \code{pCornishFisher} gives the
+distribution function, \code{qCornishFisher} gives the quantile function,
+and \code{rCornishFisher} generates \code{n} random simulations.
 }
 \description{
-\itemize{
-\item \code{rCornishFisher} simulate observations based on
-Cornish-Fisher quantile expansion given mean, standard
-deviation, skewness and excess kurtosis.
-\item \code{dCornishFisher} Computes Cornish-Fisher density
-from two term Edgeworth expansion given mean, standard
-deviation, skewness and excess kurtosis.
-\item \code{pCornishFisher} Computes Cornish-Fisher CDF from
-two term Edgeworth expansion given mean, standard
-deviation, skewness and excess kurtosis.
-\item \code{qCornishFisher} Computes Cornish-Fisher quantiles
-from two term Edgeworth expansion given mean, standard
-deviation, skewness and excess kurtosis.
+Density, distribution function, quantile function and random
+generation for the Cornish-Fisher distribution.
 }
-}
 \details{
 CDF(q) = Pr(sqrt(n)*(x_bar-mu)/sigma < q)
+\itemize{
+\item \code{dCornishFisher} Computes Cornish-Fisher density from two term
+Edgeworth expansion given mean, standard deviation, skewness and excess
+kurtosis.
+\item \code{pCornishFisher} Computes Cornish-Fisher CDF from two term
+Edgeworth expansion given mean, standard deviation, skewness and excess
+kurtosis.
+\item \code{qCornishFisher} Computes Cornish-Fisher quantiles from two term
+Edgeworth expansion given mean, standard deviation, skewness and excess
+kurtosis.
+\item \code{rCornishFisher} simulate observations based on Cornish-Fisher
+quantile expansion given mean, standard deviation, skewness and excess
+kurtosis.}
 }
 \examples{
 \dontrun{
- # generate 1000 observation from Cornish-Fisher distribution
+# generate 1000 observation from Cornish-Fisher distribution
 rc <- rCornishFisher(1000,1,0,5)
-hist(rc,breaks=100,freq=FALSE,main="simulation of Cornish Fisher Distribution",
-    xlim=c(-10,10))
-lines(seq(-10,10,0.1),dnorm(seq(-10,10,0.1),mean=0,sd=1),col=2)
+hist(rc, breaks=100, freq=FALSE,
+     main="simulation of Cornish Fisher Distribution", xlim=c(-10,10))
+lines(seq(-10,10,0.1), dnorm(seq(-10,10,0.1), mean=0, sd=1), col=2)
 # compare with standard normal curve
 
-# example from A.dasGupta p.188 exponential example
+# exponential example from A.dasGupta p.188
 # x is iid exp(1) distribution, sample size = 5
-# then x_bar is Gamma(shape=5,scale=1/5) distribution
+# then x_bar is Gamma(shape=5, scale=1/5) distribution
 q <- c(0,0.4,1,2)
 # exact cdf
-pgamma(q/sqrt(5)+1,shape=5,scale=1/5)
+pgamma(q/sqrt(5)+1, shape=5, scale=1/5)
 # use CLT
 pnorm(q)
 # use edgeworth expansion
-pCornishFisher(q,n=5,skew=2,ekurt=6)
+pCornishFisher(q, n=5, skew=2, ekurt=6)
 }
 }
 \author{
 Eric Zivot and Yi-An Chen.
 }
 \references{
-\enumerate{
-\item A.DasGupta, "Asymptotic Theory of Statistics and
-Probability", Springer Science+Business Media,LLC 2008
-\item   Thomas A.Severini, "Likelihood Methods in Statistics",
- Oxford University Press, 2000
- }
+DasGupta, A. (2008). Asymptotic theory of statistics and probability.
+Springer.
+Severini, T. A., (2000). Likelihood Methods in Statistics. Oxford University
+Press.
 }
 

Modified: pkg/FactorAnalytics/man/fitTsfm.Rd
===================================================================
--- pkg/FactorAnalytics/man/fitTsfm.Rd	2014-11-15 22:54:45 UTC (rev 3552)
+++ pkg/FactorAnalytics/man/fitTsfm.Rd	2014-11-19 10:00:09 UTC (rev 3553)
@@ -98,7 +98,7 @@
 Estimation method "OLS" corresponds to ordinary least squares using
 \code{\link[stats]{lm}}, "DLS" is discounted least squares (weighted least
 squares with exponentially declining weights that sum to unity), and,
-"Robust" is robust regression (useing \code{\link[robust]{lmRob}}).
+"Robust" is robust regression (using \code{\link[robust]{lmRob}}).
 
 If \code{variable.selection="none"}, all chosen factors are used in the
 factor model. Whereas, "stepwise" performs traditional forward/backward



More information about the Returnanalytics-commits mailing list