0

This is a sample code that uses a similar data set:

library(portfolio)
p <- new("portfolioBasic", instant = as.Date("2004-12-31"), id.var = "symbol",  
         in.var = "price", sides = "long", 
         ret.var = "month.ret", data = dow.jan.2005)
a <- performance(p)@ret
b <- log(1 + a)
sum <- b + sum #the sum variable should accumulate all returns

The example above is very similar to the code I'm working on, except that it uses the dow.jan.2005 that's included in the portfolio library.

I want to create a loop in R that calculates a certain function p <- new(... , data = "data20xx") and this data20xx that is used in the function should go from 2007 to 2017.

There are also two other functions that follow p. The performance function performance(p) calculates a percentage which then needs to be logarithmized and stored in a separate variable b. The sum variables keeps track of the cumulative log returns.

Here's the description of the performance function:

   Formal class 'performance' [package "portfolio"] with 6 slots
      ..@ ret           : num -0.366
      ..@ profit        : num 0
      ..@ missing.price : num NA
      ..@ missing.return: int 0

If i use performance(p)@ret I get a number, but I can't use the logarithm on it.

How can I create this specific loop?

DJack
  • 4,850
  • 3
  • 21
  • 45
  • 2
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick May 02 '18 at 18:40
  • oh man! I wanna help you but can't. You should provide more information. – YOLO May 02 '18 at 18:48
  • I've updated my initial post with an example – Lumberjack88 May 02 '18 at 19:28
  • 2
    Are you aware of the simple mathematical fact that there is no defined value for the log of a negative number???? – IRTFM May 02 '18 at 19:37
  • For the first issue, you can use a list as `data <- list(data2007, ..., data2017)` and either use `lapply` or a loop with `data = data[[i]]` – DJack May 02 '18 at 19:51
  • @42 Sorry, I missed that part. Now I've added the correct log return formula. – Lumberjack88 May 02 '18 at 20:27
  • @DJack could you provide a full example, please? – Lumberjack88 May 02 '18 at 20:28

2 Answers2

0

An approach is to put your data frames into a list and apply your function to each element of this list using lapply.

data <- list(data2005, data2006, data2007)

myfun <- function(x){
  p <- new("portfolioBasic", 
           instant = as.Date("2004-12-31"), 
           id.var = "symbol", in.var = "price",
           sides = "long", ret.var = "month.ret", data = x)
  a <- performance(p)@ret
  b <- log(1 + a)
  b
}

output <- lapply(data, myfun)

sum <- do.call("sum", output)

Sample data

library(portfolio)

set.seed(1)

data(dow.jan.2005)

data2007 <- data2006 <- data2005 <- dow.jan.2005
data2006$price <- runif(nrow(dow.jan.2005), 30, 100)
data2007$price <- runif(nrow(dow.jan.2005), 30, 100)
DJack
  • 4,850
  • 3
  • 21
  • 45
0

Here's my own answer that I came up with after DJack's suggestions (thanks DJack!):

mydata <- list(spxdata2007,spxdata2008 etc.)
p <- lapply(mydata, function(x) log(1+performance(new("portfolioBasic", id.var = "symbol", in.var = "inversepe", type = "linear", sides = c("long", "short"), ret.var = "return", data = x))@ret))
exp(Reduce("+",p))-1