Alex plans to save $500 in a savings account that pays 4% yearly interest, compounded monthly. This means that every month, the account earns 0.04/12 on top of the current value of the account. Ie, after the first month, they’ll have:
\[500 + 0*(0.04/12) = 500.00\]
After two months, Alex have the previous month’s balance + 500 + interest:
\[500.00 + 500 + 501.67*(0.04/12) = 1001.67\]
After each month, the balance (\(B\)) in Alex’s savings account will be:
\[B_{i+1} = B_i + 500 + B_i*(0.04/12)\]
where \(B_i\) is the balance of the previous month, \(B_{i+1}\) is the balance of the current month.
Using the appropriate loop, write the code to determine how much money Alex will have in their account after 30 years (60 months). If done correctly, you should get $33,149.49 (rounded to the nearest dollar).
Hint: Balance should appear on both sides of <-
inside the loop
## [1] 33149.49
Using the code from the previous question, update it to save the balance in a data.frame with 2 columns:
The month: 1, 2, 3, …, 60
The current balance of the account for that month
Start by making a data frame (I called it balance_df
,
but you can give it a better name if you think of one) with 60 rows and
two columns: one for the month, and the second for the balance at the
end of that month.
Adapt the code from the previous question to write a function named
savings_calculator()
that has four arguments:
starting_balance
= the initial starting balance
(Alex started with $0)
interest_rate
= the yearly interest rate applied to
the current balance (Alex’s interest was 0.04 or 4%)
savings
= the amount deposited to the account each
month (Alex saved $500 each month).
months
= how many months the person will be saving.
For Alex, it was 60 months.
It should return a list
of three objects:
The final balance named balance
A data frame with the balance at the end of each month named
monthly_balance
A line graph of the change in balance named
balance_graph
Hint: To account for the starting balance potentially not being 0,
the first line in your function should be
balance <- starting_balance
Use the code chunks below to see if your function works
# Initial set up: starting balance = 0, interest rate = 4%,
# monthly savings = 500, months = 60
savings_calculator(
starting_balance = 0,
interest_rate = 0.04,
months = 60,
savings = 500
)
## $balance
## [1] 33149.49
##
## $monthly_balance
## month balance
## 1 1 500.000
## 2 2 1001.667
## 3 3 1505.006
## 4 4 2010.022
## 5 5 2516.722
## 6 6 3025.111
## 7 7 3535.195
## 8 8 4046.979
## 9 9 4560.469
## 10 10 5075.671
## 11 11 5592.589
## 12 12 6111.231
## 13 13 6631.602
## 14 14 7153.708
## 15 15 7677.553
## 16 16 8203.145
## 17 17 8730.489
## 18 18 9259.591
## 19 19 9790.456
## 20 20 10323.091
## 21 21 10857.501
## 22 22 11393.693
## 23 23 11931.672
## 24 24 12471.444
## 25 25 13013.015
## 26 26 13556.392
## 27 27 14101.580
## 28 28 14648.585
## 29 29 15197.414
## 30 30 15748.072
## 31 31 16300.566
## 32 32 16854.901
## 33 33 17411.084
## 34 34 17969.121
## 35 35 18529.018
## 36 36 19090.781
## 37 37 19654.417
## 38 38 20219.932
## 39 39 20787.332
## 40 40 21356.623
## 41 41 21927.811
## 42 42 22500.904
## 43 43 23075.907
## 44 44 23652.827
## 45 45 24231.670
## 46 46 24812.442
## 47 47 25395.150
## 48 48 25979.800
## 49 49 26566.400
## 50 50 27154.954
## 51 51 27745.471
## 52 52 28337.956
## 53 53 28932.416
## 54 54 29528.857
## 55 55 30127.287
## 56 56 30727.711
## 57 57 31330.137
## 58 58 31934.570
## 59 59 32541.019
## 60 60 33149.489
##
## $balance_graph
# Same as the initial set up, but 8% interest (typical S&P 500)
# and just the line graph
savings_calculator(
starting_balance = 10000,
interest_rate = 0.08,
months = 60,
savings = 500
)$balance_graph
Comparing saving 100, 250, 500, and 1000 dollars over 40 years at a 7% interest rate
# Creating a data frame with the monthly balance of the 4 different savings
savings_comparison <-
bind_rows(
.id = "savings_amount",
# Saving 100 over 40 years
"100" = savings_calculator(
starting_balance = 0,
interest_rate = 0.07,
months = 480,
savings = 100
)$monthly_balance,
# Saving 250 over 40 years
"250" = savings_calculator(
starting_balance = 0,
interest_rate = 0.07,
months = 480,
savings = 250
)$monthly_balance,
# Saving 500 over 40 years
"500" = savings_calculator(
starting_balance = 0,
interest_rate = 0.07,
months = 480,
savings = 500
)$monthly_balance,
# Saving 1000 over 40 years
"1000" = savings_calculator(
starting_balance = 0,
interest_rate = 0.07,
months = 480,
savings = 1000
)$monthly_balance,
)
# Creating a line graph of the results
ggplot(
data = savings_comparison,
mapping = aes(
x = month,
y = balance,
color = savings_amount
)
) +
# Adding the lines
geom_line() +
# Adding the amounts to the graph
geom_text(
# Getting the final balance for each savings amount
data = savings_comparison |> slice_max(by = savings_amount, balance),
mapping = aes(label = paste0("$", round(balance))),
nudge_x = 30
) +
# Changing the theme and removing the legend
theme_minimal() +
theme(
legend.position = "none",
plot.title = element_text(hjust = 0.5),
plot.subtitle = ggtext::element_markdown(hjust = 0.5)
) +
# Changing the x-axis
scale_x_continuous(
limits = c(0, 520),
breaks = seq(0, 480, by = 60),
minor_breaks = NULL,
labels = seq(0, 40, by = 5),
expand = c(0, 0, 0.05, 0)
) +
scale_y_continuous(
labels = scales::label_dollar(),
breaks = seq(0, 2500000, by = 500000),
minor_breaks = NULL
) +
scale_color_manual(
values = c("100" = "red3", "250" = "orange2",
"500" = "steelblue", "1000" = "navyblue")
) +
# Changing the labels
labs(
title = "How much money would you have saved over 40 years if you saved:",
subtitle = "<span style='color:red3;'>$100</span>, <span style='color:orange2;'>$250</span>, <span style='color:steelblue;'>$500</span>, <span style='color:navy;'>$1000</span> monthly",
x = "Year",
y = NULL,
caption = "Assuming 7% annual returns"
)