Skip to contents

This function takes a data frame of loans and generates paired loan start and end dates within households. It performs the following steps:

  1. Orders the loans within each household by loan ID.

  2. Calculates the number of loans within each household and adds it as a variable.

  3. Expands each household-loan combination to multiple rows based on the number of loans.

  4. Merges in the start and end dates of each loan as paired start and end dates.

  5. Filters out disconnected loan pairs based on start and end dates.

Usage

ffp_hfid_hook_pairs(
  df_loans_pn_nd,
  svr_lender_var = "forinfm4",
  verbose = FALSE,
  verbose_detail = FALSE,
  it_verbose_detail_nrow = 100
)

Arguments

df_loans_pn_nd

A data frame containing loan information, including household ID, loan ID, loan start date, and loan end date.

svr_lender_var

The name of the variable representing the lender in the data frame.

verbose

A logical value indicating whether to print verbose output.

verbose_detail

A logical value indicating whether to print detailed verbose output.

it_verbose_detail_nrow

The number of rows to print when printing detailed verbose output.

Value

A list containing the data frame with paired loan start and end dates within households.

Author

Fan Wang, http://fanwangecon.github.io

Examples

df_loans_pn_nd <- PrjThaiHFID::tstm_loans_pn_nd
ls_return <- ffp_hfid_hook_pairs(df_loans_pn_nd)
print(ls_return)
#> $tstm_loans_startend_pair
#> # A tibble: 147,460 × 9
#>    hhid_Num hh_loan_id_nd hh_loan_id_nd_paired loan_start loan_start_paired
#>       <dbl>         <int>                <int>      <dbl>             <dbl>
#>  1    70203             3                    4         39                51
#>  2    70203             4                    3         51                39
#>  3    70203             4                    5         51                64
#>  4    70203             4                    6         51                64
#>  5    70203             4                    7         51                65
#>  6    70203             5                    4         64                51
#>  7    70203             5                    6         64                64
#>  8    70203             5                    7         64                65
#>  9    70203             6                    4         64                51
#> 10    70203             6                    5         64                64
#> # ℹ 147,450 more rows
#> # ℹ 4 more variables: loan_end <dbl>, loan_end_paired <dbl>, forinfm4 <chr>,
#> #   forinfm4_paired <chr>
#> 
#> $tstm_loans_hooks
#> # A tibble: 47,754 × 12
#>    hhid_Num hh_loan_id_nd hh_loan_id_nd_paired loan_start loan_start_paired
#>       <dbl>         <int>                <int>      <dbl>             <dbl>
#>  1    70203             3                    4         39                51
#>  2    70203             4                    5         51                64
#>  3    70203             4                    6         51                64
#>  4    70203             4                    7         51                65
#>  5    70203             5                    6         64                64
#>  6    70203             5                    7         64                65
#>  7    70203             6                    7         64                65
#>  8    70203             7                    8         65                74
#>  9    70203             7                    9         65                74
#> 10    70203             8                    9         74                74
#> # ℹ 47,744 more rows
#> # ℹ 7 more variables: loan_end <dbl>, loan_end_paired <dbl>, forinfm4 <chr>,
#> #   forinfm4_paired <chr>, hook_gap_gl <dbl>, hook_gap_gm <dbl>,
#> #   hook_gap_gr <dbl>
#>