PerformanceAnalytics
PerformanceAnalytics copied to clipboard
Issue with chart.RiskReturnScatter in performance analytics, need finite 'ylim' values error
I am currently trying to create a Risk Return Scatter plot using the following code:
x <- performance[x:y, portfolio.list]
chart.RiskReturnScatter(x, Rf = rf, main = "", cex.axis = 1.5, cex.lab = 1.5)
The performance[x:y, portfolio.list]
dataframe is formatted as:
HR Muni Bond HR Taxable Bond Composite Portfolio
2017-12-31 NA -0.006 -0.0025071641
2018-03-31 NA -0.003 -0.0012671892
2018-06-30 NA 0.007 0.0028773074
2018-09-30 NA -0.001 -0.0004108567
I modified my code to replace the NA's with zeros thinking that this would correct the error:
x <- performance[x:y, portfolio.list]
x[is.na(performance[x:y, portfolio.list])] <- 0
chart.RiskReturnScatter(x, Rf=rf, main="", cex.axis = 1.5, cex.lab = 1.5)
Nope, that didn't fix the issue. I received the following error: "Error in checkData(R): The data cannot be converted into a time series. If you are trying to pass in names from a data object with one column, you should use the form 'data[rows, columns, drop = FALSE]'. Rownames should have standard date formats, such as '1985-03-15'."
I went back to the drawing board and added tk_xts
to coerce the dataframe back to the xts format using this code:
x <- performance[x:y, portfolio.list]
x[is.na(performance[x:y, portfolio.list])] <- 0
chart.RiskReturnScatter(tk_xts(x, by 1), Rf=rf, main="", cex.axis = 1.5, cex.lab = 1.5)
I think I am up to strike 3. I now get this error: "Error in xts::xts(data, ...) : order.by requires an appropriate time-based object"
I am not sure where to proceed next. Any suggestions are greatly appreciated. Thank you.
I cannot replicate an error using the simple example below. I tried using both the CRAN version and development version of PerformanceAnalytics.
library(PerformanceAnalytics)
data(managers)
x <- managers[, 2:4]
rf <- managers[, "US 3m TR"]
chart.RiskReturnScatter(x, Rf = rf, main = "", cex.axis = 1.5, cex.lab = 1.5)
Please provide a reproducible example and the output from sessionInfo()
. It's not clear what the performance
object truly is (xts/zoo, data.frame, tibble, etc),
Here is my sessionInfo()
:
R> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.1 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] PerformanceAnalytics_1.5.2.2 xts_0.11-2.1
[3] zoo_1.8-4
loaded via a namespace (and not attached):
[1] compiler_3.5.1 tools_3.5.1 grid_3.5.1 lattice_0.20-38
[5] quadprog_1.5-5
I just noticed that you show data with only 4 rows. I'm not sure if that's for illustrative purposes, or if you are only passing 4 rows to chart.RiskReturnScatter()
.
y <- x[seq(0, nrow(x), 3),]
y <- y[1:4,]
y[, 1] <- NA
chart.RiskReturnScatter(y, Rf = rf, main = "", cex.axis = 1.5, cex.lab = 1.5)
# Error in plot.window(...) : need finite 'xlim' values
The error is xlim
instead of ylim
, but that's an actual error close to your issue. Now to replace the NA
with zero.
z <- y
z[,1] <- 0
chart.RiskReturnScatter(z, Rf = rf, main = "", cex.axis = 1.5, cex.lab = 1.5)
And the above code works. I have no idea what tk_xts()
is, so I can't help with that.
I would be happy to continue investigating if you can provide a reproducible example.
Hi Joshua - I apologize for taking up your time with my shoddy example. I am still getting my feet wet but this became a great learning lesson for me. Here is I hope a better example. Thank you for your patience.
library("PerformanceAnalytics")
Periods <- c(1, 3, 5)
Plot <- 2L
Portfolios <- list(c(3, 5, 9), 9, NULL)
rf <- 0.009585192
risk.free.rate <- structure(c(0, 0.0016, 0.002, 0.0028, 0.0029, 0.0051,
0.0076, 0.0103, 0.0106, 0.0139, 0.0173, 0.0193, 0.0219), .Dim = c(13L,
1L), .Dimnames = list(NULL, "RiskFree"), index = structure(c(1443571200,
1451520000, 1459382400, 1467244800, 1475193600, 1483142400, 1490918400,
1498780800, 1506729600, 1514678400, 1522454400, 1530316800, 1538265600),
tzone = "UTC", tclass = "Date"), class = c("xts", "zoo"), .indexCLASS = "Date",
tclass = "Date", .indexTZ = "UTC", tzone = "UTC")
performance <- structure(c(0.011, 0.004, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 0.012, 0.007, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 0.009, 0.01, -0.002, -0.017, 0.005, 0.0073, NA, 0, 0, 0, 0,
NA, NA, 0.011, 0.011, -0.001, -0.022, 0.014, 0.0117, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, 0.008, 0.003, -0.006, -0.003, 0.007,
-0.001, NA, NA, NA, NA, NA, NA, NA, 0.0117, 0.006, -0.005, -0.006,
0.007, -0.001, NA, NA, NA, NA, NA, NA, NA, 0.0094, 0.006, -0.001, -0.01,
0.003, 0.006, NA, NA, NA, NA, NA, NA, NA, 0.0094, 0.00601457, -0.002,
-0.01, 1e-04, 0.002, 0.011, 0.004, 0, 0, 0, 0, 0, 0.0075690687367229,
0.00129339236892773, -0.00250716410500521, -0.00126718922198639, 0.00287730741378422,
-0.000410856687915031, 0.012, 0.007, 0, 0, 0, 0, 0, 0.0117, 0.00258678473785546,
-0.00208930342083768, -0.00253437844397278, 0.00287730741378422, -0.000410856687915031),
.Dim = c(13L, 10L), .Dimnames = list(NULL, c("HR Muni plus cash", "BofA ML 1-10 year AAA-A Muni",
"HR Muni Bond", "BofA ML 1-10 year AAA-A Muni", "HR Taxable Bond",
"BC Interm. Gov/Credit", "HR Muni w/o cash", "BofA ML 1-10 year AAA-A Muni",
"Composite Portfolio", "Composite Benchmark")), index = structure(c(1443571200,
1451520000, 1459382400, 1467244800, 1475193600, 1483142400, 1490918400,
1498780800, 1506729600, 1514678400, 1522454400, 1530316800, 1538265600),
tzone = "UTC", tclass = "Date"), class = c("xts", "zoo"), .indexCLASS = "Date",
tclass = "Date", .indexTZ = "UTC", tzone = "UTC")
portfolio <- structure(c(0.009, 0.01, -0.002, -0.017, 0.005, 0.0073, 0,
0, 0, 0), index = structure(c(1459382400, 1467244800, 1475193600, 1483142400,
1490918400, 1498780800, 1514678400, 1522454400, 1530316800, 1538265600),
tzone = "UTC", tclass = "Date"), .Dim = c(10L, 1L), .Dimnames = list(NULL,
"HR Muni Bond"), class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date",
.indexTZ = "UTC", tzone = "UTC", na.action = structure(c(1L, 2L, 9L),
class = "omit", index = c(1443571200, 1451520000, 1506729600)))
num.quarters = nrow(performance)
# Risk Return Scatter Plots (1,3 & 5yr)
for (Plot in 1:length(Periods)) {
Period.Qtr = Periods[Plot] * 4
portfolio.list = Portfolios[[Plot]]
if (length(portfolio.list) > 0) {
x = num.quarters - Period.Qtr + 1
y = num.quarters
rf = as.numeric((1 + Return.annualized(risk.free.rate[x:y, ]))^0.25 -
1)
layout(c(1, 1))
chart.RiskReturnScatter(performance[x:y, portfolio.list][is.na(performance[x:y,
portfolio.list])] <- 0, Rf = rf, main = "", cex.axis = 1.5,
cex.lab = 1.5)
p = captureplot()
}
}
Session Info:
sessionInfo() R version 3.5.1 (2018-07-02) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 17763)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages: [1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] PerformanceAnalytics_1.5.2 xts_0.11-1 zoo_1.8-3
[4] RevoUtils_11.0.1 RevoUtilsMath_11.0.0
loaded via a namespace (and not attached): [1] compiler_3.5.1 tools_3.5.1 grid_3.5.1 lattice_0.20-35 quadprog_1.5-5
Hi Joshua - I apologize for taking up your time with my shoddy example. I am still getting my feet wet but this became a great learning lesson for me. Here is I hope a better example. I have also attached the error dump log in this email because the file format is not supported in Github. Thank you for your patience.
library("PerformanceAnalytics")
Periods <- c(1, 3, 5) Plot <- 2L Portfolios <- list(c(3, 5, 9), 9, NULL) rf <- 0.009585192 risk.free.rate <- structure(c(0, 0.0016, 0.002, 0.0028, 0.0029, 0.0051, 0.0076, 0.0103, 0.0106, 0.0139, 0.0173, 0.0193, 0.0219), .Dim = c(13L, 1L), .Dimnames = list(NULL, "RiskFree"), index = structure(c(1443571200, 1451520000, 1459382400, 1467244800, 1475193600, 1483142400, 1490918400, 1498780800, 1506729600, 1514678400, 1522454400, 1530316800, 1538265600), tzone = "UTC", tclass = "Date"), class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC")
performance <- structure(c(0.011, 0.004, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.012, 0.007, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.009, 0.01, -0.002, -0.017, 0.005, 0.0073, NA, 0, 0, 0, 0, NA, NA, 0.011, 0.011, -0.001, -0.022, 0.014, 0.0117, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.008, 0.003, -0.006, -0.003, 0.007, -0.001, NA, NA, NA, NA, NA, NA, NA, 0.0117, 0.006, -0.005, -0.006, 0.007, -0.001, NA, NA, NA, NA, NA, NA, NA, 0.0094, 0.006, -0.001, -0.01, 0.003, 0.006, NA, NA, NA, NA, NA, NA, NA, 0.0094, 0.00601457, -0.002, -0.01, 1e-04, 0.002, 0.011, 0.004, 0, 0, 0, 0, 0, 0.0075690687367229, 0.00129339236892773, -0.00250716410500521, -0.00126718922198639, 0.00287730741378422, -0.000410856687915031, 0.012, 0.007, 0, 0, 0, 0, 0, 0.0117, 0.00258678473785546, -0.00208930342083768, -0.00253437844397278, 0.00287730741378422, -0.000410856687915031), .Dim = c(13L, 10L), .Dimnames = list(NULL, c("HR Muni plus cash", "BofA ML 1-10 year AAA-A Muni", "HR Muni Bond", "BofA ML 1-10 year AAA-A Muni", "HR Taxable Bond", "BC Interm. Gov/Credit", "HR Muni w/o cash", "BofA ML 1-10 year AAA-A Muni", "Composite Portfolio", "Composite Benchmark")), index = structure(c(1443571200, 1451520000, 1459382400, 1467244800, 1475193600, 1483142400, 1490918400, 1498780800, 1506729600, 1514678400, 1522454400, 1530316800, 1538265600), tzone = "UTC", tclass = "Date"), class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC")
portfolio <- structure(c(0.009, 0.01, -0.002, -0.017, 0.005, 0.0073, 0, 0, 0, 0), index = structure(c(1459382400, 1467244800, 1475193600, 1483142400, 1490918400, 1498780800, 1514678400, 1522454400, 1530316800, 1538265600), tzone = "UTC", tclass = "Date"), .Dim = c(10L, 1L), .Dimnames = list(NULL, "HR Muni Bond"), class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", na.action = structure(c(1L, 2L, 9L), class = "omit", index = c(1443571200, 1451520000, 1506729600)))
num.quarters = nrow(performance)
Risk Return Scatter Plots (1,3 & 5yr)
for (Plot in 1:length(Periods)) {
Period.Qtr = Periods[Plot] * 4
portfolio.list = Portfolios[[Plot]]
if (length(portfolio.list) > 0) {
x = num.quarters - Period.Qtr + 1
y = num.quarters
rf = as.numeric((1 + Return.annualized(risk.free.rate[x:y, ]))^0.25 -
1)
layout(c(1, 1))
chart.RiskReturnScatter(performance[x:y,
portfolio.list][is.na(performance[x:y, portfolio.list])] <- 0, Rf = rf, main = "", cex.axis = 1.5, cex.lab = 1.5)
p = captureplot()
}
}
On Sun, Nov 18, 2018 at 7:56 AM Joshua Ulrich [email protected] wrote:
I cannot replicate an error using the simple example below. I tried using both the CRAN version and development version of PerformanceAnalytics.
library(PerformanceAnalytics) data(managers)x <- managers[, 2:4]rf <- managers[, "US 3m TR"] chart.RiskReturnScatter(x, Rf = rf, main = "", cex.axis = 1.5, cex.lab = 1.5)
Please provide a reproducible example https://stackoverflow.com/q/5963269/271616 and the output from sessionInfo(). It's not clear what the performance object truly is (xts/zoo, data.frame, tibble, etc),
Here is my sessionInfo():
R> sessionInfo() R version 3.5.1 (2018-07-02) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 18.04.1 LTS
Matrix products: default BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1 LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=en_US.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages: [1] stats graphics grDevices utils datasets methods base
other attached packages: [1] PerformanceAnalytics_1.5.2.2 xts_0.11-2.1 [3] zoo_1.8-4
loaded via a namespace (and not attached): [1] compiler_3.5.1 tools_3.5.1 grid_3.5.1 lattice_0.20-38 [5] quadprog_1.5-5
I just noticed that you show data with only 4 rows. I'm not sure if that's for illustrative purposes, or if you are only passing 4 rows to chart.RiskReturnScatter().
y <- x[seq(0, nrow(x), 3),]y <- y[1:4,]y[, 1] <- NA chart.RiskReturnScatter(y, Rf = rf, main = "", cex.axis = 1.5, cex.lab = 1.5) # Error in plot.window(...) : need finite 'xlim' values
The error is xlim instead of ylim, but that's an actual error close to your issue. Now to replace the NA with zero.
z <- yz[,1] <- 0 chart.RiskReturnScatter(z, Rf = rf, main = "", cex.axis = 1.5, cex.lab = 1.5)
And the above code works. I have no idea what tk_xts() is, so I can't help with that.
I would be happy to continue investigating if you can provide a reproducible example https://stackoverflow.com/q/5963269/271616.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/braverock/PerformanceAnalytics/issues/107#issuecomment-439690873, or mute the thread https://github.com/notifications/unsubscribe-auth/APWzEQnKm1u3nVFuuqNOvyJ_WZCjFcL9ks5uwVkDgaJpZM4YasgB .