Skip to content

Commit 24b684e

Browse files
committed
- more fixes for R CMD check prior to CRAN release
1 parent 2699232 commit 24b684e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+300
-188
lines changed

.Rbuildignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ generatechangelog.sh
33
ChangeLog.1.0.0
44
^.*\.Rproj$
55
^\.Rproj\.user$
6+
codeblock.txt

DESCRIPTION

+3-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Description: Collection of econometric functions for
1515
numbers of functions will work with P&L or price data
1616
where possible.
1717
Depends:
18-
R (>= 2.14.0),
18+
R (>= 3.0.0),
1919
zoo,
2020
xts (>= 0.8-9)
2121
Suggests:
@@ -32,8 +32,7 @@ License: GPL
3232
URL: http://r-forge.r-project.org/projects/returnanalytics/
3333
Copyright: (c) 2004-2014
3434
Contributors: Kris Boudt, Diethelm Wuertz, Eric Zivot, Matthieu Lestel
35-
Thanks: A special thanks for additional contributions from
35+
Thanks: A special thanks for additional contributions or patches from
3636
Stefan Albrecht, Khahn Nygyen, Jeff Ryan,
3737
Josh Ulrich, Sankalp Upadhyay, Tobias Verbeke,
38-
H. Felix Wittmann, Ram Ahluwalia
39-
38+
H. Felix Wittmann, Ram Ahluwalia, R. Douglas Martin

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ export(tol7qualitative)
230230
export(tol8qualitative)
231231
export(tol9qualitative)
232232
export(zerofill)
233+
import(xts)
233234
importFrom(stats,sd)
234235
importFrom(utils,packageDescription)
235236
importFrom(zoo,rollapply)

R/CAPM.dynamic.R

+8-3
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,14 @@
4848
#' @examples
4949
#'
5050
#' data(managers)
51-
#' CAPM.dynamic(managers[,1,drop=FALSE], managers[,8,drop=FALSE], Rf=.035/12, Z=managers[, 9:10])
52-
#' CAPM.dynamic(managers[80:120,1:6], managers[80:120,7,drop=FALSE], Rf=managers[80:120,10,drop=FALSE], Z=managers[80:120, 9:10])
53-
#' CAPM.dynamic(managers[80:120,1:6], managers[80:120,8:7], managers[80:120,10,drop=FALSE], Z=managers[80:120, 9:10])
51+
#' CAPM.dynamic(managers[,1,drop=FALSE], managers[,8,drop=FALSE],
52+
#' Rf=.035/12, Z=managers[, 9:10])
53+
#'
54+
#' CAPM.dynamic(managers[80:120,1:6], managers[80:120,7,drop=FALSE],
55+
#' Rf=managers[80:120,10,drop=FALSE], Z=managers[80:120, 9:10])
56+
#'
57+
#' CAPM.dynamic(managers[80:120,1:6], managers[80:120,8:7],
58+
#' managers[80:120,10,drop=FALSE], Z=managers[80:120, 9:10])
5459
#'
5560
#' @rdname CAPM.dynamic
5661
#' @export CAPM.dynamic SFM.dynamic

R/MSquaredExcess.R

+5-4
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
#' @examples
2424
#'
2525
#' data(portfolio_bacon)
26-
#' print(MSquaredExcess(portfolio_bacon[,1], portfolio_bacon[,2])) #expected -0.00998
27-
#' print(MSquaredExcess(portfolio_bacon[,1], portfolio_bacon[,2], Method="arithmetic")) #expected -0.011
26+
#' MSquaredExcess(portfolio_bacon[,1], portfolio_bacon[,2]) #expected -0.00998
27+
#'
28+
#' MSquaredExcess(portfolio_bacon[,1], portfolio_bacon[,2], Method="arithmetic") #expected -0.011
2829
#'
2930
#' data(managers)
30-
#' print(MSquaredExcess(managers['1996',1], managers['1996',8]))
31-
#' print(MSquaredExcess(managers['1996',1:5], managers['1996',8]))
31+
#' MSquaredExcess(managers['1996',1], managers['1996',8])
32+
#' MSquaredExcess(managers['1996',1:5], managers['1996',8])
3233
#'
3334
#' @export
3435
MSquaredExcess <-

R/MarketTiming.R

+95-95
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,96 @@
1-
#' Market timing models
2-
#'
3-
#' Allows to estimate Treynor-Mazuy or Merton-Henriksson market timing model.
4-
#' The Treynor-Mazuy model is essentially a quadratic extension of the basic
5-
#' CAPM. It is estimated using a multiple regression. The second term in the
6-
#' regression is the value of excess return squared. If the gamma coefficient
7-
#' in the regression is positive, then the estimated equation describes a
8-
#' convex upward-sloping regression "line". The quadratic regression is:
9-
#' \deqn{R_{p}-R_{f}=\alpha+\beta (R_{b} - R_{f})+\gamma (R_{b}-R_{f})^2+
10-
#' \varepsilon_{p}}{Rp - Rf = alpha + beta(Rb -Rf) + gamma(Rb - Rf)^2 +
11-
#' epsilonp}
12-
#' \eqn{\gamma}{gamma} is a measure of the curvature of the regression line.
13-
#' If \eqn{\gamma}{gamma} is positive, this would indicate that the manager's
14-
#' investment strategy demonstrates market timing ability.
15-
#'
16-
#' The basic idea of the Merton-Henriksson test is to perform a multiple
17-
#' regression in which the dependent variable (portfolio excess return and a
18-
#' second variable that mimics the payoff to an option). This second variable
19-
#' is zero when the market excess return is at or below zero and is 1 when it
20-
#' is above zero:
21-
#' \deqn{R_{p}-R_{f}=\alpha+\beta (R_{b}-R_{f})+\gamma D+\varepsilon_{p}}{Rp -
22-
#' Rf = alpha + beta * (Rb - Rf) + gamma * D + epsilonp}
23-
#' where all variables are familiar from the CAPM model, except for the
24-
#' up-market return \eqn{D=max(0,R_{b}-R_{f})}{D = max(0, Rb - Rf)} and market
25-
#' timing abilities \eqn{\gamma}{gamma}
26-
#' @param Ra an xts, vector, matrix, data frame, timeSeries or zoo object of
27-
#' the asset returns
28-
#' @param Rb an xts, vector, matrix, data frame, timeSeries or zoo object of
29-
#' the benchmark asset return
30-
#' @param Rf risk free rate, in same period as your returns
31-
#' @param method used to select between Treynor-Mazuy and Henriksson-Merton
32-
#' models. May be any of: \itemize{ \item TM - Treynor-Mazuy model,
33-
#' \item HM - Henriksson-Merton model} By default Treynor-Mazuy is selected
34-
#' @param \dots any other passthrough parameters
35-
#' @author Andrii Babii, Peter Carl
36-
#' @seealso \code{\link{CAPM.beta}}
37-
#' @references J. Christopherson, D. Carino, W. Ferson. \emph{Portfolio
38-
#' Performance Measurement and Benchmarking}. 2009. McGraw-Hill, p. 127-133.
39-
#' \cr J. L. Treynor and K. Mazuy, "Can Mutual Funds Outguess the Market?"
40-
#' \emph{Harvard Business Review}, vol44, 1966, pp. 131-136
41-
#' \cr Roy D. Henriksson and Robert C. Merton, "On Market Timing and Investment
42-
#' Performance. II. Statistical Procedures for Evaluating Forecast Skills,"
43-
#' \emph{Journal of Business}, vol.54, October 1981, pp.513-533 \cr
44-
#' @examples
45-
#'
46-
#' data(managers)
47-
#' MarketTiming(managers[,1,drop=FALSE], managers[,8,drop=FALSE], Rf=.035/12, method = "HM")
48-
#' MarketTiming(managers[80:120,1:6], managers[80:120,7,drop=FALSE], managers[80:120,10,drop=FALSE])
49-
#' MarketTiming(managers[80:120,1:6], managers[80:120,8:7], managers[80:120,10,drop=FALSE], method = "TM")
50-
#'
51-
#' @export
52-
MarketTiming <- function (Ra, Rb, Rf = 0, method = c("TM", "HM"))
53-
{ # @author Andrii Babii, Peter Carl
54-
55-
# FUNCTION
56-
57-
Ra = checkData(Ra)
58-
Rb = checkData(Rb)
59-
if (!is.null(dim(Rf)))
60-
Rf = checkData(Rf)
61-
Ra.ncols = NCOL(Ra)
62-
Rb.ncols = NCOL(Rb)
63-
pairs = expand.grid(1:Ra.ncols, 1)
64-
method = method[1]
65-
xRa = Return.excess(Ra, Rf)
66-
xRb = Return.excess(Rb, Rf)
67-
68-
mt <- function (xRa, xRb)
69-
{
70-
switch(method,
71-
"HM" = { S = xRb > 0 },
72-
"TM" = { S = xRb }
73-
)
74-
R = merge(xRa, xRb, xRb*S)
75-
R.df = as.data.frame(R)
76-
model = lm(R.df[, 1] ~ 1 + ., data = R.df[, -1])
77-
return(coef(model))
78-
}
79-
80-
result = apply(pairs, 1, FUN = function(n, xRa, xRb)
81-
mt(xRa[, n[1]], xRb[, 1]), xRa = xRa, xRb = xRb)
82-
result = t(result)
83-
84-
if (ncol(Rb) > 1){
85-
for (i in 2:ncol(xRb)){
86-
res = apply(pairs, 1, FUN = function(n, xRa, xRb)
87-
mt(xRa[, n[1]], xRb[, i]), xRa = xRa, xRb = xRb)
88-
res = t(res)
89-
result = rbind(result, res)
90-
}
91-
}
92-
93-
rownames(result) = paste(rep(colnames(Ra), ncol(Rb)), "to", rep(colnames(Rb), each = ncol(Ra)))
94-
colnames(result) = c("Alpha", "Beta", "Gamma")
95-
return(result)
1+
#' Market timing models
2+
#'
3+
#' Allows to estimate Treynor-Mazuy or Merton-Henriksson market timing model.
4+
#' The Treynor-Mazuy model is essentially a quadratic extension of the basic
5+
#' CAPM. It is estimated using a multiple regression. The second term in the
6+
#' regression is the value of excess return squared. If the gamma coefficient
7+
#' in the regression is positive, then the estimated equation describes a
8+
#' convex upward-sloping regression "line". The quadratic regression is:
9+
#' \deqn{R_{p}-R_{f}=\alpha+\beta (R_{b} - R_{f})+\gamma (R_{b}-R_{f})^2+
10+
#' \varepsilon_{p}}{Rp - Rf = alpha + beta(Rb -Rf) + gamma(Rb - Rf)^2 +
11+
#' epsilonp}
12+
#' \eqn{\gamma}{gamma} is a measure of the curvature of the regression line.
13+
#' If \eqn{\gamma}{gamma} is positive, this would indicate that the manager's
14+
#' investment strategy demonstrates market timing ability.
15+
#'
16+
#' The basic idea of the Merton-Henriksson test is to perform a multiple
17+
#' regression in which the dependent variable (portfolio excess return and a
18+
#' second variable that mimics the payoff to an option). This second variable
19+
#' is zero when the market excess return is at or below zero and is 1 when it
20+
#' is above zero:
21+
#' \deqn{R_{p}-R_{f}=\alpha+\beta (R_{b}-R_{f})+\gamma D+\varepsilon_{p}}{Rp -
22+
#' Rf = alpha + beta * (Rb - Rf) + gamma * D + epsilonp}
23+
#' where all variables are familiar from the CAPM model, except for the
24+
#' up-market return \eqn{D=max(0,R_{b}-R_{f})}{D = max(0, Rb - Rf)} and market
25+
#' timing abilities \eqn{\gamma}{gamma}
26+
#' @param Ra an xts, vector, matrix, data frame, timeSeries or zoo object of
27+
#' the asset returns
28+
#' @param Rb an xts, vector, matrix, data frame, timeSeries or zoo object of
29+
#' the benchmark asset return
30+
#' @param Rf risk free rate, in same period as your returns
31+
#' @param method used to select between Treynor-Mazuy and Henriksson-Merton
32+
#' models. May be any of: \itemize{ \item TM - Treynor-Mazuy model,
33+
#' \item HM - Henriksson-Merton model} By default Treynor-Mazuy is selected
34+
#' @param \dots any other passthrough parameters
35+
#' @author Andrii Babii, Peter Carl
36+
#' @seealso \code{\link{CAPM.beta}}
37+
#' @references J. Christopherson, D. Carino, W. Ferson. \emph{Portfolio
38+
#' Performance Measurement and Benchmarking}. 2009. McGraw-Hill, p. 127-133.
39+
#' \cr J. L. Treynor and K. Mazuy, "Can Mutual Funds Outguess the Market?"
40+
#' \emph{Harvard Business Review}, vol44, 1966, pp. 131-136
41+
#' \cr Roy D. Henriksson and Robert C. Merton, "On Market Timing and Investment
42+
#' Performance. II. Statistical Procedures for Evaluating Forecast Skills,"
43+
#' \emph{Journal of Business}, vol.54, October 1981, pp.513-533 \cr
44+
#' @examples
45+
#'
46+
#' data(managers)
47+
#' MarketTiming(managers[,1], managers[,8], Rf=.035/12, method = "HM")
48+
#' MarketTiming(managers[80:120,1:6], managers[80:120,7], managers[80:120,10])
49+
#' MarketTiming(managers[80:120,1:6], managers[80:120,8:7], managers[80:120,10], method = "TM")
50+
#'
51+
#' @export
52+
MarketTiming <- function (Ra, Rb, Rf = 0, method = c("TM", "HM"))
53+
{ # @author Andrii Babii, Peter Carl
54+
55+
# FUNCTION
56+
57+
Ra = checkData(Ra)
58+
Rb = checkData(Rb)
59+
if (!is.null(dim(Rf)))
60+
Rf = checkData(Rf)
61+
Ra.ncols = NCOL(Ra)
62+
Rb.ncols = NCOL(Rb)
63+
pairs = expand.grid(1:Ra.ncols, 1)
64+
method = method[1]
65+
xRa = Return.excess(Ra, Rf)
66+
xRb = Return.excess(Rb, Rf)
67+
68+
mt <- function (xRa, xRb)
69+
{
70+
switch(method,
71+
"HM" = { S = xRb > 0 },
72+
"TM" = { S = xRb }
73+
)
74+
R = merge(xRa, xRb, xRb*S)
75+
R.df = as.data.frame(R)
76+
model = lm(R.df[, 1] ~ 1 + ., data = R.df[, -1])
77+
return(coef(model))
78+
}
79+
80+
result = apply(pairs, 1, FUN = function(n, xRa, xRb)
81+
mt(xRa[, n[1]], xRb[, 1]), xRa = xRa, xRb = xRb)
82+
result = t(result)
83+
84+
if (ncol(Rb) > 1){
85+
for (i in 2:ncol(xRb)){
86+
res = apply(pairs, 1, FUN = function(n, xRa, xRb)
87+
mt(xRa[, n[1]], xRb[, i]), xRa = xRa, xRb = xRb)
88+
res = t(res)
89+
result = rbind(result, res)
90+
}
91+
}
92+
93+
rownames(result) = paste(rep(colnames(Ra), ncol(Rb)), "to", rep(colnames(Rb), each = ncol(Ra)))
94+
colnames(result) = c("Alpha", "Beta", "Gamma")
95+
return(result)
9696
}

R/StdDev.annualized.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
#' sd.multiperiod(edhec[,6,drop=FALSE],scale=3)
4646
#'
4747
#' @export StdDev.annualized sd.annualized sd.multiperiod
48-
#' @alias StdDev.annualized sd.annualized sd.multiperiod
48+
#' @aliases StdDev.annualized sd.annualized sd.multiperiod
4949
#' @rdname StdDev.annualized
5050
StdDev.annualized <- sd.annualized <- sd.multiperiod <-
5151
function (x, scale = NA, ...)

R/TreynorRatio.R

+6-6
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@
3030
#'
3131
#' data(portfolio_bacon)
3232
#' data(managers)
33-
#' round(TreynorRatio(managers[,1,drop=FALSE], managers[,8,drop=FALSE], Rf=.035/12),4)
34-
#' round(TreynorRatio(managers[,1,drop=FALSE], managers[,8,drop=FALSE], Rf = managers[,10,drop=FALSE]),4)
35-
#' round(TreynorRatio(managers[,1:6], managers[,8,drop=FALSE], Rf=.035/12),4)
36-
#' round(TreynorRatio(managers[,1:6], managers[,8,drop=FALSE], Rf = managers[,10,drop=FALSE]),4)
37-
#' round(TreynorRatio(managers[,1:6], managers[,8:7,drop=FALSE], Rf=.035/12),4)
38-
#' round(TreynorRatio(managers[,1:6], managers[,8:7,drop=FALSE], Rf = managers[,10,drop=FALSE]),4)
33+
#' round(TreynorRatio(managers[,1], managers[,8], Rf=.035/12),4)
34+
#' round(TreynorRatio(managers[,1], managers[,8], Rf = managers[,10]),4)
35+
#' round(TreynorRatio(managers[,1:6], managers[,8], Rf=.035/12),4)
36+
#' round(TreynorRatio(managers[,1:6], managers[,8], Rf = managers[,10]),4)
37+
#' round(TreynorRatio(managers[,1:6], managers[,8:7], Rf=.035/12),4)
38+
#' round(TreynorRatio(managers[,1:6], managers[,8:7], Rf = managers[,10]),4)
3939
#'
4040
#' print(TreynorRatio(portfolio_bacon[,1], portfolio_bacon[,2], modified = TRUE)) #expected 0.7975
4141
#'

R/chart.TimeSeries.R

+7-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,13 @@
135135
#' R=edhec[,"Funds of Funds",drop=FALSE]
136136
#' Return.cumulative = cumprod(1+R) - 1
137137
#' chart.TimeSeries(Return.cumulative)
138-
#' chart.TimeSeries(Return.cumulative, colorset = "darkblue", legend.loc = "bottomright", period.areas = cycles.dates, period.color = "lightblue", event.lines = risk.dates, event.labels = risk.labels, event.color = "red", lwd = 2)
138+
#' chart.TimeSeries(Return.cumulative, colorset = "darkblue",
139+
#' legend.loc = "bottomright",
140+
#' period.areas = cycles.dates,
141+
#' period.color = "lightblue",
142+
#' event.lines = risk.dates,
143+
#' event.labels = risk.labels,
144+
#' event.color = "red", lwd = 2)
139145
#'
140146
#' @export
141147
chart.TimeSeries <-

R/charts.RollingPerformance.R

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
#' @examples
2222
#'
2323
#' data(managers)
24-
#' charts.RollingPerformance(managers[,1:8], Rf=managers[,10,drop=FALSE], colorset=tim8equal, main="Rolling 12-Month Performance", legend.loc="topleft")
24+
#' charts.RollingPerformance(managers[,1:8],
25+
#' Rf=managers[,10,drop=FALSE],
26+
#' colorset=tim8equal,
27+
#' main="Rolling 12-Month Performance",
28+
#' legend.loc="topleft")
2529
#'
2630
#' @export
2731
charts.RollingPerformance <-

R/lpm.R

+6-8
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,33 @@
1919
#' @export
2020
lpm <- function(R,n=2,threshold=0,about_mean=FALSE){
2121

22+
R <- checkData(R)
2223
if(about_mean==TRUE){
2324
#Calculate Number of obs less than threshold
24-
nb = nrow(x[x<threshold,])
25+
nb = nrow(R[R<threshold,])
2526
#Calculate mean of all obs less than threshold
26-
R_avg = mean(x[x<threshold,],na.rm=T)
27+
R_avg = mean(R[R<threshold,],na.rm=T)
2728
#subset data as less than threshold
28-
x_cond = x[x<threshold,]
29+
x_cond = R[R<threshold,]
2930
#Calculate LPM
3031

3132
LPM = (1/nb) * sum(max(0,R_avg-x_cond)^n)
3233
} else {
3334

3435
#Calculate Number of obs less than threshold
35-
nb = nrow(x[x<threshold,])
36+
nb = nrow(R[R<threshold,])
3637
#Calculate mean of all obs less than threshold
3738
R_avg = threshold
3839
#subset data as less than threshold
39-
x_cond = x[x<threshold,]
40+
x_cond = R[R<threshold,]
4041
#Calculate LPM
4142

4243
LPM = (1/nb) * sum(max(0,R_avg-x_cond)^n)
4344

4445
}
45-
4646
return(LPM)
4747
}
4848

49-
50-
5149
###############################################################################
5250
# R (http://r-project.org/) Econometrics for Performance and Risk Analysis
5351
#

R/table.AnnualizedReturns.R

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@
2323
#' require("Hmisc")
2424
#' result = t(table.AnnualizedReturns(managers[,1:8], Rf=.04/12))
2525
#'
26-
#' textplot(format.df(result, na.blank=TRUE, numeric.dollar=FALSE, cdec=c(3,3,1)), rmar = 0.8, cmar = 2, max.cex=.9, halign = "center", valign = "top", row.valign="center", wrap.rownames=20, wrap.colnames=10, col.rownames=c("red", rep("darkgray",5), rep("orange",2)), mar = c(0,0,3,0)+0.1)
26+
#' textplot(format.df(result, na.blank=TRUE, numeric.dollar=FALSE,
27+
#' cdec=c(3,3,1)), rmar = 0.8, cmar = 2, max.cex=.9,
28+
#' halign = "center", valign = "top", row.valign="center",
29+
#' wrap.rownames=20, wrap.colnames=10, col.rownames=c("red",
30+
#' rep("darkgray",5), rep("orange",2)), mar = c(0,0,3,0)+0.1)
31+
#'
2732
#' title(main="Annualized Performance")
2833
#'
2934
#' @export

R/table.Arbitrary.R

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@
6363
#' @examples
6464
#'
6565
#' data(edhec)
66-
#' table.Arbitrary(edhec,metrics=c("VaR", "ES"),metricsNames=c("Modified VaR","Modified Expected Shortfall"))
66+
#' table.Arbitrary(edhec,metrics=c("VaR", "ES"),
67+
#' metricsNames=c("Modified VaR","Modified Expected Shortfall"))
6768
#'
6869
#' @export
6970
table.Arbitrary <-

R/table.Autocorrelation.R

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
#' t(table.Autocorrelation(managers))
2525
#'
2626
#' result = t(table.Autocorrelation(managers[,1:8]))
27-
#' textplot(result, rmar = 0.8, cmar = 2, max.cex=.9, halign = "center", valign = "top", row.valign="center", wrap.rownames=15, wrap.colnames=10, mar = c(0,0,3,0)+0.1)
27+
#'
28+
#' textplot(result, rmar = 0.8, cmar = 2, max.cex=.9, halign = "center",
29+
#' valign = "top", row.valign="center", wrap.rownames=15,
30+
#' wrap.colnames=10, mar = c(0,0,3,0)+0.1)
31+
#'
2832
#' title(main="Autocorrelation")
2933
#'
3034
#' @export

0 commit comments

Comments
 (0)