0

Basically, I'm trying to create a portfolio data frame, where the coefficient estimates of the linear regressions are lagged. I've seen many similar issues like mine on stack overflow, but I can't understand what is meant when they say that the two vectors are of different length (I'm still a bit new to R and programming). Hopefully someone can better explain it for my situation.

I think the potential root to my problem is that before this code, Returns had 120 time-series observations and Dates had 121.

Portfolio_dataframe <-  data.frame(Dates = Returns$Date[-c(1:Lookback)][1:(Periods*Holding)],
                                   Market = Returns$Market.Close[-c(1:Lookback)][1:(Periods*Holding)],
                                   Low_beta = unlist(Map(Holding_Period_Calculations, Holding_list, Portfolio_names,Portfolio_number = 1)),
                                   Low_beta2 = unlist(Map(Holding_Period_Calculations, Holding_list, Portfolio_names,Portfolio_number = 2)),
                                   Medium_beta = unlist(Map(Holding_Period_Calculations, Holding_list, Portfolio_names,Portfolio_number = 3)),
                                   High_beta4 = unlist(Map(Holding_Period_Calculations, Holding_list, Portfolio_names,Portfolio_number = 4)),
                                   High_beta = unlist(Map(Holding_Period_Calculations, Holding_list, Portfolio_names,Portfolio_number = 5)))

Lastly, the code runs well on my script, but the markdown fails to print this^^ section. enter image description here

Kgosi
  • 33
  • 5
  • Aside - `Map` + `unlist` = `mapply`. – Parfait Jun 07 '22 at 00:48
  • Hi, welcome to SO. It is a good practice to avoid including error messages or codes as images. You should add them as text inside your question, to be easier to be assessed. Regarding the question, you mentioned the codes run well in markdown, that the error was raised just when you attempt to knit it? A possible reason can be because you can run the cells separately and out of order in markdown, but when you `knit` it, it will happen in order... maybe you made some modification in the code, one cell was corrupted/excluded, you added the new code before the older one, etc. – hamagust Jun 07 '22 at 00:49
  • Without [reproducible data](https://stackoverflow.com/q/5963269/1422451) of any of these objects, we really cannot help. – Parfait Jun 07 '22 at 00:50
  • Thanks so much guys, I fixed the error. I think it had to do with the way that I imported my data l, similar to what @hamagust suggested. But also I will do my best to create examples with reproducible data in future – Kgosi Jun 07 '22 at 01:45

1 Answers1

0

Check the length of your variables using length(Returns$Date[-c(1:Lookback)][1:(Periods*Holding)]), length(Returns$Market.Close[-c(1:Lookback)][1:(Periods*Holding)]), and so on.

If they have different lengths, that is why the function is returning an error. You need to keep all on the same length to be able to create your Porfolio_dataframe dataframe.

You may also want to take a look at the function lag() to obtain lagged data.

Fla28
  • 197
  • 11
  • Firstly, thanks for replying. I just did length tests and everything looks fine, the lengths were all the same. But when I did the same using the lag function, my first observation for all my variables were NAs. Do you think that this is the issue? – Kgosi Jun 07 '22 at 00:14
  • No, this is not the issue. – Fla28 Jun 07 '22 at 00:23