[Returnanalytics-commits] r2754 - in pkg/FactorAnalytics: R vignettes

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Aug 10 03:30:11 CEST 2013


Author: chenyian
Date: 2013-08-10 03:30:10 +0200 (Sat, 10 Aug 2013)
New Revision: 2754

Modified:
   pkg/FactorAnalytics/R/fitFundamentalFactorModel.R
   pkg/FactorAnalytics/vignettes/equity.Rdata
   pkg/FactorAnalytics/vignettes/fundamentalFM.Rnw
Log:
update vignette and data set

Modified: pkg/FactorAnalytics/R/fitFundamentalFactorModel.R
===================================================================
--- pkg/FactorAnalytics/R/fitFundamentalFactorModel.R	2013-08-10 01:11:43 UTC (rev 2753)
+++ pkg/FactorAnalytics/R/fitFundamentalFactorModel.R	2013-08-10 01:30:10 UTC (rev 2754)
@@ -138,16 +138,16 @@
     require(robust)
     
     
-    assets = unique(data[,assetvar])
-    timedates = as.Date(unique(data[,datevar]))    
+    assets = unique(data[[assetvar]])
+    timedates = as.Date(unique(data[[datevar]]))    
+#     data[[datevar]] <- as.Date(data[[datevar]])
     
-    
     if (length(timedates) < 2) 
       stop("At least two time points, t and t-1, are needed for fitting the factor model.")
-    if (!is(exposure.names, "vector") || !is.character(exposure.names)) 
-      stop("exposure argument invalid---must be character vector.")
-    if (!is(assets, "vector") || !is.character(assets)) 
-      stop("assets argument invalid---must be character vector.")
+     if (!is(exposure.names, "vector") || !is.character(exposure.names)) 
+       stop("exposure argument invalid---must be character vector.")
+     if (!is(assets, "vector") || !is.character(assets)) 
+       stop("assets argument invalid---must be character vector.")
     
     wls <- as.logical(wls)
     full.resid.cov <- as.logical(full.resid.cov)
@@ -279,8 +279,7 @@
         else stop(mess)
       }
       tstat <- rep(NA, length(model$coef))
-      tstat[!is.na(model$coef)] <- summary(model, cor = FALSE)$coef[, 
-                                                                    3]
+      tstat[!is.na(model$coef)] <- summary(model, cor = FALSE)$coef[,3]
       alphaord <- order(names(model$coef))
       c(length(model$coef), model$coef[alphaord], tstat[alphaord], 
         model$resid)
@@ -319,7 +318,8 @@
         FE.hat <- by(data = data, INDICES = as.numeric(data[[datevar]]), 
                      FUN = wls.robust, modelterms = regression.formula, 
                      conlist = contrasts.list, w = weights)
-      } else { 
+      } 
+      else { 
         # wls.classic
         resids <- by(data = data, INDICES = as.numeric(data[[datevar]]), 
                      FUN = function(xdf, modelterms, conlist) {

Modified: pkg/FactorAnalytics/vignettes/equity.Rdata
===================================================================
(Binary files differ)

Modified: pkg/FactorAnalytics/vignettes/fundamentalFM.Rnw
===================================================================
--- pkg/FactorAnalytics/vignettes/fundamentalFM.Rnw	2013-08-10 01:11:43 UTC (rev 2753)
+++ pkg/FactorAnalytics/vignettes/fundamentalFM.Rnw	2013-08-10 01:30:10 UTC (rev 2754)
@@ -41,13 +41,49 @@
 We download data from CRSP/Compustat quarterly fundamental and name \verb at equity@ which contains 67 stocks from January 2000 to Decenmber 2013.  
 
 <<loading.data>>=
-equity <- read.csv(file="equity.csv")
+equity <- data(equity)
 names(equity)
 length(unique(equity$datadate)) # number of period t 
 length(unique(equity$tic)) # number of assets
 @
-We want return
+We want asset returns.
+<<get.returns>>=
+library(quantmod)   # for Delt. See Delt for detail
+equity <- cbind(equity,do.call(rbind,lapply(split(equity,equity$tic), 
+                                            function(x) Delt(x$PRCCQ)))) 
+names(equity)[22] <- "RET"
+@
+We want market value and book-to-market ratio too. market vale can be achieved by commom stocks outstading x price and book value we use commom/ordinary equity value. 
+<<get.mv.bm>>==
+equity$MV <- equity$PRCCQ*equity$CSHOQ 
+equity$BM <- equity$CEQQ/equity$MV
+@
+now we use model \ref{ffm} where K=2, b = [ MV , BM ].
 
+We will get an error message if \verb at datevar@ is not \verb at as.Date@ format compatible. In our example, our date variable is \emph{DATACQTR} and looks like "2000Q1". We have to convert it to \verb at as.Date@ compatible. We can utilize \verb at as.yearqtr@ to do it. Aslo, we will use character string for asset variable instead of factor.
+<<as.yearqtr>>=
+a <- unlist( lapply(strsplit(as.character(equity$DATACQTR),"Q"),
+               function(x)  paste(x[[1]],"-",x[[2]],sep="") ) )
+equity$yearqtr <- as.yearqtr(a,format="%Y-%q")
+equity$tic <- as.character(equity$tic)
+equity <- subset(equity,yearqtr != "2000 Q1")
+# delete the first element of each assets
+@
 
+fit the function:
+<<fit.ffm>>=
+fit.fund <- fitFundamentalFactorModel(exposure.names=c("BM","MV"),datevar="yearqtr",
+                                      returnsvar ="RET",assetvar="tic",wls=TRUE,data=equity)
+names(fit.fund)
+@
 
+
+
+
+
+
+
+
+
+
 \end{document}
\ No newline at end of file



More information about the Returnanalytics-commits mailing list