1

suppose you invest 1$ in the end of December 1933 in each asset and hold it until end of December 2019. Make a plot which shows how your initial investment of 1$ evolves over time.

  #plotting log of treasury bills
TBS2_dates <- TBS2[,1]
log_ret_treasury <- cbind(TBS2_dates, ret_treasury[,4])
plot(log_ret_treasury,  type="l",
     xlab = "monthly obs", ylab = "Rate of return in decimals, ex. (0.01 = 1%", 
     main = "monthly rate of return on the US Treasury Bills 3months 1934-2019")

#plotting log index
IND_dates <- IND[,3]
log_ret_index <- cbind(IND_dates, ret_index[,1])
plot(log_ret_index, type="l",
     xlab = "monthly obs", ylab = "Rate of return in decimals, ex. (0.01 = 1%",
     main = "monthly rate of return on US stock index 1934-2020")

how do i make a plot of my 1$ investment in both of these plots? Thanks for any help ^^

> dput(head(log_ret_index))
structure(list(date = structure(c(-13119, -13091, -13060, -13030, 
-12999, -12969), class = "Date"), `ret_index[, 1]` = c(0.119363119998702, 
-0.0245867931894417, 0.00441523847340962, -0.0185153561852664, 
-0.0750177705101481, 0.0250281660421121)), row.names = c(NA, 
6L), class = "data.frame")

enter image description here

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • It seems that your question is not about plotting, it's about homework that is asking you to calculate compound interest and then demonstrate that in a plot. You need to know how to do compound interest. What have you tried? Is there sample data we can use? – r2evans Sep 29 '20 at 16:31
  • I have already plotted the two df´s in two plots. Im new to R, so i am unsure how to give you the sample data. But how would you proceed if you were supposed to plot your investment over time compared to the plots? – Thommas Espeland Sep 29 '20 at 16:35
  • Welcome to SO, Thommas! You've provided sample code which is good, but we have nothing to use that code on. Please make this question *reproducible*. This includes sample *unambiguous* data (e.g., `dput(head(x))` or `data.frame(x=...,y=...)`). Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Sep 29 '20 at 16:38
  • Thank you :) i posted the us stock index head for you to see :) – Thommas Espeland Sep 29 '20 at 16:47
  • Thanks for posting the `dput` output. So you're interested in how to plot a line, and your `plot` command actually does that, so ... I think we're back to this: you need to know how to do compound interest, initially this question has nothing to do with "how to plot". It should instead be "how to calculate", and then plotting should work itself out. – r2evans Sep 29 '20 at 16:52
  • Further: plotting the schedule of rates is interesting, but you need to derive your own `data.frame` based on this schedule of return rates. If you know how to calculate compounding interest but don't know how to do it in R, then please explain what you understand. – r2evans Sep 29 '20 at 16:55
  • Related: https://stackoverflow.com/q/24595890/3358272 – r2evans Sep 29 '20 at 16:55
  • Yes. as i said earlier, im a newbie on this subject.. But if that is what i need to do to get my wish true, how can i do this in each of my df's? i suppose the method is identical for both? – Thommas Espeland Sep 29 '20 at 16:56
  • You said you are new to R, but it sounds like you don't know how to do compound interest in general, is that right? For instance, if you were to do this somewhere else (e.g., Excel), do you know how you would do it? – r2evans Sep 29 '20 at 16:59
  • i find excel way easier than R, the thing is that we are obligated to do all calculations in R. i saw your link with the example. I see the OP had frequent investments every month. In my case it is just one investment at the start of 1933 to 2019. I imagine i can apply a formula containing the dataframes i made my plot from with the 1$ investment. Or is my logic wrong? thanks ^^ – Thommas Espeland Sep 29 '20 at 17:09

1 Answers1

0

If the data is contiguous month-wise, then cumulative interest can be calculated with cumprod, or cumulative product. Since it's interest, we need to add 1 first.

Edit: this is monthly compounding on an annual return rate, I think, so I'll divide the rate by 12 ...

cumprod(1 + log_ret_index[,2]/12)
# [1] 1.009947 1.007878 1.008248 1.006693 1.000399 1.002486

Since you want to go from 1933 to 2019, for the sake of execution I'll carry the last rate (0.025) forward ten years:

out <- cumprod(1 + c(log_ret_index[,2], rep(log_ret_index[6,2], 120)) / 12)
head(out)
# [1] 1.009947 1.007878 1.008248 1.006693 1.000399 1.002486
tail(out)
# [1] 1.273904 1.276561 1.279224 1.281892 1.284565 1.287245

Let's flash forward some decades:

tail(cumprod(1 + c(log_ret_index[,2], rep(log_ret_index[6,2], 10*12)) / 12))
# [1] 1.273904 1.276561 1.279224 1.281892 1.284565 1.287245
tail(cumprod(1 + c(log_ret_index[,2], rep(log_ret_index[6,2], 20*12)) / 12))
# [1] 1.635760 1.639171 1.642590 1.646016 1.649449 1.652889
tail(cumprod(1 + c(log_ret_index[,2], rep(log_ret_index[6,2], 30*12)) / 12))
# [1] 2.100401 2.104782 2.109172 2.113571 2.117979 2.122397
tail(cumprod(1 + c(log_ret_index[,2], rep(log_ret_index[6,2], 40*12)) / 12))
# [1] 2.697025 2.702651 2.708287 2.713936 2.719596 2.725269
tail(cumprod(1 + c(log_ret_index[,2], rep(log_ret_index[6,2], 50*12)) / 12))
# [1] 3.463122 3.470345 3.477583 3.484836 3.492104 3.499388
tail(cumprod(1 + c(log_ret_index[,2], rep(log_ret_index[6,2], 60*12)) / 12))
# [1] 4.446830 4.456105 4.465399 4.474712 4.484045 4.493397
tail(cumprod(1 + c(log_ret_index[,2], rep(log_ret_index[6,2], 70*12)) / 12))
# [1] 5.709963 5.721872 5.733806 5.745765 5.757749 5.769757
tail(cumprod(1 + c(log_ret_index[,2], rep(log_ret_index[6,2], 80*12)) / 12))
# [1] 7.331891 7.347183 7.362507 7.377863 7.393251 7.408671

(Yes, there are much better ways to calculate and show that ...)

To me, this suggests that if you had invested a single $1 contribute in 1933; and the rate stayed at its last 2.5% annual return; and you let it sit for 80 years; *then you might have multiplied your dollar by over 7x over those eight decades.

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • when i did your calculation on the big scale i got 41964.87 but the head is 0.119363 and tail is 0.0280508. how can it be this big when the values are that small? – Thommas Espeland Sep 29 '20 at 18:23
  • See my edit ... the `/12` is most likely a fix here, otherwise `$1 --> $20B` ... unlikely :-) – r2evans Sep 29 '20 at 18:40
  • thanks. That helps ^^But if you see my screenshot i added on the post. I don't understand how it can grow 7x in those years according to the plot. – Thommas Espeland Sep 29 '20 at 18:55
  • Your screenshot shows you still using `cumprod(1 + log_ret_index[,2])` instead of `cumprod(1 + log_ret_index[,2]/12)`, not sure if that's in error. But ... verify with https://www.investor.gov/financial-tools-calculators/calculators/compound-interest-calculator, values: initial `1`, monthly contrib `0`, length in years `80`, est interest `2`, int variance `0`, compound `monthly`, and hit calculate, and it returns `$4.95`. In R, run `tail(cumprod(rep(1 + 0.02/12, 80*12)))`, and the last value is 4.946440. I'm not saying I'm a business expert, but the math seems to line up. – r2evans Sep 29 '20 at 19:19
  • 1
    alright. ill try that out. and hopefully everything works out! thanks R2evans :)) – Thommas Espeland Sep 29 '20 at 19:22
  • i checked ther gov website. i should have 5.58$ after 86 years. in R, the last number in cumprod(1 + log_ret_index[,2]/12) gives me the same number in thousands. Something is wrong. The thing is that im also supposed to plot how the investment involves over time... Maybe there is an easier way? – Thommas Espeland Sep 29 '20 at 19:28
  • never mind. brainfart on my part! now how do i plot this – Thommas Espeland Sep 29 '20 at 19:34
  • `plot(log_ret_index[,1], out)`? It's not appropriate to put it on the same y-axis since it's a different "unit" than the "rate". – r2evans Sep 29 '20 at 19:38
  • thankyou so much sir:) quick question: how do you know the rate is 2% im just curious since according to the plot of rate of return on the us. stock index shows a lower number on the tail compared to the head. Should not this in theory give me a negative end value? – Thommas Espeland Sep 29 '20 at 19:49
  • 2% was merely an example, since I don't have all of your data, and I thought it would be an easy verification of the concept. – r2evans Sep 29 '20 at 21:45