I have a simple problem and would be grateful for anyone's advice.
I am working on an investment portfolio analysis problem and have created a time series object that includes daily returns data of 100 stocks. I am trying to create 100 portfolios each with k number of stocks (i.e. portfolio 1 only includes stock 1, portfolio 2 includes stocks 1 and 2, portfolio 3 includes stocks 1, 2 and 3, etc.). My idea is to write a for loop using cbind to define each portfolio (as each column represents one stock, we can group the columns to create a new object that includes returns data for each stock in that portfolio). Unfortunately my code isn't working and I would be grateful for anyone's help in correcting my code or suggesting a better way to achieve the same result.
This is the code that I have been trying:
port1 <- returns$X1 #defines the first portfolio, which only includes data from the first column (stock 1)
for(i in 2:100)
{port[i] <- cbind(port[i-1],returns$Xi)}
Please find below my initial code:
prices <- read.csv("ass2data.csv") #import data
prices$Date <- as.Date(prices$Date, format("%d/%m/%Y"))
prices <- prices[order(prices$Date), ]
prices <- as.xts(prices[, 2:101], order.by = prices$Date) #transform data table into time series in order to conduct returns analysis
returns <- Return.calculate(prices) #create variable returns
returns <- returns[-1, ] #remove the first row of returns (which are NA)
colnames(returns) <- paste0("X", 1:100) #update column names
This is a snapshot of the returns data for the first five stocks:
> head(returns)
X1 X2 X3 X4 X5
2014-01-02 0.003270007 0.003266847 0.002258356 0.002421568 0.003186671
2014-01-03 -0.018734046 -0.019517029 -0.021293375 -0.016185312 -0.018242174
2014-01-06 -0.001660788 -0.002475299 -0.003453436 -0.014787330 -0.002424078
2014-01-07 0.009156889 -0.001674973 -0.006930807 -0.013347733 -0.002429969
2014-01-08 0.002465390 0.006669705 -0.002210073 0.004210054 0.004871776
2014-01-09 -0.013155214 -0.006625514 -0.003497319 -0.004192403 -0.008884820
And this is a sample of what the result should be for the third portfolio:
X1 X2 X3
2014-01-02 0.003270007 0.003266847 0.002258356
2014-01-03 -0.018734046 -0.019517029 -0.021293375
2014-01-06 -0.001660788 -0.002475299 -0.003453436
2014-01-07 0.009156889 -0.001674973 -0.006930807
2014-01-08 0.002465390 0.006669705 -0.002210073
2014-01-09 -0.013155214 -0.006625514 -0.003497319
Thank you!!