[Returnanalytics-commits] r3395 - in pkg/FactorAnalytics: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue May 27 08:01:42 CEST 2014
Author: chenyian
Date: 2014-05-27 08:01:39 +0200 (Tue, 27 May 2014)
New Revision: 3395
Modified:
pkg/FactorAnalytics/R/fitFundamentalFactorModel.R
pkg/FactorAnalytics/man/fitFundamentalFactorModel.Rd
Log:
add corr functionality to fitFundamentalFactorModel.R
Modified: pkg/FactorAnalytics/R/fitFundamentalFactorModel.R
===================================================================
--- pkg/FactorAnalytics/R/fitFundamentalFactorModel.R 2014-05-27 04:51:58 UTC (rev 3394)
+++ pkg/FactorAnalytics/R/fitFundamentalFactorModel.R 2014-05-27 06:01:39 UTC (rev 3395)
@@ -53,6 +53,10 @@
#' \item resids.cov An object of class "cov" or "covRob" which contains
#' the covariance matrix of the residuals, if "full.resid.cov" is TRUE. NULL
#' if "full.resid.cov" is FALSE.
+#' \item returns.corr Correlation matrix of assets returns.
+#' \item factor.corr An object of class "cov" or "covRob" which
+#' contains the correlation matrix of the factor returns (including intercept).
+#' \item resids.corr Correlation matrix of returns returns.
#' \item resid.variance A vector of variances estimated from the OLS
#' residuals for each asset. If "wls" is TRUE, these are the weights used in
#' the weighted least squares regressions. If "cov = robust" these values are
@@ -428,16 +432,10 @@
if (full.resid.cov) { D.hat$cov
} else { D.hat }
mean.cov.returns = tapply(data[[returnsvar]],data[[assetvar]], mean)
+Corr.returns = cov2cor(cov.returns)
Cov.returns <- list(cov = cov.returns, mean=mean.cov.returns, eigenvalues = eigen(cov.returns,
only.values = TRUE, symmetric = TRUE)$values)
- # report residual covaraince if full.resid.cov is true.
- if (full.resid.cov) {
- Cov.resids <- D.hat
- }
- else {
- Cov.resids <- diag(resid.vars)
- }
#
# # r-square for each asset = 1 - SSE/SST
# SSE <- apply(fit.fund$residuals^2,2,sum)
@@ -449,9 +447,34 @@
colnames(f.hat)[1] <- "Intercept"
}
+# report corr matrix
+
+if (covariance == "robust") {
+ if (kappa(na.exclude(coredata(f.hat))) < 1e+10) {
+ Corr.factors <- covRob(coredata(f.hat), estim = "pairwiseGK",
+ distance = FALSE, na.action = na.omit,corr=TRUE)
+ } else {
+ cat("Covariance matrix of factor returns is singular.\n")
+ Corr.factors <- covRob(coredata(f.hat), distance = FALSE,
+ na.action = na.omit,corr=TRUE)
+ }
+ Corr.D <- if (full.resid.cov) { cov2cor(D.hat$cov)
+ } else { NULL }
+
+} else {
+ Corr.factors <- covClassic(coredata(f.hat), distance = FALSE,na.action = na.omit,corr=TRUE)
+ Corr.D <- if (full.resid.cov) { cov2cor(D.hat$cov)
+ } else { NULL }
+}
+
+
+
output <- list(returns.cov = Cov.returns,
factor.cov = Cov.factors,
- resids.cov = Cov.resids,
+ resids.cov = D.hat,
+ returns.corr = Corr.returns,
+ factor.corr = Corr.factors,
+ resids.corr = Corr.D,
resid.variance = resid.vars,
factor.returns = f.hat,
residuals = resids,
Modified: pkg/FactorAnalytics/man/fitFundamentalFactorModel.Rd
===================================================================
--- pkg/FactorAnalytics/man/fitFundamentalFactorModel.Rd 2014-05-27 04:51:58 UTC (rev 3394)
+++ pkg/FactorAnalytics/man/fitFundamentalFactorModel.Rd 2014-05-27 06:01:39 UTC (rev 3395)
@@ -63,19 +63,24 @@
resids.cov An object of class "cov" or "covRob" which
contains the covariance matrix of the residuals, if
"full.resid.cov" is TRUE. NULL if "full.resid.cov" is
- FALSE. \item resid.variance A vector of variances
- estimated from the OLS residuals for each asset. If "wls"
- is TRUE, these are the weights used in the weighted least
- squares regressions. If "cov = robust" these values are
- computed with "scale.tau". Otherwise they are computed
- with "var". \item factor.returns A "xts" object
- containing the times series of estimated factor returns
- and intercepts. \item residuals A "xts" object containing
- the time series of residuals for each asset. \item tstats
- A "xts" object containing the time series of t-statistics
- for each exposure. \item call function call \item
- exposure.names A character string giving the name of the
- exposure variable in the data. }
+ FALSE. \item returns.corr Correlation matrix of assets
+ returns. \item factor.corr An object of class "cov" or
+ "covRob" which contains the correlation matrix of the
+ factor returns (including intercept). \item resids.corr
+ Correlation matrix of returns returns. \item
+ resid.variance A vector of variances estimated from the
+ OLS residuals for each asset. If "wls" is TRUE, these are
+ the weights used in the weighted least squares
+ regressions. If "cov = robust" these values are computed
+ with "scale.tau". Otherwise they are computed with
+ "var". \item factor.returns A "xts" object containing the
+ times series of estimated factor returns and intercepts.
+ \item residuals A "xts" object containing the time series
+ of residuals for each asset. \item tstats A "xts" object
+ containing the time series of t-statistics for each
+ exposure. \item call function call \item exposure.names A
+ character string giving the name of the exposure variable
+ in the data. }
}
\description{
fit fundamental factor model or cross-sectional factor
More information about the Returnanalytics-commits
mailing list