[R-SIG-Finance] xts 'order.by' cannot contain 'NA', 'NaN', or 'Inf' in optimize.portfolio.rebalancing
Amit Mittal
prof@@mit@mitt@l @ending from gm@il@com
Wed Sep 26 08:54:18 CEST 2018
Where you are using data / data.frame use na.omit(data) na.omit(df) instead it will remove the rows with NAs or any market/asset. If you want to replace with zeroes etc instead of losing rows where NAs are included in your data, there are other options to transform missing data using is.na()
https://stats.idre.ucla.edu/r/faq/how-does-r-handle-missing-values/
Best Regards
______________________________
Amit Mittal
Ph.D. in Finance and Accounting (tbd)
Indian Institute of Management, Lucknow
http://ssrn.com/author=2665511
Mob: +91 7899381263
______________________________
________________________________
From: R-SIG-Finance <r-sig-finance-bounces using r-project.org> on behalf of Simon Hovmark <simonhovmark using gmail.com>
Sent: Wednesday, September 26, 2018 2:33:12 AM
To: r-sig-finance using r-project.org
Subject: [R-SIG-Finance] xts 'order.by' cannot contain 'NA', 'NaN', or 'Inf' in optimize.portfolio.rebalancing
I am trying to run the following optimize.portfolio.rebalancing:
opt <- optimize.portfolio.rebalancing(R=returns, portfolio=tranch1,
optimize_method="ROI",
#momentFUN = tranch1_boudt,
rebalance_on = rebal.freq,
training_period = training.period,
rolling_window = rolling.window)
But when I use summary(opt) I get the following error:
xts(x, order.by = order.by, frequency = frequency, ...) :'order.by' cannot contain 'NA', 'NaN', or 'Inf'
I can see that other has had a similar problem, but I've not been able to solve it using their answers. When I sum NA, NaN and InF on returns$dato I get 0.
A subset of my data is here:
dato stock_1 stock_2 stock_3
1999-10-14 -0.002006019 0.016164145 -100
1999-10-15 0.000000000 0.000000000 -100
1999-10-18 -0.036813973 -0.049017341 -100
1999-10-19 0.016529302 0.000000000 -100
1999-10-20 0.016260521 0.011996238 -100
1999-10-21 0.008032172 0.005806736 -100
1999-10-22 0.000000000 0.000000000 -100
1999-10-25 0.039220713 0.023164955 -100
1999-10-26 0.028437935 0.002152853 -100
1999-10-27 -0.032291505 0.014941580 -100
1999-10-28 0.030420597 0.011061477 -100
1999-10-29 0.000000000 0.000000000 -100
1999-11-02 0.027702603 -0.003410734 -100
1999-11-03 0.007259560 -0.007650743 -100
1999-11-04 0.003610112 0.000000000 -100
1999-11-05 0.000000000 0.000000000 -100
1999-11-08 0.014311514 0.005546033 -100
1999-11-09 0.007079676 -0.002373106 -100
1999-11-10 0.039763233 0.024512309 -100
1999-11-11 -0.001696353 -0.018721296 -100
1999-11-12 0.000000000 0.000000000 -100
And here is my full code.
rebal.freq <- "years"
training.period <- 0
rolling.window <- 120
returns <- read_excel("HEX.xlsx", sheet = 1, col_names = TRUE)
returns <- xts(returns[,-1], order.by= returns[,1])
returns <- Return.calculate(returns, method = "log")
returns <- returns[-1,]
returns[!is.finite(returns)] <- NA
returns[!is.finite(returns)] <- NA
returns <- na.fill(returns, fill = -100)
sum(is.nan(returns$dato)) #returns 0
sum(is.infinite(returns$dato)) #returns 0
sum(is.na(returns$dato)) #returns 0
fund.names <- colnames(returns)
tranch1 <- portfolio.spec(assets = fund.names)
tranch1 <- add.constraint(portfolio = tranch1, type = "leverage")
tranch1 <- add.constraint(portfolio = tranch1, type = "long_only")
tranch1 <- add.objective(portfolio=tranch1, type="return", name="mean")
tranch1 <- add.objective(portfolio=tranch1, type="risk", name="StdDev")
opt <- optimize.portfolio.rebalancing(R=returns, portfolio=tranch1,
optimize_method="ROI",
#momentFUN = tranch1_boudt,
rebalance_on = rebal.freq,
training_period = training.period,
rolling_window = rolling.window)
summary(opt)
And my sessioninfo:
R version 3.3.1 (2016-06-21)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.13.3 (unknown)
locale:
[1] C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] RColorBrewer_1.1-2 readxl_0.1.1 DEoptimR_1.0-8
[4] PortfolioAnalytics_1.0.3636 PerformanceAnalytics_1.4.3541 foreach_1.4.4
[7] xts_0.10-1 zoo_1.7-13
loaded via a namespace (and not attached):
[1] compiler_3.3.1 parallel_3.3.1 tools_3.3.1 Rcpp_0.12.9 codetools_0.2-14
[6] grid_3.3.1 iterators_1.0.8 DEoptim_2.2-4 lattice_0.20-34
EDIT When I read in the Excel file, then class(returns$Dato) returns
class(returns$Dato) [1] "POSIXct" "POSIXt"
Then instead of the below
returns <- xts(returns[,-1], order.by= returns[,1])
I tried using
returns <- xts(returns[, -1], order.by=as.Date(paste(returns$Dato, "%m/%d/%Y")))
and run the optimization but summary(opt) again returned
xts(x, order.by = order.by, frequency = frequency, ...) :�order.by' cannot contain 'NA', 'NaN', or 'Inf'
[[alternative HTML version deleted]]
_______________________________________________
R-SIG-Finance using r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.
[[alternative HTML version deleted]]
More information about the R-SIG-Finance
mailing list