How to Compute Compound Interest with Tidyverse

In this post, we will learn how to calculate compound interest in R using tidyverse framework. Getting an intuition behind how compound interest can influence investing returns is wxtremely useful in personal final. On a high level, compounding interest refers to the process of earning interest not only on the the original investment amount (i.e. principal amount of money), but also on the interest that accumulates over time. This results in a “compounding” effect, where the investment or debt grows at an increasing rate.

Here is the formula for computing compound interest and it shows the variables that influence the compounding.

Compound Interest Formula
Compound Interest Formula

Let us get started computing compound interest on principal amount of $1000 over 10 years period with interest rate of 5%.

library(tidyverse)
theme_set(theme_bw(16))
# Starting amount
principal <- 1000    
# Time period in years
years <- 10          
# Interest rate (5% in this case)
rate <- 0.05          
tibble(year = 0:years)

# A tibble: 11 × 1
    year
   <int>
 1     0
 2     1
 3     2
 4     3
 5     4
 6     5
 7     6
 8     7
 9     8
10     9
11    10

By applying the compound interest formula we can compute compound interest for each year.

tibble(year = 0:years) |>
  mutate(amount = principal * (1 + rate) ^ year)

# A tibble: 11 × 2
    year amount
   <int>  <dbl>
 1     0  1000 
 2     1  1050 
 3     2  1102.
 4     3  1158.
 5     4  1216.
 6     5  1276.
 7     6  1340.
 8     7  1407.
 9     8  1477.
10     9  1551.
11    10  1629.

We can also compute the gain or interest earned due to compound at each year

tibble(year = 1:years) |>
  mutate(amount = principal * (1 + rate) ^ year,
         interest_earned = amount - principal )

# A tibble: 10 × 3
    year amount interest_earned
   <int>  <dbl>           <dbl>
 1     1  1050              50 
 2     2  1102.            102.
 3     3  1158.            158.
 4     4  1216.            216.
 5     5  1276.            276.
 6     6  1340.            340.
 7     7  1407.            407.
 8     8  1477.            477.
 9     9  1551.            551.
10    10  1629.            629.

Let us visualize the growth of investment return due to compound interest over 10 years.

# Create a dataframe to compute compound interest for each year
compound_interest <- tibble(year = 0:years) |>
  # Calculate accumulated amount each year
  # Calculate interest earned each year
  mutate( amount = principal * (1 + rate) ^ year,   
          interest_earned = amount - principal
          )
# Display the results
print(compound_interest)

# A tibble: 11 × 3
    year amount interest_earned
   <int>  <dbl>           <dbl>
 1     0  1000               0 
 2     1  1050              50 
 3     2  1102.            102.
 4     3  1158.            158.
 5     4  1216.            216.
 6     5  1276.            276.
 7     6  1340.            340.
 8     7  1407.            407.
 9     8  1477.            477.
10     9  1551.            551.
11    10  1629.            629.

# Visualize the compound interest gained on 1000 over the years
compound_interest |>
  ggplot(aes(x = year, y = amount)) +
  geom_line(color = "blue", linewidth = 1) +
  geom_point(color = "red", size = 2) +
  labs(
    title = paste("Compound Interest Over", years),
    x = "Year",
    y = "Final Amount",
    subtitle = paste("Interest Rate:", rate * 100, "%")
  ) +
  scale_x_continuous(breaks=scales::breaks_pretty(n=10))+
  scale_y_continuous(breaks=scales::breaks_pretty(n=8))
ggsave("growth_of_1000_compound_interest_example.png")
Computing Compound Interest Example

Leave a comment

Your email address will not be published. Required fields are marked *

Exit mobile version