[Uwgarp-commits] r178 - in pkg/GARPFRM: . R man sandbox

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jun 10 20:34:50 CEST 2014


Author: tfillebeen
Date: 2014-06-10 20:34:49 +0200 (Tue, 10 Jun 2014)
New Revision: 178

Modified:
   pkg/GARPFRM/NAMESPACE
   pkg/GARPFRM/R/riskMetricsAndHedges.R
   pkg/GARPFRM/man/plot.capm_uv.Rd
   pkg/GARPFRM/sandbox/principleComponent.R
Log:
PCA additions

Modified: pkg/GARPFRM/NAMESPACE
===================================================================
--- pkg/GARPFRM/NAMESPACE	2014-06-10 01:29:55 UTC (rev 177)
+++ pkg/GARPFRM/NAMESPACE	2014-06-10 18:34:49 UTC (rev 178)
@@ -16,6 +16,7 @@
 S3method(hypTest,capm_uv)
 S3method(plot,EWMA)
 S3method(plot,MonteCarlo)
+S3method(plot,PCA)
 S3method(plot,backtestVaR)
 S3method(plot,capm_mlm)
 S3method(plot,capm_uv)
@@ -60,11 +61,12 @@
 export(getCov)
 export(getEstimate)
 export(getFit)
+export(getLoadings)
 export(getSpec)
 export(getStatistics)
 export(getVaREstimates)
 export(getVaRViolations)
-export(getthreeLoadings)
+export(getWeights)
 export(hypTest)
 export(is.bond)
 export(linearHedge)

Modified: pkg/GARPFRM/R/riskMetricsAndHedges.R
===================================================================
--- pkg/GARPFRM/R/riskMetricsAndHedges.R	2014-06-10 01:29:55 UTC (rev 177)
+++ pkg/GARPFRM/R/riskMetricsAndHedges.R	2014-06-10 18:34:49 UTC (rev 178)
@@ -108,7 +108,10 @@
   return(abs(tmpPrice - targetPrice))
 }
 
-#' Calculate the convexity of a fixed rate coupon bond
+
+############Hedge section
+
+#' Estimate the delta hedge of for a bond
 #' 
 #' This function estimates the delta for hedging a particular bond 
 #' given bond data
@@ -130,30 +133,36 @@
 #' given bond data
 #' 
 #' @param data time series data
+#' @param nfactors number of components to extract
+#' @param rotate "none", "varimax", "quatimax", "promax", "oblimin", "simplimax", and "cluster" are possible rotations/transformations of the solution.
 #' @return pca object loadings
 #' @export
-PCA <- function(data){
-  ### Write body####
-  
-  
-  
-  return(delta)
+PCA <- function(data, nfactors, rotate = "none"){
+  pca = principal(data, nfactors, rotate="none")
+  class(pca) <- c("psych", "principal","PCA")
+  return(pca)
 }
 
-#' PCA loadings
+#' Retrieve PCA loadings
 #' 
 #' @param object is a pca object
 #' @author TF
 #' @export 
-getthreeLoadings <- function(object){
-  ### Write body####
-  
-  return(threeLoadings)
+getLoadings <- function(object){
+loadings = object$loadings
+  return(loadings)
 }
 
+#' Retrieve PCA weights
+#' 
+#' @param object is a pca object
+#' @author TF
+#' @export 
+getWeights <- function(object){
+  weights = object$weight
+  return(weights)
+}
 
-
-
 #' Plotting method for PCA
 #' 
 #' Plot a fitted PCA object
@@ -163,21 +172,16 @@
 #' @param number specify the nunber of loadings
 #' @param \dots passthrough parameters to \code{\link{plot}}.
 #' @param main a main title for the plot
-#' @author Thomas Fillebeen
-#' @method plot pca loadings
-#' @S3method plot capm_uv
-plot.capm_uv <- function(x, y, number, ..., main="CAPM"){
-  ### Write body####
-  
-  
-  
-  
+#' @author TF
+#' @method plot PCA
+#' @S3method plot PCA
+plot.PCA <- function(x, y, ..., main="Beta from PCA regression"){
+  if(ncol(x$loading)> 3) warning("Only first 3 loadings will be graphically displayed")
   # Plot the first three factors
-  plot(pca$loading[,1], type="l", main="Beta from PCA regression", 
+  plot(pca$loading[,1], type="l", main, 
        xlab="maturity", ylab="beta")
   lines(pca$loading[,2], col="blue",lty=2)
   lines(pca$loading[,3], col="red",lty=2)
   legend("topleft",legend=c("PCA1","PCA2","PCA3"),bty="n",lty=c(1,2,2),col=c("black","blue","red"), cex=0.8)
   
-}
-
+}
\ No newline at end of file

Modified: pkg/GARPFRM/man/plot.capm_uv.Rd
===================================================================
--- pkg/GARPFRM/man/plot.capm_uv.Rd	2014-06-10 01:29:55 UTC (rev 177)
+++ pkg/GARPFRM/man/plot.capm_uv.Rd	2014-06-10 18:34:49 UTC (rev 178)
@@ -2,9 +2,7 @@
 \alias{plot.capm_uv}
 \title{Plotting method for CAPM}
 \usage{
-\method{plot}{capm_uv}(x, y, number, ..., main = "CAPM")
-
-\method{plot}{pca loadings}(x, y, number, ..., main = "CAPM")
+\method{plot}{capm_uv}(x, y, ..., main = "CAPM")
 }
 \arguments{
   \item{x}{a capm object created by \code{\link{CAPM}}.}
@@ -15,26 +13,11 @@
   \code{\link{plot}}.}
 
   \item{main}{a main title for the plot}
-
-  \item{x}{a PCA object created.}
-
-  \item{y}{not used}
-
-  \item{number}{specify the nunber of loadings}
-
-  \item{\dots}{passthrough parameters to
-  \code{\link{plot}}.}
-
-  \item{main}{a main title for the plot}
 }
 \description{
 Plot a fitted CAPM object
-
-Plot a fitted PCA object
 }
 \author{
 Thomas Fillebeen
-
-Thomas Fillebeen
 }
 

Modified: pkg/GARPFRM/sandbox/principleComponent.R
===================================================================
--- pkg/GARPFRM/sandbox/principleComponent.R	2014-06-10 01:29:55 UTC (rev 177)
+++ pkg/GARPFRM/sandbox/principleComponent.R	2014-06-10 18:34:49 UTC (rev 178)
@@ -7,9 +7,16 @@
 head(data)
 
 # Retain components that combined account for x% of the cumulative variance
-pca = principal(r = data, nfactors=5, rotate="none")
+pca = PCA(data, nfactors = 3, rotate="none")
 summary(pca)
 
+# Retrieve Loadings and if loading is insignificant then omit
+getLoadings(pca)
+
+# Retrieve Weights
+getWeights(pca)
+
+## Structural Equation Modelling
 # Determining the appropriate number of factors
 # A graphic representation of the 3 oblique factors
 fa.diagram(pca)



More information about the Uwgarp-commits mailing list