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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jan 10 23:10:07 CET 2014


Author: rossbennett34
Date: 2014-01-10 23:10:06 +0100 (Fri, 10 Jan 2014)
New Revision: 41

Modified:
   pkg/GARPFRM/NAMESPACE
   pkg/GARPFRM/R/capm.R
   pkg/GARPFRM/man/CAPM.Rd
   pkg/GARPFRM/sandbox/test_capm_rb.R
Log:
Adding mv for CAPM

Modified: pkg/GARPFRM/NAMESPACE
===================================================================
--- pkg/GARPFRM/NAMESPACE	2014-01-03 20:20:14 UTC (rev 40)
+++ pkg/GARPFRM/NAMESPACE	2014-01-10 22:10:06 UTC (rev 41)
@@ -2,6 +2,7 @@
 export(getAlphas)
 export(getBetas)
 export(getStatistics)
+export(plot.capm_uv)
 S3method(getAlphas,capm_uv)
 S3method(getBetas,capm_uv)
 S3method(getStatistics,capm_uv)

Modified: pkg/GARPFRM/R/capm.R
===================================================================
--- pkg/GARPFRM/R/capm.R	2014-01-03 20:20:14 UTC (rev 40)
+++ pkg/GARPFRM/R/capm.R	2014-01-10 22:10:06 UTC (rev 41)
@@ -23,24 +23,38 @@
 #' Capital Asset Pricing Model
 #' 
 #' Description of CAPM
+#' bla bla bla
 #' 
 #' @param R asset returns
 #' @param Rmkt market returns
 #' @export
+#' @examples
+#' data(crsp.short)
+#' 
+#' head(largecap.ts)
+#' 
+#' Rf <- largecap.ts[, "t90"]
+#' R <- largecap.ts[, "CAT"] - Rf
+#' MKT <- largecap.ts[, "market"] - Rf
+#'
+#' # Fit the CAPM model
+#' tmp <- CAPM(R=R, Rmkt=MKT)
 CAPM <- function(R, Rmkt){
   # We should have a capm_uv class for a univariate capm (i.e. R is the returns
   # of a single asset) and capm_mv for a multivariate capm (i.e. R is the returns
   # for multiple assets)
   
-  # if(ncol(R) > 1){
-  #   multivariate capm
-  # } else if(ncol(R) == 1){
-  #   univariate capm
-  # }
+  capm_fit <- lm(R ~ Rmkt)
+  capm_fit$x_data <- Rmkt
+  capm_fit$y_data <- R
   
-  # For now, assume that R is the returns of a single asset
-  capm_fit <- lm(R ~ Rmkt)
-  class(capm_fit) <- c("capm_uv", "lm")
+  if(ncol(R) > 1){
+    #  multivariate capm
+    class(capm_fit) <- c("capm_mv", "lm")
+  } else if(ncol(R) == 1){
+    #  univariate capm
+    class(capm_fit) <- c("capm_uv", "lm")
+  }
   return(capm_fit)
 }
 
@@ -57,9 +71,16 @@
 #' @method getAlphas capm_uv
 #' @S3method getAlphas capm_uv
 getAlphas.capm_uv <- function(object){
+  if(!inherits(object, "capm_uv")) stop("object must be of class capm_uv")
   return(coef(object)[1])
 }
 
+#' @method getAlphas capm_mv
+#' @S3method getAlphas capm_mv
+getAlphas.capm_mv <- function(object){
+  if(!inherits(object, "capm_mv")) stop("object must be of class capm_uv")
+}
+
 #' CAPM betas
 #' 
 #' Description of CAPM betas
@@ -73,9 +94,16 @@
 #' @method getBetas capm_uv
 #' @S3method getBetas capm_uv
 getBetas.capm_uv <- function(object){
+  if(!inherits(object, "capm_uv")) stop("object must be of class capm_uv")
   return(coef(object)[2])
 }
 
+#' @method getBetas capm_mv
+#' @S3method getBetas capm_mv
+getBetas.capm_mv <- function(object){
+  if(!inherits(object, "capm_mv")) stop("object must be of class capm_uv")
+}
+
 #' CAPM statistics
 #' 
 #' Description of CAPM statistics (standard error, t-values, and p-values)
@@ -88,7 +116,24 @@
 #' @method getStatistics capm_uv
 #' @S3method getStatistics capm_uv
 getStatistics.capm_uv <- function(object){
+  if(!inherits(object, "capm_uv")) stop("object must be of class capm_uv")
   tmp_sm <- summary.lm(object)
   # gets the standard error, t-value, and p-value of model
   return(coef(tmp_sm)[,2:4])
 }
+
+#' @method getStatistics capm_mv
+#' @S3method getStatistics capm_mv
+getStatistics.capm_uv <- function(object){
+  if(!inherits(object, "capm_mv")) stop("object must be of class capm_uv")
+  tmp_sm <- summary.lm(object)
+  # gets the standard error, t-value, and p-value of model
+}
+
+#' @export
+plot.capm_uv <- function(object){
+  xlab <- colnames(object$x_data)
+  ylab <- colnames(object$y_data)
+  plot(x=coredata(object$x_data), y=(object$y_data), xlab=xlab, ylab=ylab, main="CAPM Plot")
+  abline(object)
+}

Modified: pkg/GARPFRM/man/CAPM.Rd
===================================================================
--- pkg/GARPFRM/man/CAPM.Rd	2014-01-03 20:20:14 UTC (rev 40)
+++ pkg/GARPFRM/man/CAPM.Rd	2014-01-10 22:10:06 UTC (rev 41)
@@ -10,6 +10,18 @@
   \item{Rmkt}{market returns}
 }
 \description{
-  Description of CAPM
+  Description of CAPM bla bla bla
 }
+\examples{
+data(crsp.short)
 
+head(largecap.ts)
+
+Rf <- largecap.ts[, "t90"]
+R <- largecap.ts[, "CAT"] - Rf
+MKT <- largecap.ts[, "market"] - Rf
+
+# Fit the CAPM model
+tmp <- CAPM(R=R, Rmkt=MKT)
+}
+

Modified: pkg/GARPFRM/sandbox/test_capm_rb.R
===================================================================
--- pkg/GARPFRM/sandbox/test_capm_rb.R	2014-01-03 20:20:14 UTC (rev 40)
+++ pkg/GARPFRM/sandbox/test_capm_rb.R	2014-01-10 22:10:06 UTC (rev 41)
@@ -13,6 +13,18 @@
 
 class(tmp)
 
+plot(tmp)
+
+
+tmp$raw_data
+
+coef(tmp)
+summary(tmp)
+
+
+
+plot(tmp)
+
 # Internally, R is calling print.lm and summary.lm because tmp is of class lm
 # You can define your own print.capm_uv and summary.capm_uv for control over
 # what values are calculated and the output that is displayed.



More information about the Uwgarp-commits mailing list