JB17
JB17

Reputation: 65

PortfolioAnalytics Error row names not Dates

I am getting the following error from the portfolio analytics package.

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'. 

The data set I am using is simulated data

> df
      X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
 [1,]  0  1  0  1  0  0  0  1  1   0
 [2,]  0  1  0  0  1  1  1  1  1   1
 [3,]  1  0  0  0  0  0  0  1  1   0
 [4,]  1  0  1  1  1  0  0  1  0   1
 [5,]  0  0  1  0  1  0  1  1  1   0
 [6,]  0  1  0  1  0  1  1  0  1   1
 [7,]  1  0  0  0  0  1  1  1  1   0
 [8,]  0  0  1  1  0  0  1  1  0   1
 [9,]  1  0  0  0  0  1  1  1  1   0
[10,]  0  1  1  0  0  1  0  1  0   0

I set the portfolio constraints to be

returns = as.matrix(df)
> funds = colnames(df)
> init.portfolio <- portfolio.spec(assets = funds)
> init.portfolio <- add.constraint(portfolio = init.portfolio, type = "full_investment")
> init.portfolio <- add.constraint(portfolio = init.portfolio, type = "long_only")
> minSD.portfolio <- add.objective(portfolio=init.portfolio, 
+                                  type="risk", 
+                                  name="StdDev")
> minSD.opt <- optimize.portfolio(R = df, portfolio = minSD.portfolio, 
+     optimize_method = "ROI", trace = TRUE)
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'. 

How can I fix this error. DF is a simulation of single period returns. So they are all eithe 100% or 0%, and for the same period. I can add a date variable if I need to as the row names, but I do not know how. I tried

> rownames(df) = as.Date(c("Jan", rep(nrow(df))))
Error in charToDate(x) : 
  character string is not in a standard unambiguous format

Can someone help me with this error? Thanks

Upvotes: 0

Views: 1078

Answers (1)

WaltS
WaltS

Reputation: 5530

You'll need to add date data to df. Assuming that the data is monthly returns beginning at the start of this year, you can either add rownames using

rownames(df) <- as.character(seq(as.Date("2015-01-01"), length.out=nrow(df), by = "month"))

or convert df to an xts time series by

library(xts)
df <- xts(df, order.by = seq(as.Date("2015-01-01"), 
                                  length.out=nrow(df), by = "month"), df)

xts is a commonly used format for financial time series and works well with PortfolioAnalytics so you might consider that.

Once you've done that and run optimize.portfolio, you'll won't get an solution. It appears that df is not positive definite so you'll have to adjust the values in df.

Also I don't quite understand your comment that single period returns require that returns be 0 or 1. That's not true in general.

Upvotes: 2

Related Questions