xts 'order.by' cannot contain 'NA', 'NaN', or 'Inf' in optimize.portfolio.rebalancing

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

xts 'order.by' cannot contain 'NA', 'NaN', or 'Inf' in optimize.portfolio.rebalancing

SimonHovmark
This post was updated on .
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@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.
Reply | Threaded
Open this post in threaded view
|

Re: xts 'order.by' cannot contain 'NA', 'NaN', or 'Inf' in optimize.portfolio.rebalancing

Enrico Schumann-2
On Di, 25 Sep 2018, Simon Hovmark writes:

> 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]]
>

Your example is not reproducible: you should post the
data that causes the problems; e.g. by using
?dput. (But if the dataset is large, try find a
reasonably small subset that causes the problem and
send only the subset.)

You may check the dates with

    any(!is.finite(returns$dato))
    which(!is.finite(returns$dato))



--
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: xts 'order.by' cannot contain 'NA', 'NaN', or 'Inf' in optimize.portfolio.rebalancing

prof.amit.mittal
In reply to this post by SimonHovmark


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 <[hidden email]> on behalf of Simon Hovmark <[hidden email]>
Sent: Wednesday, September 26, 2018 2:33:12 AM
To: [hidden email]
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]]

_______________________________________________
[hidden email] 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]]


_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: xts 'order.by' cannot contain 'NA', 'NaN', or 'Inf' in optimize.portfolio.rebalancing

braverock
In reply to this post by SimonHovmark
As Amit already said, zero is a better answer than -100.  log returns
don't behave well with -100x compounding returns.  This is what is
causing Inf/-Inf problems.  there is a 'zerofill' utility function
available in PerformanceAnalytics to make this replacement easier.  

see:

?zerofill

If you are *also* having a problem with order.by, your sample data
would b e insufficient to reproduce it, since the index in the subset
you pasted seems well-formed.

If Amit's and my suggestion to replace NA with 0 in your returns
doesn't solve the problem, then please reply to this thread with a
fully reproducible example (including data e.g. using dput function the
or reprex package).

Regards,

Brian
 
--
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock

On Tue, 2018-09-25 at 17:03 -0400, Simon Hovmark wrote:

> 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     forea
> ch_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]]
>
> _______________________________________________
> [hidden email] 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.
>

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: xts 'order.by' cannot contain 'NA', 'NaN', or 'Inf' in optimize.portfolio.rebalancing

SimonHovmark
So I got it to work even with -100x compounding returns and I’ve have tried to figure out why, so I could leave a solution. But no luck.

But just som intuition on why I replace NAs with -100. I’ve a dataset of 14 different European stock indices from 2000 to present with the historical components. However, as some stocks are not always in the indices they should also not always be part of the optimization. PerformanceAnalytics replaces per default NAs with 0’s which means that even though there are no return value for the excluded stock, it is still (wrongfully) included in the optimization every month because it just looks like a 0% return.

I tried to find a way to work around this using a constraint but it is my understanding that constraints in PerformanceAnalytics cannot be customized. So my very ugly solution was to replace NAs with -100, so the stocks in that way would get penalized in the optimization. However, this is far from ideal and do also not work perfectly.

Any suggestions on how to solve this problem or should I create a new thread?

-Simon

> Den 26. sep. 2018 kl. 08.05 skrev braverock [via R] <[hidden email]>:
>
> As Amit already said, zero is a better answer than -100.  log returns
> don't behave well with -100x compounding returns.  This is what is
> causing Inf/-Inf problems.  there is a 'zerofill' utility function
> available in PerformanceAnalytics to make this replacement easier.  
>
> see:
>
> ?zerofill
>
> If you are *also* having a problem with order.by, your sample data
> would b e insufficient to reproduce it, since the index in the subset
> you pasted seems well-formed.
>
> If Amit's and my suggestion to replace NA with 0 in your returns
> doesn't solve the problem, then please reply to this thread with a
> fully reproducible example (including data e.g. using dput function the
> or reprex package).
>
> Regards,
>
> Brian
>  
> --
> Brian G. Peterson
> http://braverock.com/brian/ <http://braverock.com/brian/>
> Ph: 773-459-4973
> IM: bgpbraverock
>
> On Tue, 2018-09-25 at 17:03 -0400, Simon Hovmark wrote:
>
> > 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     forea
> > ch_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]]
> >
> > _______________________________________________
> > [hidden email] <x-msg://73/user/SendEmail.jtp?type=node&node=4752761&i=0> mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-sig-finance <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.
> >
>
> _______________________________________________
> [hidden email] <x-msg://73/user/SendEmail.jtp?type=node&node=4752761&i=1> mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance <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.
>
>
> If you reply to this email, your message will be added to the discussion below:
> http://r.789695.n4.nabble.com/xts-order-by-cannot-contain-NA-NaN-or-Inf-in-optimize-portfolio-rebalancing-tp4752748p4752761.html <http://r.789695.n4.nabble.com/xts-order-by-cannot-contain-NA-NaN-or-Inf-in-optimize-portfolio-rebalancing-tp4752748p4752761.html>
> To unsubscribe from xts 'order.by' cannot contain 'NA', 'NaN', or 'Inf' in optimize.portfolio.rebalancing, click here <
> NAML <
http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>

        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] 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.