Hi!
Suppose I lend people money and they repay in smaller amounts over month. I expect all clear at due date. I would like to know how much of the money I get after X% of the time and how certain I can be.
library(brms)
library(tidyverse)
library(lubridate)
library(tsibble)
library(ggplot2)
df_raw <- tribble(
~Person, ~Date, ~Amount, ~Comment,
"A", "2000-01-01", 1000, "Borrow",
"A", "2000-02-01", -200, "",
"A", "2000-03-01", -10, "",
"A", "2000-05-01", -300, "",
"A", "2000-06-01", -100, "Due date",
"B", "2000-03-01", 2000, "Borrow",
"B", "2000-04-01", -420, "",
"B", "2000-05-01", -20, "",
"B", "2000-06-01", -500, "Due date",
"B", "2000-10-01", -620, "",
"C", "2000-03-01", 3000, "Borrow",
"C", "2000-04-01", -500, "",
"C", "2000-05-01", -30, "",
"C", "2000-06-01", -900, "",
"C", "2000-07-01", -300, "Due date",
"C", "2001-10-01", -300, ""
)
df <- df_raw |>
mutate(Date = yearmonth(Date)) |>
group_by(Person) |>
mutate(
loan_start = first(Date[Comment == "Borrow"]),
borrowed_amount = first(Amount[Comment == "Borrow"]),
amount_open = cumsum(Amount),
relative_amount = amount_open / borrowed_amount,
due_date = first(Date[Comment == "Due date"]),
loan_duration = as.numeric(due_date - loan_start),
time_point = as.numeric(Date - loan_start) / loan_duration
) |>
ungroup() |>
select(-Comment)
ggplot(df, aes(x=time_point, y=relative_amount, color = Person, group = Person)) +
geom_line() +
geom_point()
The relation must not be linear. The relative amount should be 1 at time 0 because I divided by a reference amount. I would like to learn from all persons.
df1 <- df |>
filter(between(time_point,0,1))
fit1 <- brm(
formula = bf(relative_amount ~ time_point + (1 | p | Person)),
data = df1,
family = gaussian(),
seed = 12345
)
summary(fit1)
Is this the right approach? How to specify the priors? Thanks,