library(PrjThaiHFID)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(tidyr)
library(readr)
library(ggplot2)
library(kableExtra)
#>
#> Attaching package: 'kableExtra'
#> The following object is masked from 'package:dplyr':
#>
#> group_rows
bl_save_figs <- FALSE
cbp1 <- c(
"#999999", "#E69F00", "#56B4E9", "#009E73",
"#F0E442", "#0072B2", "#D55E00", "#CC79A7"
)
Categories and tabulations
# code to prepare `tstm_loans` dataset goes here
# 1000. Generate Loan IDs and MISC ----
# tstm_loans <- PrjThaiHFID::tstm_loans
# Unique ID for each loan
tstm_loans <- tstm_loans %>%
select(
S_region, provid_Num, vilid_Num, hhid_Num, surveymonth,
formal, institutional, G_LenderType,
everything()
) %>%
drop_na(G_Loan_Repaid_Length) %>%
arrange(hhid_Num, surveymonth, G_Loan_Repaid_Length) %>%
mutate(loan_id = row_number())
# Tally loan types and drop NA
tstm_loans %>%
group_by(G_LenderType) %>%
tally()
#> # A tibble: 10 × 2
#> G_LenderType n
#> <chr> <int>
#> 1 Agri Coop 577
#> 2 BAAC 3396
#> 3 Commercial Bank 64
#> 4 MoneyLender 625
#> 5 Neighbor 998
#> 6 Other Non-Indi Formal or Informal 4659
#> 7 Others 1349
#> 8 PCG 501
#> 9 Relatives 1363
#> 10 Village Fund 7408
tstm_loans %>%
group_by(G_LenderType, G_Location) %>%
tally() %>%
spread(G_Location, n)
#> # A tibble: 10 × 5
#> # Groups: G_LenderType [10]
#> G_LenderType `Changwat and Out` `Tambon or Amphoe` Village `<NA>`
#> <chr> <int> <int> <int> <int>
#> 1 Agri Coop 29 547 1 NA
#> 2 BAAC 757 2636 2 1
#> 3 Commercial Bank 17 46 1 NA
#> 4 MoneyLender 162 201 260 2
#> 5 Neighbor 3 20 975 NA
#> 6 Other Non-Indi Formal o… 773 1146 2732 8
#> 7 Others 555 506 272 16
#> 8 PCG 3 9 489 NA
#> 9 Relatives 220 433 705 5
#> 10 Village Fund 9 251 7148 NA
Review formal, informal, and joint loan categories.
tstm_loans %>%
group_by(G_LenderType, formal) %>%
tally() %>%
spread(formal, n)
#> # A tibble: 10 × 3
#> # Groups: G_LenderType [10]
#> G_LenderType `FALSE` `TRUE`
#> <chr> <int> <int>
#> 1 Agri Coop 577 NA
#> 2 BAAC NA 3396
#> 3 Commercial Bank NA 64
#> 4 MoneyLender 625 NA
#> 5 Neighbor 998 NA
#> 6 Other Non-Indi Formal or Informal 2732 1927
#> 7 Others 1349 NA
#> 8 PCG 501 NA
#> 9 Relatives 1363 NA
#> 10 Village Fund NA 7408
tstm_loans %>%
group_by(institutional, G_LenderType, formal) %>%
tally() %>%
spread(formal, n)
#> # A tibble: 10 × 4
#> # Groups: institutional, G_LenderType [10]
#> institutional G_LenderType `FALSE` `TRUE`
#> <lgl> <chr> <int> <int>
#> 1 FALSE MoneyLender 625 NA
#> 2 FALSE Neighbor 998 NA
#> 3 FALSE Others 1349 NA
#> 4 FALSE Relatives 1363 NA
#> 5 TRUE Agri Coop 577 NA
#> 6 TRUE BAAC NA 3396
#> 7 TRUE Commercial Bank NA 64
#> 8 TRUE Other Non-Indi Formal or Informal 2732 1927
#> 9 TRUE PCG 501 NA
#> 10 TRUE Village Fund NA 7408
tstm_loans %>%
group_by(institutional, G_LenderType, formalqf) %>%
tally() %>%
spread(formalqf, n)
#> # A tibble: 10 × 4
#> # Groups: institutional, G_LenderType [10]
#> institutional G_LenderType `FALSE` `TRUE`
#> <lgl> <chr> <int> <int>
#> 1 FALSE MoneyLender 625 NA
#> 2 FALSE Neighbor 998 NA
#> 3 FALSE Others 1349 NA
#> 4 FALSE Relatives 1363 NA
#> 5 TRUE Agri Coop NA 577
#> 6 TRUE BAAC NA 3396
#> 7 TRUE Commercial Bank NA 64
#> 8 TRUE Other Non-Indi Formal or Informal 2732 1927
#> 9 TRUE PCG NA 501
#> 10 TRUE Village Fund NA 7408
tstm_loans %>%
group_by(institutional, G_LenderType, forinfm3) %>%
tally() %>%
spread(forinfm3, n)
#> # A tibble: 10 × 5
#> # Groups: institutional, G_LenderType [10]
#> institutional G_LenderType Formal Informal `Quasi-formal`
#> <lgl> <chr> <int> <int> <int>
#> 1 FALSE MoneyLender NA 625 NA
#> 2 FALSE Neighbor NA 998 NA
#> 3 FALSE Others NA 1349 NA
#> 4 FALSE Relatives NA 1363 NA
#> 5 TRUE Agri Coop NA NA 577
#> 6 TRUE BAAC 3396 NA NA
#> 7 TRUE Commercial Bank 64 NA NA
#> 8 TRUE Other Non-Indi Formal or Inform… NA NA 4659
#> 9 TRUE PCG NA NA 501
#> 10 TRUE Village Fund 7408 NA NA
Stats to count conditional overlaps
Formal and Informal Base Definition Main
# View(tstm_hh_skeleton)
# 1200. loan span skeleton ----
# 1. long frame, observation is household loan month,
# for each month of each loan for each household
# select loans of one type and generate household loan id
for (bl_formal in c(TRUE, FALSE)) {
tstm_loans_type <- tstm_loans %>%
filter(formal == bl_formal) %>%
arrange(hhid_Num, loan_id) %>%
group_by(hhid_Num) %>%
mutate(hh_loan_id = row_number()) %>%
ungroup()
tstm_loans_mths <- tstm_loans_type %>%
select(loan_id, hhid_Num, hh_loan_id, surveymonth, G_Loan_Repaid_Length) %>%
rename(
mth_start = surveymonth,
mth_duration = G_Loan_Repaid_Length
) %>%
uncount(mth_duration) %>%
group_by(loan_id) %>%
mutate(surveymonth = row_number() + mth_start - 1) %>%
ungroup()
# View(tstm_loans_mths)
# 1300. Loan long to wide
# 3. reshape wide, each col is a different loan id, row is a household month, for each household, the number of months/rows is the coverage of loans over the span of the household
tstm_loans_mth_wide <- tstm_loans_mths %>%
select(hhid_Num, hh_loan_id, surveymonth) %>%
arrange(hhid_Num, hh_loan_id, surveymonth) %>%
mutate(has_loan = 1) %>%
pivot_wider(
id_cols = c("hhid_Num", "surveymonth"),
names_from = "hh_loan_id",
names_prefix = "loan_",
values_from = has_loan
)
# View(tstm_loans_mth_wide)
# 4. drop all columns, but keep months and household, these are the months in which this household has these loans
tstm_loans_mth_cover_type <- tstm_loans_mth_wide %>%
select(hhid_Num, surveymonth)
if (bl_formal) {
tstm_loans_mth_cover_formal <- tstm_loans_mth_cover_type %>%
mutate(formal = TRUE)
} else {
tstm_loans_mth_cover_informal <- tstm_loans_mth_cover_type %>%
mutate(informal = TRUE)
}
}
# Merge formal and informal
tstm_loans_mth_cover_all <- tstm_hh_skeleton %>%
left_join(tstm_loans_mth_cover_formal,
by = (c("hhid_Num" = "hhid_Num", "surveymonth" = "surveymonth"))
) %>%
left_join(tstm_loans_mth_cover_informal,
by = (c("hhid_Num" = "hhid_Num", "surveymonth" = "surveymonth"))
) %>%
arrange(hhid_Num, surveymonth)
# Print
# print(tstm_loans_mth_cover_all[1:200,])
Consideration based on formal and informal, where quasi-formal = informal.
# Total number of months with any loans
tstm_loans_mth_all_stats <- tstm_loans_mth_cover_all %>%
group_by(hhid_Num) %>%
mutate(mth_span_hh = n())
# Total number of months with any loans
tstm_loans_mth_all_stats <- tstm_loans_mth_all_stats %>%
mutate(
has_any =
case_when(
formal == TRUE ~ 1,
informal == TRUE ~ 1,
TRUE ~ 0
)
) %>%
group_by(hhid_Num) %>%
mutate(mth_has_any = sum(has_any))
# Total number of months with only formal loans
tstm_loans_mth_all_stats <- tstm_loans_mth_all_stats %>%
ungroup() %>%
mutate(
has_any =
case_when(
formal == TRUE ~ 1,
TRUE ~ 0
)
) %>%
group_by(hhid_Num) %>%
mutate(mth_has_any_formal = sum(has_any))
# Total number of months with only informal loans
tstm_loans_mth_all_stats <- tstm_loans_mth_all_stats %>%
ungroup() %>%
mutate(
has_any =
case_when(
informal == TRUE ~ 1,
TRUE ~ 0
)
) %>%
group_by(hhid_Num) %>%
mutate(mth_has_any_informal = sum(has_any))
# Total numbers of joint formal and informal
tstm_loans_mth_all_stats <- tstm_loans_mth_all_stats %>%
ungroup() %>%
mutate(
has_any =
case_when(
informal == TRUE & formal == TRUE ~ 1,
TRUE ~ 0
)
) %>%
group_by(hhid_Num) %>%
mutate(mth_has_any_joint = sum(has_any))
# Slice
tstm_loans_mth_all_stats_l1 <- tstm_loans_mth_all_stats %>%
group_by(hhid_Num) %>%
slice_head(n = 1) %>%
select(hhid_Num, starts_with("mth"))
print(tstm_loans_mth_all_stats_l1[1:100, ])
#> # A tibble: 100 × 6
#> # Groups: hhid_Num [100]
#> hhid_Num mth_span_hh mth_has_any mth_has_any_formal mth_has_any_informal
#> <dbl> <int> <dbl> <dbl> <dbl>
#> 1 70201 18 0 0 0
#> 2 70202 135 0 0 0
#> 3 70203 133 101 97 24
#> 4 70204 135 135 117 71
#> 5 70205 133 85 63 85
#> 6 70206 135 131 16 131
#> 7 70207 135 103 99 20
#> 8 70208 135 41 2 39
#> 9 70209 135 25 25 0
#> 10 70210 135 102 79 35
#> # ℹ 90 more rows
#> # ℹ 1 more variable: mth_has_any_joint <dbl>
# write_csv(tstm_loans_mth_all_stats_l1, "../data-raw/tstm_loans_mth_all_stats_l1.csv")
Formal and Informal Alternative Definition
# View(tstm_hh_skeleton)
# 1200. loan span skeleton ----
# 1. long frame, observation is household loan month,
# for each month of each loan for each household
# select loans of one type and generate household loan id
for (bl_formalqf in c(TRUE, FALSE)) {
tstm_loans_type <- tstm_loans %>%
filter(formalqf == bl_formalqf) %>%
arrange(hhid_Num, loan_id) %>%
group_by(hhid_Num) %>%
mutate(hh_loan_id = row_number()) %>%
ungroup()
tstm_loans_mths <- tstm_loans_type %>%
select(loan_id, hhid_Num, hh_loan_id, surveymonth, G_Loan_Repaid_Length) %>%
rename(
mth_start = surveymonth,
mth_duration = G_Loan_Repaid_Length
) %>%
uncount(mth_duration) %>%
group_by(loan_id) %>%
mutate(surveymonth = row_number() + mth_start - 1) %>%
ungroup()
# View(tstm_loans_mths)
# 1300. Loan long to wide
# 3. reshape wide, each col is a different loan id, row is a household month, for each household, the number of months/rows is the coverage of loans over the span of the household
tstm_loans_mth_wide <- tstm_loans_mths %>%
select(hhid_Num, hh_loan_id, surveymonth) %>%
arrange(hhid_Num, hh_loan_id, surveymonth) %>%
mutate(has_loan = 1) %>%
pivot_wider(
id_cols = c("hhid_Num", "surveymonth"),
names_from = "hh_loan_id",
names_prefix = "loan_",
values_from = has_loan
)
# View(tstm_loans_mth_wide)
# 4. drop all columns, but keep months and household, these are the months in which this household has these loans
tstm_loans_mth_cover_type <- tstm_loans_mth_wide %>%
select(hhid_Num, surveymonth)
if (bl_formalqf) {
tstm_loans_mth_cover_formalqf <- tstm_loans_mth_cover_type %>%
mutate(formalqf = TRUE)
} else {
tstm_loans_mth_cover_informalqf <- tstm_loans_mth_cover_type %>%
mutate(informalqf = TRUE)
}
}
# Merge formalqf and informalqf
tstm_loans_mth_cover_all <- tstm_hh_skeleton %>%
left_join(tstm_loans_mth_cover_formalqf,
by = (c("hhid_Num" = "hhid_Num", "surveymonth" = "surveymonth"))
) %>%
left_join(tstm_loans_mth_cover_informalqf,
by = (c("hhid_Num" = "hhid_Num", "surveymonth" = "surveymonth"))
) %>%
arrange(hhid_Num, surveymonth)
# Print
# print(tstm_loans_mth_cover_all[1:200,])
Consideration based on formalqf and informalqf, where quasi-formalqf = informalqf.
# Total number of months with any loans
tstm_loans_mth_all_stats <- tstm_loans_mth_cover_all %>%
group_by(hhid_Num) %>%
mutate(mth_span_hh = n())
# Total number of months with any loans
tstm_loans_mth_all_stats <- tstm_loans_mth_all_stats %>%
mutate(
has_any =
case_when(
formalqf == TRUE ~ 1,
informalqf == TRUE ~ 1,
TRUE ~ 0
)
) %>%
group_by(hhid_Num) %>%
mutate(mth_has_any = sum(has_any))
# Total number of months with only formalqf loans
tstm_loans_mth_all_stats <- tstm_loans_mth_all_stats %>%
ungroup() %>%
mutate(
has_any =
case_when(
formalqf == TRUE ~ 1,
TRUE ~ 0
)
) %>%
group_by(hhid_Num) %>%
mutate(mth_has_any_formalqf = sum(has_any))
# Total number of months with only informalqf loans
tstm_loans_mth_all_stats <- tstm_loans_mth_all_stats %>%
ungroup() %>%
mutate(
has_any =
case_when(
informalqf == TRUE ~ 1,
TRUE ~ 0
)
) %>%
group_by(hhid_Num) %>%
mutate(mth_has_any_informalqf = sum(has_any))
# Total numbers of joint formalqf and informalqf
tstm_loans_mth_all_stats <- tstm_loans_mth_all_stats %>%
ungroup() %>%
mutate(
has_any =
case_when(
informalqf == TRUE & formalqf == TRUE ~ 1,
TRUE ~ 0
)
) %>%
group_by(hhid_Num) %>%
mutate(mth_has_any_joint = sum(has_any))
# Slice
tstm_loans_mth_all_stats_l1_qf <- tstm_loans_mth_all_stats %>%
group_by(hhid_Num) %>%
slice_head(n = 1) %>%
select(hhid_Num, starts_with("mth"))
print(tstm_loans_mth_all_stats_l1_qf[1:100, ])
#> # A tibble: 100 × 6
#> # Groups: hhid_Num [100]
#> hhid_Num mth_span_hh mth_has_any mth_has_any_formalqf mth_has_any_informalqf
#> <dbl> <int> <dbl> <dbl> <dbl>
#> 1 70201 18 0 0 0
#> 2 70202 135 0 0 0
#> 3 70203 133 101 97 24
#> 4 70204 135 135 122 55
#> 5 70205 133 85 76 37
#> 6 70206 135 131 106 80
#> 7 70207 135 103 103 0
#> 8 70208 135 41 2 39
#> 9 70209 135 25 25 0
#> 10 70210 135 102 79 35
#> # ℹ 90 more rows
#> # ℹ 1 more variable: mth_has_any_joint <dbl>
# write_csv(tstm_loans_mth_all_stats_l1, "../data-raw/tstm_loans_mth_all_stats_l1.csv")
Overlapping conditional probability visualization
Formal and Informal Definition Main
df_loan_condi <- tstm_loans_mth_all_stats_l1 %>%
mutate(
inf_condi_for = mth_has_any_joint / mth_has_any_formal,
for_condi_inf = mth_has_any_joint / mth_has_any_informal,
share_has_loan = mth_has_any / mth_span_hh
) %>%
filter(mth_span_hh >= 100) %>%
ungroup() %>%
summarize(
inf_condi_for_mean = mean(inf_condi_for, na.rm = TRUE),
for_condi_inf_mean = mean(for_condi_inf, na.rm = TRUE),
share_has_loan_mean = mean(share_has_loan, na.rm = TRUE)
)
print(df_loan_condi)
#> # A tibble: 1 × 3
#> inf_condi_for_mean for_condi_inf_mean share_has_loan_mean
#> <dbl> <dbl> <dbl>
#> 1 0.530 0.785 0.790
Probability of formal conditional on informal.
# Color list
ls_colors <- c("P(For=1|Inf=1, Month)" = "blue", "P(Inf=1|For=1, Month)" = "red", "P(Loan|Month)" = "orange")
pl_density_pforcinf <- tstm_loans_mth_all_stats_l1 %>%
mutate(
inf_condi_for = mth_has_any_joint / mth_has_any_formal,
for_condi_inf = mth_has_any_joint / mth_has_any_informal,
share_has_loan = mth_has_any / mth_span_hh
) %>%
filter(mth_span_hh >= 100) %>%
ggplot() +
# geom_density(aes(share_has_loan), alpha=0.5, stat = "density", position = "identity", color="#0072B2") +
geom_density(aes(x = for_condi_inf, color = "P(For=1|Inf=1, Month)"),
size = 1,
alpha = 1, stat = "density", position = "identity"
) +
# geom_density(aes(x = inf_condi_for, color="P(Inf=1|For=1, Month)"), alpha=0.5, stat = "density", position = "identity") +
scale_color_manual(values = ls_colors, name = "") +
theme_minimal() +
labs(
x = paste0("Conditional probability (share of months, unit-of-obs: household)"),
y = paste0("Density"),
title = paste0(
"Chance in month with informal loan outstanding, there is also formal loan outstanding\n",
"(Quasi-formal grouped with informal)\n",
"Solid blue: P(formal = 1 | informal = 1, month)",
sep = ""
),
caption = paste(
"Townsend Thai Monthly data up 1998 to 2011",
sep = ""
)
)
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
print(pl_density_pforcinf)
#> Warning: Removed 103 rows containing non-finite outside the scale range
#> (`stat_density()`).
Add on to this probability of informal conditional on formal.
pl_density_pforcinf_pinfcfor <- tstm_loans_mth_all_stats_l1 %>%
mutate(
inf_condi_for = mth_has_any_joint / mth_has_any_formal,
for_condi_inf = mth_has_any_joint / mth_has_any_informal,
share_has_loan = mth_has_any / mth_span_hh
) %>%
filter(mth_span_hh >= 100) %>%
ggplot() +
# geom_density(aes(share_has_loan), alpha=0.5, stat = "density", position = "identity", color="#0072B2") +
geom_density(aes(x = for_condi_inf, color = "P(For=1|Inf=1, Month)"),
size = 1, stat = "density", position = "identity"
) +
geom_density(aes(x = inf_condi_for, color = "P(Inf=1|For=1, Month)"),
size = 1.5, linetype = "longdash", stat = "density", position = "identity"
) +
scale_color_manual(values = ls_colors) +
theme_minimal() +
labs(
x = paste0("Conditional probability (share of months, unit-of-obs: household)"),
y = paste0("Density"),
title = paste0(
"Chance in month with inf (for) loan outstand., there is also for (inf) loan outstand.\n",
"(Quasi-formal grouped with informal)\n",
"Solid blue: P(formal = 1 | informal = 1, month)\n",
"Dashed red: P(informal = 1 | formal = 1, month)",
sep = ""
),
caption = paste(
"Townsend Thai Monthly data up 1998 to 2011",
sep = ""
)
)
print(pl_density_pforcinf_pinfcfor)
#> Warning: Removed 103 rows containing non-finite outside the scale range
#> (`stat_density()`).
#> Warning: Removed 42 rows containing non-finite outside the scale range
#> (`stat_density()`).
pl_hist_phasloan <- tstm_loans_mth_all_stats_l1 %>%
mutate(
inf_condi_for = mth_has_any_joint / mth_has_any_formal,
for_condi_inf = mth_has_any_joint / mth_has_any_informal,
share_has_loan = mth_has_any / mth_span_hh
) %>%
filter(mth_span_hh >= 100) %>%
ggplot() +
geom_histogram(aes(share_has_loan), stat = "density", position = "identity") +
theme_minimal() +
labs(
x = paste0("Share of months with any loans outstanding (unit-of-obs: household)"),
y = paste0("Density"),
title = paste0(
"Share of months in which a household has any loans outstanding\n",
"(Households with at least 100 months in survey)",
sep = ""
),
caption = paste(
"Townsend Thai Monthly data up 1998 to 2011",
sep = ""
)
)
#> Warning in geom_histogram(aes(share_has_loan), stat = "density", position =
#> "identity"): Ignoring unknown parameters: `binwidth`, `bins`, and `pad`
print(pl_hist_phasloan)
Formal and Informal Definition Auxilliary
df_loan_condi <- tstm_loans_mth_all_stats_l1_qf %>%
mutate(
inf_condi_for = mth_has_any_joint / mth_has_any_formalqf,
for_condi_inf = mth_has_any_joint / mth_has_any_informalqf,
share_has_loan = mth_has_any / mth_span_hh
) %>%
filter(mth_span_hh >= 100) %>%
ungroup() %>%
summarize(
inf_condi_for_mean = mean(inf_condi_for, na.rm = TRUE),
for_condi_inf_mean = mean(for_condi_inf, na.rm = TRUE),
share_has_loan_mean = mean(share_has_loan, na.rm = TRUE)
)
print(df_loan_condi)
#> # A tibble: 1 × 3
#> inf_condi_for_mean for_condi_inf_mean share_has_loan_mean
#> <dbl> <dbl> <dbl>
#> 1 0.485 0.814 0.790
# Color list
ls_colors <- c("P(For=1|Inf=1, Month)" = "blue", "P(Inf=1|For=1, Month)" = "red", "P(Loan|Month)" = "orange")
pl_for_inf_jnt_1a_qf <- tstm_loans_mth_all_stats_l1_qf %>%
mutate(
inf_condi_for = mth_has_any_joint / mth_has_any_formalqf,
for_condi_inf = mth_has_any_joint / mth_has_any_informalqf,
share_has_loan = mth_has_any / mth_span_hh
) %>%
filter(mth_span_hh >= 100) %>%
ggplot() +
# geom_density(aes(share_has_loan), alpha=0.5, stat = "density", position = "identity", color="#0072B2") +
geom_density(aes(x = for_condi_inf, color = "P(For=1|Inf=1, Month)"), alpha = 0.5, stat = "density", position = "identity") +
# geom_density(aes(x = inf_condi_for, color="P(Inf=1|For=1, Month)"), alpha=0.5, stat = "density", position = "identity") +
scale_color_manual(values = ls_colors) +
theme_minimal() +
labs(
x = paste0("Conditional probaiblity share of months"),
y = paste0("Density"),
title = paste(
"Chance in month with formal (informal) loan outstanding, there is also informal (formal) loan outstanding.\n",
"P(Has formal loan|Has informal loan, Month)\n",
"P(Has informal loan|Has formal loan, Month)",
sep = " "
),
caption = paste(
"Townsend Thai Monthly data up 1998 to 2011",
sep = ""
)
)
print(pl_for_inf_jnt_1a_qf)
#> Warning: Removed 119 rows containing non-finite outside the scale range
#> (`stat_density()`).
pl_for_inf_jnt_1b_qf <- tstm_loans_mth_all_stats_l1_qf %>%
mutate(
inf_condi_for = mth_has_any_joint / mth_has_any_formalqf,
for_condi_inf = mth_has_any_joint / mth_has_any_informalqf,
share_has_loan = mth_has_any / mth_span_hh
) %>%
filter(mth_span_hh >= 100) %>%
ggplot() +
# geom_density(aes(share_has_loan), alpha=0.5, stat = "density", position = "identity", color="#0072B2") +
geom_density(aes(x = for_condi_inf, color = "P(For=1|Inf=1, Month)"), alpha = 0.5, stat = "density", position = "identity") +
geom_density(aes(x = inf_condi_for, color = "P(Inf=1|For=1, Month)"), alpha = 0.5, stat = "density", position = "identity") +
scale_color_manual(values = ls_colors, name = "") +
theme_minimal() +
labs(
x = paste0("Conditional probaiblity share of months"),
y = paste0("Density"),
title = paste(
"Chance in month with formal (informal) loan outstanding, there is also informal (formal) loan outstanding.\n",
"P(Has formal loan|Has informal loan, Month)\n",
"P(Has informal loan|Has formal loan, Month)",
sep = " "
),
caption = paste(
"Townsend Thai Monthly data up 1998 to 2011",
sep = ""
)
)
print(pl_for_inf_jnt_1b_qf)
#> Warning: Removed 119 rows containing non-finite outside the scale range
#> (`stat_density()`).
#> Warning: Removed 36 rows containing non-finite outside the scale range
#> (`stat_density()`).
pl_for_inf_jnt_2_qf <- tstm_loans_mth_all_stats_l1_qf %>%
mutate(
inf_condi_for = mth_has_any_joint / mth_has_any_formalqf,
for_condi_inf = mth_has_any_joint / mth_has_any_informalqf,
share_has_loan = mth_has_any / mth_span_hh
) %>%
filter(mth_span_hh >= 135) %>%
ggplot() +
geom_histogram(aes(mth_has_any), alpha = 0.5, stat = "bin", position = "identity") +
theme_minimal() +
labs(
x = paste0("Number of months with any loans outstanding"),
y = paste0("Frequency (months)"),
title = paste(
"Number of months in which a household has any loans outstanding\n",
# "All loan typess",
sep = " "
),
caption = paste(
"Townsend Thai Monthly data up 1998 to 2011",
sep = ""
)
)
print(pl_for_inf_jnt_2_qf)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Saving Figures
Now we save the figures we have generates
if (bl_save_figs) {
# Project root
spt_root_prj <- ffs_hfid_path()$spt_root_prj_main_dropbox
ar_it_save_group <- c(1, 2)
for (it_grp in ar_it_save_group) {
# Print info
print(glue::glue("Save fig, group = {it_grp}"))
# Path
spt_img_folder <- file.path(
spt_root_prj,
"summ", "summ_loan_overlap", "res_2023_1211",
fsep = .Platform$file.sep
)
if (it_grp == 1) {
# Plots and names
# plots
ls_plts <- list(
pl_density_pforcinf,
pl_density_pforcinf_pinfcfor
)
# Names
ls_plts_names <- c(
"pl_density_pforcinf",
"pl_density_pforcinf_pinfcfor"
)
}
if (it_grp == 2) {
# Plots and names
# plots
ls_plts <- list(
pl_hist_phasloan
)
# Names
ls_plts_names <- c(
"pl_hist_phasloan"
)
}
# Save plot one by one
it_len_figs <- length(ls_plts_names)
for (it_fig in 1:1:it_len_figs) {
# Print info
print(glue::glue("Save fig, figure count = {it_fig}"))
# Get plot
pl_fig <- ls_plts[[it_fig]]
st_fig_name <- ls_plts_names[it_fig]
# Image save path
spn_graph_png <- file.path(spt_img_folder, paste0(st_fig_name, ".png"),
fsep = .Platform$file.sep
)
spn_graph_eps <- file.path(spt_img_folder, paste0(st_fig_name, ".eps"),
fsep = .Platform$file.sep
)
# Plot
print(pl_fig)
png(spn_graph_png,
width = 250,
height = 150, units = "mm",
res = 150, pointsize = 7
)
ggsave(spn_graph_eps,
plot = last_plot(),
device = "eps",
path = NULL,
scale = 1,
width = 250,
height = 150,
units = c("mm"),
dpi = 150,
limitsize = TRUE
)
print(pl_fig)
dev.off()
}
}
}