diff --git a/404.html b/404.html index cfb7f37..38e1b1c 100644 --- a/404.html +++ b/404.html @@ -24,7 +24,7 @@ unitdid - 0.0.4.1 + 0.0.5 diff --git a/LICENSE-text.html b/LICENSE-text.html index 38585a2..d57ae2a 100644 --- a/LICENSE-text.html +++ b/LICENSE-text.html @@ -10,7 +10,7 @@ unitdid - 0.0.4.1 + 0.0.5 diff --git a/LICENSE.html b/LICENSE.html index caf3c8d..3a161bc 100644 --- a/LICENSE.html +++ b/LICENSE.html @@ -10,7 +10,7 @@ unitdid - 0.0.4.1 + 0.0.5 diff --git a/articles/base_heterocp/figure/unnamed-chunk-2-1.svg b/articles/base_heterocp/figure/unnamed-chunk-2-1.svg index 42262ec..391a6d6 100644 --- a/articles/base_heterocp/figure/unnamed-chunk-2-1.svg +++ b/articles/base_heterocp/figure/unnamed-chunk-2-1.svg @@ -85,7 +85,7 @@ - + diff --git a/articles/base_heterocp/figure/unnamed-chunk-4-1.svg b/articles/base_heterocp/figure/unnamed-chunk-4-1.svg index 3efa702..5b5f9d5 100644 --- a/articles/base_heterocp/figure/unnamed-chunk-4-1.svg +++ b/articles/base_heterocp/figure/unnamed-chunk-4-1.svg @@ -334,7 +334,7 @@ - + diff --git a/articles/base_heterocp/figure/unnamed-chunk-5-1.svg b/articles/base_heterocp/figure/unnamed-chunk-5-1.svg index 8482741..9582eea 100644 --- a/articles/base_heterocp/figure/unnamed-chunk-5-1.svg +++ b/articles/base_heterocp/figure/unnamed-chunk-5-1.svg @@ -301,7 +301,7 @@ - + diff --git a/articles/base_heterocp/index.html b/articles/base_heterocp/index.html index 5c9c770..dc69d4c 100644 --- a/articles/base_heterocp/index.html +++ b/articles/base_heterocp/index.html @@ -26,7 +26,7 @@ unitdid - 0.0.4.1 + 0.0.5 @@ -69,7 +69,7 @@ Kazuharu Yanagimoto - March 17, 2024 + March 27, 2024 index.Rmd @@ -178,7 +178,7 @@ Simulation= "year", ename = "cyear", bname = "byear", - compute_var = TRUE) + compute_varcov = "var") summary(mdl_base, agg = "event_age") } diff --git a/articles/example_psid/figure/mean_cage-1.svg b/articles/example_psid/figure/mean_cage-1.svg index 0f0ea86..e34ba82 100644 --- a/articles/example_psid/figure/mean_cage-1.svg +++ b/articles/example_psid/figure/mean_cage-1.svg @@ -244,7 +244,7 @@ - + diff --git a/articles/example_psid/figure/sd_cage-1.svg b/articles/example_psid/figure/sd_cage-1.svg index 5131113..9b8d510 100644 --- a/articles/example_psid/figure/sd_cage-1.svg +++ b/articles/example_psid/figure/sd_cage-1.svg @@ -262,7 +262,7 @@ - + diff --git a/articles/example_psid/figure/unnamed-chunk-2-1.svg b/articles/example_psid/figure/unnamed-chunk-2-1.svg index b95aa32..4ef5a4d 100644 --- a/articles/example_psid/figure/unnamed-chunk-2-1.svg +++ b/articles/example_psid/figure/unnamed-chunk-2-1.svg @@ -1219,7 +1219,7 @@ - + diff --git a/articles/example_psid/figure/unnamed-chunk-5-1.svg b/articles/example_psid/figure/unnamed-chunk-5-1.svg index 9417e2f..2b0bbb0 100644 --- a/articles/example_psid/figure/unnamed-chunk-5-1.svg +++ b/articles/example_psid/figure/unnamed-chunk-5-1.svg @@ -784,7 +784,7 @@ - + diff --git a/articles/example_psid/index.html b/articles/example_psid/index.html index 0dea012..69edf2f 100644 --- a/articles/example_psid/index.html +++ b/articles/example_psid/index.html @@ -26,7 +26,7 @@ unitdid - 0.0.4.1 + 0.0.5 @@ -69,7 +69,7 @@ Kazuharu Yanagimoto - March 17, 2024 + March 27, 2024 index.Rmd @@ -157,7 +157,7 @@ Analysis of Child Penaltie ename = "cyear", bname = "byear", by = c("gender"), - compute_var = TRUE) + compute_varcov = "var") Mean of Child Penalties on Participation diff --git a/articles/index.html b/articles/index.html index c23e4eb..0a4481f 100644 --- a/articles/index.html +++ b/articles/index.html @@ -10,7 +10,7 @@ unitdid - 0.0.4.1 + 0.0.5 diff --git a/authors.html b/authors.html index a85f69f..281fa31 100644 --- a/authors.html +++ b/authors.html @@ -10,7 +10,7 @@ unitdid - 0.0.4.1 + 0.0.5 @@ -59,13 +59,13 @@ Citation Yanagimoto K (2024). unitdid: Unit-level Difference-in-Difference Estimator. -R package version 0.0.4.1, http://kazuyanagimoto.com/unitdid/. +R package version 0.0.5, http://kazuyanagimoto.com/unitdid/. @Manual{, title = {unitdid: Unit-level Difference-in-Difference Estimator}, author = {Kazuharu Yanagimoto}, year = {2024}, - note = {R package version 0.0.4.1}, + note = {R package version 0.0.5}, url = {http://kazuyanagimoto.com/unitdid/}, } diff --git a/index.html b/index.html index 70a99bd..a93bd42 100644 --- a/index.html +++ b/index.html @@ -26,7 +26,7 @@ unitdid - 0.0.4.1 + 0.0.5 diff --git a/news/index.html b/news/index.html index b667b32..3c0bc37 100644 --- a/news/index.html +++ b/news/index.html @@ -10,7 +10,7 @@ unitdid - 0.0.4.1 + 0.0.5 @@ -45,6 +45,15 @@ + +unitdid 0.0.5 + +New Features +Support for unit-level covariance estimation, which allows for more flexible aggregation using the covariance matrix +Add only_full_horizon option for get_unitdid() + + + unitdid 0.0.4 diff --git a/pkgdown.yml b/pkgdown.yml index 3c75715..e695e1d 100644 --- a/pkgdown.yml +++ b/pkgdown.yml @@ -4,7 +4,7 @@ pkgdown_sha: ~ articles: index: base_heterocp/index.html index: example_psid/index.html -last_built: 2024-03-17T09:13Z +last_built: 2024-03-27T17:07Z urls: reference: http://kazuyanagimoto.com/unitdid/reference article: http://kazuyanagimoto.com/unitdid/articles diff --git a/reference/aggregate_unitdid.html b/reference/aggregate_unitdid.html index dfded0a..6d1981d 100644 --- a/reference/aggregate_unitdid.html +++ b/reference/aggregate_unitdid.html @@ -10,7 +10,7 @@ unitdid - 0.0.4.1 + 0.0.5 diff --git a/reference/base_heterocp.html b/reference/base_heterocp.html index e09ae34..0370a78 100644 --- a/reference/base_heterocp.html +++ b/reference/base_heterocp.html @@ -10,7 +10,7 @@ unitdid - 0.0.4.1 + 0.0.5 diff --git a/reference/gen_heterocp.html b/reference/gen_heterocp.html index 0b6b65f..3a418e7 100644 --- a/reference/gen_heterocp.html +++ b/reference/gen_heterocp.html @@ -10,7 +10,7 @@ unitdid - 0.0.4.1 + 0.0.5 diff --git a/reference/get_unitdid.html b/reference/get_unitdid.html index 51b1aeb..b9e62a7 100644 --- a/reference/get_unitdid.html +++ b/reference/get_unitdid.html @@ -10,7 +10,7 @@ unitdid - 0.0.4.1 + 0.0.5 @@ -52,7 +52,12 @@ Usage - get_unitdid(object, normalized = NULL, export = TRUE) + get_unitdid( + object, + normalized = NULL, + export = TRUE, + only_full_horizon = FALSE +) @@ -71,6 +76,15 @@ Arguments Value diff --git a/reference/index.html b/reference/index.html index 9384214..4be6ab6 100644 --- a/reference/index.html +++ b/reference/index.html @@ -10,7 +10,7 @@ unitdid - 0.0.4.1 + 0.0.5 diff --git a/reference/pipe.html b/reference/pipe.html index 187273a..2cf1ad1 100644 --- a/reference/pipe.html +++ b/reference/pipe.html @@ -10,7 +10,7 @@ unitdid - 0.0.4.1 + 0.0.5 diff --git a/reference/summary.unitdid.html b/reference/summary.unitdid.html index 5931293..c03cdcb 100644 --- a/reference/summary.unitdid.html +++ b/reference/summary.unitdid.html @@ -10,7 +10,7 @@ unitdid - 0.0.4.1 + 0.0.5 diff --git a/reference/unitdid.html b/reference/unitdid.html index 6c18b28..e7f29f5 100644 --- a/reference/unitdid.html +++ b/reference/unitdid.html @@ -10,7 +10,7 @@ unitdid - 0.0.4.1 + 0.0.5 @@ -62,9 +62,11 @@ Usage wname = NULL, ytildename = NULL, yvarname = NULL, + ycovname = NULL, + kprimename = "kprime", k_min = 0, k_max = 5, - compute_var = FALSE, + compute_varcov = "none", by = NULL, bname = NULL, normalized = FALSE @@ -115,6 +117,18 @@ Arguments. +ycovname +Optional. The name of the unit-level covariance of +the outcome variable. If not provided, the function will use +paste0(yname, "_cov"). + + +kprimename +Optional. The name of the relative time to treatment. +This is used for the second column name of the relative time of +the unit-level covariance estimation. Default is "kprime". + + k_min Relative time to treatment at which treatment starts. Default is 0. @@ -125,10 +139,11 @@ Argumentsk_min:k_max. by diff --git a/search.json b/search.json index bb6ba09..0328dcb 100644 --- a/search.json +++ b/search.json @@ -1 +1 @@ -[{"path":"http://kazuyanagimoto.com/unitdid/LICENSE.html","id":null,"dir":"","previous_headings":"","what":"MIT License","title":"MIT License","text":"Copyright (c) 2023 Kazuharu Yanagimoto Permission hereby granted, free charge, person obtaining copy software associated documentation files (“Software”), deal Software without restriction, including without limitation rights use, copy, modify, merge, publish, distribute, sublicense, /sell copies Software, permit persons Software furnished , subject following conditions: copyright notice permission notice shall included copies substantial portions Software. SOFTWARE PROVIDED “”, WITHOUT WARRANTY KIND, EXPRESS IMPLIED, INCLUDING LIMITED WARRANTIES MERCHANTABILITY, FITNESS PARTICULAR PURPOSE NONINFRINGEMENT. EVENT SHALL AUTHORS COPYRIGHT HOLDERS LIABLE CLAIM, DAMAGES LIABILITY, WHETHER ACTION CONTRACT, TORT OTHERWISE, ARISING , CONNECTION SOFTWARE USE DEALINGS SOFTWARE.","code":""},{"path":"http://kazuyanagimoto.com/unitdid/articles/base_heterocp/index.html","id":"data-generation-process","dir":"Articles > Base_heterocp","previous_headings":"","what":"Data Generation Process","title":"Data Generation Process of base_heterocp","text":"sample data set base_heterocp generated unitdid::gen_heterocp() seed 1234. Inside function gen_heterocp(), following steps performed.","code":"set.seed(1234) base_heterocp <- gen_heterocp()"},{"path":"http://kazuyanagimoto.com/unitdid/articles/base_heterocp/index.html","id":"generate-i-1-dots-1000-individuals","dir":"Articles > Base_heterocp","previous_headings":"Data Generation Process","what":"1. Generate \\(i = \\{1, \\dots, 1000\\}\\) individuals","title":"Data Generation Process of base_heterocp","text":"Generate \\(= \\{1, \\dots, 1000\\}\\) individuals age child birth \\(a_i\\) multinomial distribution \\(f()\\). \\[ f() = \\begin{cases} \\frac{- 14}{15 \\cdot 16} & \\text{} \\\\{15, \\dots, 29\\} \\\\ \\frac{45 - }{15 \\cdot 16} & \\text{} \\\\{30, \\dots, 44\\} \\\\ \\end{cases} \\]","code":"base_heterocp |> distinct(id, .keep_all = TRUE) |> mutate(event_age = cyear - byear) |> summarize(n = n(), .by = event_age) |> mutate(density = n / sum(n)) |> ggplot(aes(x = event_age, y = density)) + geom_col() + labs(x = \"Age at First Childbirth\", y = NULL) + theme_minimal() + theme(panel.grid.minor = element_blank(), panel.grid.major.x = element_blank())"},{"path":"http://kazuyanagimoto.com/unitdid/articles/base_heterocp/index.html","id":"generate-individual-fe-alpha_i-and-time-fe-lambda_t","dir":"Articles > Base_heterocp","previous_headings":"Data Generation Process","what":"2. Generate Individual FE \\(\\alpha_i\\) and Time FE \\(\\lambda_t\\)","title":"Data Generation Process of base_heterocp","text":"Generate individual fixed effects \\(\\alpha_i\\) time fixed effects \\(\\lambda_t\\) normal distributions. \\[ \\alpha_{} \\sim \\mathcal{N}(0.15, 0.1),\\, \\lambda_{t} \\sim \\mathcal{N}(0, 0.1) \\]","code":""},{"path":"http://kazuyanagimoto.com/unitdid/articles/base_heterocp/index.html","id":"generate-heterogeneous-treatment-effect-tau_i-k","dir":"Articles > Base_heterocp","previous_headings":"Data Generation Process","what":"3. Generate Heterogeneous Treatment Effect \\(\\tau_{i, k}\\)","title":"Data Generation Process of base_heterocp","text":"individual \\(\\) age first childbirth \\(a_i\\), draw \\(\\tau_{, k} \\sim \\mathcal{N}(\\mu(a_i, k), \\sigma(a_i, k))\\) \\[ \\begin{aligned} \\mu() &= -1.2 + 0.3\\log() \\\\ \\sigma() &= 2.8 - 0.8\\log() \\\\ \\mu(, k) &= \\begin{cases} \\frac{1}{3}\\mu() & \\text{} k = 0 \\\\ \\mu() & \\text{} k = 1 \\\\ 0.05 + 1.5\\mu() - 0.01k & \\text{} k \\ge 2 \\\\ \\end{cases} \\\\ \\sigma(, k) &= \\begin{cases} \\frac{1}{3}\\sigma() & \\text{} k = 0 \\\\ \\sigma() & \\text{} k = 1 \\\\ 0.01 + \\sigma() + 0.01k & \\text{} k \\ge 2 \\\\ \\end{cases} \\end{aligned} \\]","code":""},{"path":"http://kazuyanagimoto.com/unitdid/articles/base_heterocp/index.html","id":"generate-outcome-y_it","dir":"Articles > Base_heterocp","previous_headings":"Data Generation Process","what":"4. Generate Outcome \\(y_{it}\\)","title":"Data Generation Process of base_heterocp","text":"\\[ y_{} = \\alpha_i + \\lambda_t + \\tau_{, k} + \\varepsilon_{} \\] \\(\\varepsilon_{} \\sim \\mathcal{N}(0, 025)\\).","code":""},{"path":"http://kazuyanagimoto.com/unitdid/articles/base_heterocp/index.html","id":"simulation","dir":"Articles > Base_heterocp","previous_headings":"","what":"Simulation","title":"Data Generation Process of base_heterocp","text":"see estimation properties, simulate data set 100 times compare true values mean, 2.5% quantile, 97.5% quantile estimated values.","code":"set.seed(1234) n_sim <- 100 sim <- function() { mdl_base <- gen_heterocp(size_cohort = 1000) |> unitdid(yname = \"y\", iname = \"id\", tname = \"year\", ename = \"cyear\", bname = \"byear\", compute_var = TRUE) summary(mdl_base, agg = \"event_age\") } path_sim <- here::here(\"vignettes/base_heterocp/simulated.rds\") if (!file.exists(path_sim)) { simulated <- map(1:n_sim, ~sim(), .progress = TRUE) |> list_rbind() save(simulated, file = path_sim) } else { load(path_sim) } val_true <- tibble( event_age = 25:34, tau1 = -1.2 + 0.3 * log(event_age), sd1 = 3 - 0.75 * log(event_age)) |> slice(rep(1:n(), each = 6)) |> mutate(rel_time = rep(0:5, 10), mean = case_when( rel_time == 0 ~ tau1 / 3, rel_time == 1 ~ tau1, rel_time > 1 ~ 0.05 + 1.5 * tau1 - 0.02 * rel_time), sd = case_when( rel_time == 0 ~ sd1 / 3, rel_time == 1 ~ sd1, rel_time > 1 ~ 0.01 + sd1 + 0.02 * rel_time), lbl = \"Truth\") |> select(-c(tau1, sd1)) df_plot <- simulated |> mutate(sd = sqrt(var)) |> summarize(across(c(mean, sd), list(\"lci\" = ~quantile(.x, 0.025), \"rci\" = ~quantile(.x, 0.975))), across(c(mean, sd), mean), .by = c(event_age, rel_time)) |> mutate(lbl = \"Simulation\") |> bind_rows(val_true) |> mutate(lbl_facet = paste0(\"Age \", event_age)) |> filter(between(event_age, 25, 34))"},{"path":"http://kazuyanagimoto.com/unitdid/articles/base_heterocp/index.html","id":"mean-of-tau_ik","dir":"Articles > Base_heterocp","previous_headings":"Simulation","what":"Mean of \\(\\tau_{ik}\\)","title":"Data Generation Process of base_heterocp","text":"","code":"df_plot |> ggplot(aes(x = rel_time, y = mean, ymin = mean_lci, ymax = mean_rci, color = lbl, shape = lbl)) + geom_point(position = position_dodge(width = 0.3)) + geom_errorbar(width = 0.2, position = position_dodge(width = 0.3)) + geom_vline(xintercept = -1, linetype = \"dashed\") + geom_hline(yintercept = 0) + scale_color_manual(values = c(\"#009F8C\", \"#B75C9D\")) + facet_wrap(~lbl_facet, ncol = 5) + labs(x = \"Time to Treatment\", y = NULL, color = NULL, shape = NULL) + theme_minimal() + theme(panel.grid.minor = element_blank(), panel.grid.major.x = element_blank(), legend.position = \"bottom\")"},{"path":"http://kazuyanagimoto.com/unitdid/articles/base_heterocp/index.html","id":"standard-deviation-of-tau_ik","dir":"Articles > Base_heterocp","previous_headings":"Simulation","what":"Standard Deviation of \\(\\tau_{ik}\\)","title":"Data Generation Process of base_heterocp","text":"","code":"df_plot |> ggplot(aes(x = rel_time, y = sd, ymin = sd_lci, ymax = sd_rci, color = lbl, shape = lbl)) + geom_point(position = position_dodge(width = 0.3)) + geom_errorbar(width = 0.2, position = position_dodge(width = 0.3)) + geom_vline(xintercept = -1, linetype = \"dashed\") + geom_hline(yintercept = 0) + scale_color_manual(values = c(\"#009F8C\", \"#B75C9D\")) + facet_wrap(~lbl_facet, ncol = 5) + labs(x = \"Time to Treatment\", y = NULL, color = NULL, shape = NULL) + theme_minimal() + theme(panel.grid.minor = element_blank(), panel.grid.major.x = element_blank(), legend.position = \"bottom\")"},{"path":"http://kazuyanagimoto.com/unitdid/articles/example_psid/index.html","id":"panel-study-of-income-dynamics-psid","dir":"Articles > Example_psid","previous_headings":"","what":"Panel Study of Income Dynamics (PSID)","title":"Individual-level Child Penalties with PSID","text":"PSID data longitudinal household survey United States one longest famous panel surveys world. study began 1968 nationally representative sample 18,000 individuals living 5,000 families. data available registered users. avoid convoluted cleaning tasks, mostly use cleaning codes provided Comparative Panel File (CPF). cleaning codes developed creating harmonized panel data seven countries (Australia, Germany, UK, South Korea, Russia, Switzerland, US). replicate article, need run step 4 (Add vars constant across waves) CPF-Code/03_PSID/us_01_3_GEt_vars., add ER32024 (birth year first child) us_01.dta-us_03.dta produced Run following code create dataset child penalties","code":"library(haven) cpf_us01 <- read_dta(here(\"vignettes/example_psid/us_01.dta\")) cpf_us03 <- read_dta(here(\"vignettes/example_psid/us_03_CPF.dta\")) ever_birth <- cpf_us01 |> select(pid, cyear = ER32024) |> filter(cyear != 9999) |> distinct() cp_psid <- cpf_us03 |> filter(country == 3) |> select(id = pid, year = intyear, byear = yborn, gender = female, earn = incjobs_yg, hours = whweek, years_edu = eduy) |> mutate(gender = if_else(gender == 0, \"Men\", \"Women\"), particip = if_else(hours > 0, 1, 0), wage = if_else(particip == 1, earn / hours, 0), years_edu = if_else(years_edu < 0, NA_integer_, years_edu)) |> right_join(ever_birth, by = c(\"id\" = \"pid\")) |> mutate(rel_time = year - cyear) |> filter(!is.na(earn)) cpf_us01 |> glimpse() save(cp_psid, file = here(\"vignettes/example_psid/cp_psid.rds\"))"},{"path":"http://kazuyanagimoto.com/unitdid/articles/example_psid/index.html","id":"analysis-of-child-penalties-on-participation","dir":"Articles > Example_psid","previous_headings":"","what":"Analysis of Child Penalties on Participation","title":"Individual-level Child Penalties with PSID","text":"","code":"load(here(\"vignettes/example_psid/cp_psid.rds\")) cp_psid <- cp_psid |> mutate(eage = cyear - byear) |> filter(cyear >= 1968, year <= 1997, # The last year of the annual data between(eage, 22, 40)) # The Age range of the first childbirth mdl_particip <- cp_psid |> unitdid(yname = \"particip\", iname = \"id\", tname = \"year\", ename = \"cyear\", bname = \"byear\", by = c(\"gender\"), compute_var = TRUE)"},{"path":"http://kazuyanagimoto.com/unitdid/articles/example_psid/index.html","id":"mean-of-child-penalties-on-participation","dir":"Articles > Example_psid","previous_headings":"Analysis of Child Penalties on Participation","what":"Mean of Child Penalties on Participation","title":"Individual-level Child Penalties with PSID","text":"","code":"sum_particip <- summary(mdl_particip, agg = \"event_age\", na.rm = TRUE) |> filter(between(event_age, 26, 32)) df_plot <- sum_particip |> filter(rel_time == 0) |> mutate(rel_time = -1, mean = 0, var = 0) |> bind_rows(sum_particip) |> mutate(lbl_facet = paste0(\"Age \", event_age)) df_plot |> ggplot(aes(x = rel_time, y = mean, color = gender, shape = gender)) + geom_point() + geom_line() + geom_vline(xintercept = -1, linetype = \"dashed\") + geom_hline(yintercept = 0) + facet_wrap(~lbl_facet) + scale_color_manual(values = c(\"#009F8C\", \"#B75C9D\")) + labs(x = \"Time to First Childbirth\", y = NULL, color = NULL, shape = NULL) + theme_minimal() + theme(panel.grid.minor = element_blank(), panel.grid.major.x = element_blank(), legend.position = \"bottom\")"},{"path":"http://kazuyanagimoto.com/unitdid/articles/example_psid/index.html","id":"standard-deviation-of-child-penalties-on-participation","dir":"Articles > Example_psid","previous_headings":"Analysis of Child Penalties on Participation","what":"Standard Deviation of Child Penalties on Participation","title":"Individual-level Child Penalties with PSID","text":"","code":"df_plot |> ggplot(aes(x = rel_time, y = sqrt(var), color = gender, shape = gender)) + geom_point() + geom_line() + geom_vline(xintercept = -1, linetype = \"dashed\") + geom_hline(yintercept = 0) + facet_wrap(~lbl_facet) + scale_color_manual(values = c(\"#009F8C\", \"#B75C9D\")) + labs(x = \"Time to First Childbirth\", y = NULL, color = NULL, shape = NULL) + theme_minimal() + theme(panel.grid.minor = element_blank(), panel.grid.major.x = element_blank(), legend.position = \"bottom\")"},{"path":"http://kazuyanagimoto.com/unitdid/articles/example_psid/index.html","id":"scatter-plot-with-covariates","dir":"Articles > Example_psid","previous_headings":"","what":"Scatter Plot with Covariates","title":"Individual-level Child Penalties with PSID","text":"highlight flexibility individual child penalties, let’s plot relationship years education child penalties.","code":"library(patchwork) unitdid_particip <- get_unitdid(mdl_particip) plot_scatter <- function(gender) { clr <- ifelse(gender == \"Men\", \"#009F8C\", \"#B75C9D\") unitdid_particip |> filter(gender == {{gender}}, between(rel_time, 0, 5)) |> mutate(lbl_facet = paste0(\"k = \", rel_time)) |> ggplot(aes(x = years_edu, y = particip_tilde)) + geom_point(color = clr, alpha = 0.1) + geom_smooth(method = \"lm\", color = clr, fill = clr) + facet_wrap(~lbl_facet, nrow = 1) + labs(x = \"Years of Education\", y = \"CP on Participation\", title = gender) + coord_cartesian(xlim = c(6, NA), ylim = c(-1.25, 0.5)) + theme_minimal() } p1 <- plot_scatter(\"Men\") p2 <- plot_scatter(\"Women\") p1 / p2"},{"path":"http://kazuyanagimoto.com/unitdid/articles/example_psid/index.html","id":"on-binscatter","dir":"Articles > Example_psid","previous_headings":"Scatter Plot with Covariates","what":"On Binscatter","title":"Individual-level Child Penalties with PSID","text":"Another way visualize relationship use binscatter (Cattaneo et al. 2023).","code":"library(binsreg) compute_binsreg <- function(gender, rel_time) { est <- unitdid_particip |> filter(gender == {{ gender }}, rel_time == {{ rel_time }}) |> as.data.frame() |> binsreg(x = years_edu, y = particip_tilde, data = _) est$data.plot$`Group Full Sample`$data.dots |> mutate(gender = gender, rel_time = rel_time) } bs <- map2(rep(c(\"Men\", \"Women\"), times = 6), rep(0:5, each = 2), ~compute_binsreg(.x, .y)) |> list_rbind() plot_binsreg <- function(gender) { clr <- ifelse(gender == \"Men\", \"#009F8C\", \"#B75C9D\") bs |> filter(gender == {{ gender }}) |> mutate(lbl_facet = paste0(\"k = \", rel_time)) |> ggplot(aes(x = x, y = fit)) + geom_point(color = clr) + facet_wrap(~lbl_facet, nrow = 1) + labs(x = \"Years of Education\", y = \"CP on Participation\", title = gender) + theme_minimal() } p1 <- plot_binsreg(\"Men\") p2 <- plot_binsreg(\"Women\") p1 / p2"},{"path":[]},{"path":"http://kazuyanagimoto.com/unitdid/authors.html","id":null,"dir":"","previous_headings":"","what":"Authors","title":"Authors and Citation","text":"Kazuharu Yanagimoto. Author, maintainer.","code":""},{"path":"http://kazuyanagimoto.com/unitdid/authors.html","id":"citation","dir":"","previous_headings":"","what":"Citation","title":"Authors and Citation","text":"Yanagimoto K (2024). unitdid: Unit-level Difference--Difference Estimator. R package version 0.0.4.1, http://kazuyanagimoto.com/unitdid/.","code":"@Manual{, title = {unitdid: Unit-level Difference-in-Difference Estimator}, author = {Kazuharu Yanagimoto}, year = {2024}, note = {R package version 0.0.4.1}, url = {http://kazuyanagimoto.com/unitdid/}, }"},{"path":"http://kazuyanagimoto.com/unitdid/index.html","id":"unitdid","dir":"","previous_headings":"","what":"Unit-level Difference-in-Difference Estimator","title":"Unit-level Difference-in-Difference Estimator","text":"unitdid package provides set functions analysis unit-level difference--differences (Arkhangelsky, Yanagimoto, Zohar, 2024)","code":""},{"path":"http://kazuyanagimoto.com/unitdid/index.html","id":"installation","dir":"","previous_headings":"","what":"Installation","title":"Unit-level Difference-in-Difference Estimator","text":"can install development version unitdid GitHub :","code":"# install.packages(\"remotes\") remotes::install_github(\"kazuyanagimoto/unitdid\")"},{"path":"http://kazuyanagimoto.com/unitdid/index.html","id":"example","dir":"","previous_headings":"","what":"Example","title":"Unit-level Difference-in-Difference Estimator","text":"basic example simulated base_heterocp data set: Individual-level child penalties estimated unitdid():","code":"library(unitdid) library(dplyr) library(ggplot2) base_heterocp |> head() #> # A tibble: 6 × 5 #> id year byear cyear y #> #> 1 1 1999 1955 1985 -0.848 #> 2 1 2000 1955 1985 0.759 #> 3 1 2001 1955 1985 -1.03 #> 4 1 2002 1955 1985 0.858 #> 5 1 2003 1955 1985 -0.866 #> 6 1 2004 1955 1985 -0.651 mdl_base <- base_heterocp |> unitdid(yname = \"y\", iname = \"id\", tname = \"year\", ename = \"cyear\", bname = \"byear\") # Estimated individual-level child penalties (y_tilde) get_unitdid(mdl_base) #> # A tibble: 32,257 × 6 #> id year byear cyear y y_tilde #> #> 1 705 2000 1957 2000 0.138 -0.0287 #> 2 997 2000 1958 2000 0.138 0.0849 #> 3 998 2000 1958 2000 0.119 -0.104 #> 4 1013 2000 1958 2000 0.115 -0.0000709 #> 5 1082 2000 1958 2000 0.0362 0.00549 #> 6 1127 2000 1958 2000 0.386 0.125 #> 7 1225 2001 1959 2001 0.158 -0.118 #> 8 1228 2000 1959 2000 0.241 -0.0937 #> 9 1228 2001 1959 2000 0.443 0.0226 #> 10 1230 2000 1959 2000 0.143 -0.0266 #> # ℹ 32,247 more rows"},{"path":"http://kazuyanagimoto.com/unitdid/index.html","id":"aggregation-of-individual-level-child-penalties","dir":"","previous_headings":"Example","what":"Aggregation of Individual-level Child Penalties","title":"Unit-level Difference-in-Difference Estimator","text":"can aggregated full, event (year event (treatment). Mainly staggered design), event_age (age event. Mainly child penalties) levels:","code":"summary(mdl_base) # default agg = \"full\" #> # A tibble: 6 × 3 #> rel_time mean n #> #> 1 0 -0.0653 4357 #> 2 1 -0.193 4357 #> 3 2 -0.307 4357 #> 4 3 -0.310 4357 #> 5 4 -0.350 4357 #> 6 5 -0.349 4357 sum_eage <- summary(mdl_base, agg = \"event_age\") sum_eage |> filter(rel_time == 0) |> mutate(rel_time = -1, mean = 0) |> bind_rows(sum_eage) |> filter(between(event_age, 25, 34)) |> mutate(lbl_facet = paste0(\"Age \", event_age)) |> ggplot(aes(x = rel_time, y = mean)) + geom_point() + geom_line() + geom_vline(xintercept = -1, linetype = \"dashed\") + geom_hline(yintercept = 0) + facet_wrap(~lbl_facet, ncol = 5) + labs(x = \"Time to First Childbirth\", y = \"Child Penalties on y\") + theme_minimal() + theme(panel.grid.major.x = element_blank(), panel.grid.minor = element_blank())"},{"path":"http://kazuyanagimoto.com/unitdid/index.html","id":"variance-of-individual-level-child-penalties","dir":"","previous_headings":"Example","what":"Variance of Individual-level Child Penalties","title":"Unit-level Difference-in-Difference Estimator","text":"Since individual-level child penalties estimated measurement errors, variance y_tilde equal variance individual-level child penalties. compute_var option unitdid estimates variance measurement errors variance individual-level child penalties subtracting variance measurement errors variance y_tilde","code":"mdl_base <- base_heterocp |> unitdid(yname = \"y\", iname = \"id\", tname = \"year\", ename = \"cyear\", bname = \"byear\", compute_var = TRUE) sum_eage <- summary(mdl_base, agg = \"event_age\") sum_eage |> filter(rel_time == 0) |> mutate(rel_time = -1, var = 0) |> bind_rows(sum_eage) |> filter(between(event_age, 25, 34)) |> mutate(lbl_facet = paste0(\"Age \", event_age)) |> ggplot(aes(x = rel_time, y = sqrt(var))) + geom_point() + geom_line() + geom_vline(xintercept = -1, linetype = \"dashed\") + geom_hline(yintercept = 0) + facet_wrap(~lbl_facet, ncol = 5) + labs(x = \"Time to First Childbirth\", y = \"S.D. of Child Penalties\") + theme_minimal() + theme(panel.grid.major.x = element_blank(), panel.grid.minor = element_blank())"},{"path":"http://kazuyanagimoto.com/unitdid/reference/aggregate_unitdid.html","id":null,"dir":"Reference","previous_headings":"","what":"Aggregate the mean and variance of the estimated unit-level DiD effects — aggregate_unitdid","title":"Aggregate the mean and variance of the estimated unit-level DiD effects — aggregate_unitdid","text":"Aggregate mean variance estimated unit-level effects","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/aggregate_unitdid.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Aggregate the mean and variance of the estimated unit-level DiD effects — aggregate_unitdid","text":"","code":"aggregate_unitdid( object, agg = \"full\", na.rm = TRUE, by = NULL, normalized = NULL, allow_negative_var = FALSE, only_full_horizon = TRUE )"},{"path":"http://kazuyanagimoto.com/unitdid/reference/aggregate_unitdid.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Aggregate the mean and variance of the estimated unit-level DiD effects — aggregate_unitdid","text":"object unitdid object agg Aggregation method. One c(\"full\", \"event\", \"event_age\") default full. provided model, options separately aggregate group. event option aggregates group event timing. event_age option aggregates group age event time. event_age requires bname provided model. na.rm Logical. TRUE, remove NA values aggregation. default TRUE. character vector variables aggregate separately . Default inherited unitdid object can override . can estimate unit-level effects separately unitdid can also aggregate estimates (higher-level) . can use \"rel_time\" highest level aggregation. normalized Logical. TRUE, function normalize aggregated mean variance mean imputed outcome variable. Default inherited unitdid object. allow_negative_var Logical. FALSE, function return estimated variance trimmed zero. Default FALSE. only_full_horizon Logical. TRUE, aggregate unit-level treatment effect, event year (ename) full horizon (k_min:k_max) included. recommended case want change composition event year (age child penalties) estimated point k_min:k_max. Default TRUE.","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/aggregate_unitdid.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Aggregate the mean and variance of the estimated unit-level DiD effects — aggregate_unitdid","text":"tibble aggregated mean variance estimated unit-level effects","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/base_heterocp.html","id":null,"dir":"Reference","previous_headings":"","what":"Simulated Individual Child Panalty Data — base_heterocp","title":"Simulated Individual Child Panalty Data — base_heterocp","text":"Simulated Individual Child Panalty Data","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/base_heterocp.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Simulated Individual Child Panalty Data — base_heterocp","text":"","code":"base_heterocp"},{"path":[]},{"path":"http://kazuyanagimoto.com/unitdid/reference/base_heterocp.html","id":"base-heterocp","dir":"Reference","previous_headings":"","what":"base_heterocp","title":"Simulated Individual Child Panalty Data — base_heterocp","text":"dataframe 1000 individuals birth year 1965 1984: id Individual identifier year Year observation byear Birth year cage Age first birth rel_time Relative time first birth y Outcome variable","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/base_heterocp.html","id":"source","dir":"Reference","previous_headings":"","what":"Source","title":"Simulated Individual Child Panalty Data — base_heterocp","text":"Generated gen_heterocp() seed 1234","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/gen_heterocp.html","id":null,"dir":"Reference","previous_headings":"","what":"Generate Sample Heterogenous Child Penalty Data — gen_heterocp","title":"Generate Sample Heterogenous Child Penalty Data — gen_heterocp","text":"Generate Sample Heterogenous Child Penalty Data","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/gen_heterocp.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Generate Sample Heterogenous Child Penalty Data — gen_heterocp","text":"","code":"gen_heterocp(size_cohort = 300)"},{"path":"http://kazuyanagimoto.com/unitdid/reference/gen_heterocp.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Generate Sample Heterogenous Child Penalty Data — gen_heterocp","text":"size_cohort n_obsumber individuals per birth year","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/gen_heterocp.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Generate Sample Heterogenous Child Penalty Data — gen_heterocp","text":"sample dataframe heterogenous child penalty age first birth","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/gen_heterocp.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Generate Sample Heterogenous Child Penalty Data — gen_heterocp","text":"","code":"set.seed(1234) base_heterocp <- gen_heterocp()"},{"path":"http://kazuyanagimoto.com/unitdid/reference/get_unitdid.html","id":null,"dir":"Reference","previous_headings":"","what":"Get unit-level Difference-in-Differences estimates — get_unitdid","title":"Get unit-level Difference-in-Differences estimates — get_unitdid","text":"Get unit-level Difference--Differences estimates","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/get_unitdid.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Get unit-level Difference-in-Differences estimates — get_unitdid","text":"","code":"get_unitdid(object, normalized = NULL, export = TRUE)"},{"path":"http://kazuyanagimoto.com/unitdid/reference/get_unitdid.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Get unit-level Difference-in-Differences estimates — get_unitdid","text":"object unitdid object normalized Logical. TRUE, function normalize mean imputed outcome variable. Default inherited unitdid object. export Logical. TRUE, function export columns zz000 prefix, used internal computation.","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/get_unitdid.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Get unit-level Difference-in-Differences estimates — get_unitdid","text":"dataframe new column unit-level estimates","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/pipe.html","id":null,"dir":"Reference","previous_headings":"","what":"Pipe operator — %>%","title":"Pipe operator — %>%","text":"See magrittr::%>% details.","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/pipe.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Pipe operator — %>%","text":"","code":"lhs %>% rhs"},{"path":"http://kazuyanagimoto.com/unitdid/reference/pipe.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Pipe operator — %>%","text":"lhs value magrittr placeholder. rhs function call using magrittr semantics.","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/pipe.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Pipe operator — %>%","text":"result calling rhs(lhs).","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/summary.unitdid.html","id":null,"dir":"Reference","previous_headings":"","what":"Aggregate the mean and variance of the estimated unit-level DiD effects — summary.unitdid","title":"Aggregate the mean and variance of the estimated unit-level DiD effects — summary.unitdid","text":"Aggregate mean variance estimated unit-level effects","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/summary.unitdid.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Aggregate the mean and variance of the estimated unit-level DiD effects — summary.unitdid","text":"","code":"# S3 method for unitdid summary(object, ...)"},{"path":"http://kazuyanagimoto.com/unitdid/reference/summary.unitdid.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Aggregate the mean and variance of the estimated unit-level DiD effects — summary.unitdid","text":"object unitdid object ... aggregate_unitdid arguments","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/summary.unitdid.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Aggregate the mean and variance of the estimated unit-level DiD effects — summary.unitdid","text":"tibble summary statistics","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/summary.unitdid.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Aggregate the mean and variance of the estimated unit-level DiD effects — summary.unitdid","text":"","code":"library(unitdid) mdl_base <- base_heterocp |> unitdid(yname = \"y\", iname = \"id\", tname = \"year\", ename = \"cyear\", bname = \"byear\") summary(mdl_base, agg = \"event_age\") #> # A tibble: 144 × 4 #> mean rel_time n event_age #> #> 1 -0.0344 0 13 15 #> 2 -0.0860 0 42 16 #> 3 -0.170 0 59 17 #> 4 -0.141 0 79 18 #> 5 -0.0883 0 99 19 #> 6 -0.0689 0 107 20 #> 7 -0.115 0 158 21 #> 8 -0.0684 0 165 22 #> 9 -0.0778 0 168 23 #> 10 -0.0567 0 206 24 #> # ℹ 134 more rows"},{"path":"http://kazuyanagimoto.com/unitdid/reference/unitdid.html","id":null,"dir":"Reference","previous_headings":"","what":"A function estimates unit-level difference-in-differences — unitdid","title":"A function estimates unit-level difference-in-differences — unitdid","text":"function estimates unit-level difference--differences","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/unitdid.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"A function estimates unit-level difference-in-differences — unitdid","text":"","code":"unitdid( data, yname, iname, tname, ename, first_stage = NULL, wname = NULL, ytildename = NULL, yvarname = NULL, k_min = 0, k_max = 5, compute_var = FALSE, by = NULL, bname = NULL, normalized = FALSE )"},{"path":"http://kazuyanagimoto.com/unitdid/reference/unitdid.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"A function estimates unit-level difference-in-differences — unitdid","text":"data dataframe containing variables yname Outcome variable iname Unit identifier tname Time variable ename Event timing variable first_stage Formula Y(0). Formula follows fixest::feols. specified, unit (iname) time (tname) fixed effects used. wname Optional. name weight variable. ytildename Optional. name imputed outcome variable. provided, function use paste0(yname, \"_tilde\"). yvarname Optional. name unit-level variance outcome variable. provided, function use paste0(yname, \"_var\"). k_min Relative time treatment treatment starts. Default 0. k_max Relative time treatment treatment ends. Default 5. compute_var Logical. TRUE, function compute variance measurement errors variance unit-level treatment effects. Default FALSE. character vector variables estimate separately . Default NULL. bname Birth year variable. Default NULL. Necessary aggregate estimates age event. normalized Logical. TRUE, function normalize outcome variable scale. Default FALSE.","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/unitdid.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"A function estimates unit-level difference-in-differences — unitdid","text":"unitdid class object.","code":""},{"path":[]},{"path":"http://kazuyanagimoto.com/unitdid/news/index.html","id":"new-features-0-0-4","dir":"Changelog","previous_headings":"","what":"New Features","title":"unitdid 0.0.4","text":"Support unit-level variance estimation, allows flexible aggregation variance estimation Add allow_negative_var option allow negative variance estimation, used automatically trimmed 0 (v0.0.4.1).","code":""},{"path":"http://kazuyanagimoto.com/unitdid/news/index.html","id":"bug-fixes-0-0-4","dir":"Changelog","previous_headings":"","what":"Bug Fixes","title":"unitdid 0.0.4","text":"Measurement error (zz000varcont code) esimated weights (#11)","code":""},{"path":[]},{"path":"http://kazuyanagimoto.com/unitdid/news/index.html","id":"new-features-0-0-3","dir":"Changelog","previous_headings":"","what":"New Features","title":"unitdid 0.0.3","text":"Support first_stage argument parametric first stage estimation Support argument aggregation","code":""},{"path":"http://kazuyanagimoto.com/unitdid/news/index.html","id":"bug-fixes-0-0-3","dir":"Changelog","previous_headings":"","what":"Bug Fixes","title":"unitdid 0.0.3","text":"Bug data[[yname]] includes NA values Bug variance estimation includes NA values (v0.0.3.1) Bug higher category (v0.0.3.2)","code":""},{"path":[]},{"path":"http://kazuyanagimoto.com/unitdid/news/index.html","id":"new-features-0-0-2","dir":"Changelog","previous_headings":"","what":"New Features","title":"unitdid 0.0.2","text":"Support wname argument weighted estimation","code":""},{"path":"http://kazuyanagimoto.com/unitdid/news/index.html","id":"bug-fixes-0-0-2","dir":"Changelog","previous_headings":"","what":"Bug Fixes","title":"unitdid 0.0.2","text":"Normalization summary normalized = FALSE model Bug data[[tname]] includes NA values","code":""},{"path":"http://kazuyanagimoto.com/unitdid/news/index.html","id":"unitdid-001","dir":"Changelog","previous_headings":"","what":"unitdid 0.0.1","title":"unitdid 0.0.1","text":"Initial GitHub submission","code":""}] +[{"path":"http://kazuyanagimoto.com/unitdid/LICENSE.html","id":null,"dir":"","previous_headings":"","what":"MIT License","title":"MIT License","text":"Copyright (c) 2023 Kazuharu Yanagimoto Permission hereby granted, free charge, person obtaining copy software associated documentation files (“Software”), deal Software without restriction, including without limitation rights use, copy, modify, merge, publish, distribute, sublicense, /sell copies Software, permit persons Software furnished , subject following conditions: copyright notice permission notice shall included copies substantial portions Software. SOFTWARE PROVIDED “”, WITHOUT WARRANTY KIND, EXPRESS IMPLIED, INCLUDING LIMITED WARRANTIES MERCHANTABILITY, FITNESS PARTICULAR PURPOSE NONINFRINGEMENT. EVENT SHALL AUTHORS COPYRIGHT HOLDERS LIABLE CLAIM, DAMAGES LIABILITY, WHETHER ACTION CONTRACT, TORT OTHERWISE, ARISING , CONNECTION SOFTWARE USE DEALINGS SOFTWARE.","code":""},{"path":"http://kazuyanagimoto.com/unitdid/articles/base_heterocp/index.html","id":"data-generation-process","dir":"Articles > Base_heterocp","previous_headings":"","what":"Data Generation Process","title":"Data Generation Process of base_heterocp","text":"sample data set base_heterocp generated unitdid::gen_heterocp() seed 1234. Inside function gen_heterocp(), following steps performed.","code":"set.seed(1234) base_heterocp <- gen_heterocp()"},{"path":"http://kazuyanagimoto.com/unitdid/articles/base_heterocp/index.html","id":"generate-i-1-dots-1000-individuals","dir":"Articles > Base_heterocp","previous_headings":"Data Generation Process","what":"1. Generate \\(i = \\{1, \\dots, 1000\\}\\) individuals","title":"Data Generation Process of base_heterocp","text":"Generate \\(= \\{1, \\dots, 1000\\}\\) individuals age child birth \\(a_i\\) multinomial distribution \\(f()\\). \\[ f() = \\begin{cases} \\frac{- 14}{15 \\cdot 16} & \\text{} \\\\{15, \\dots, 29\\} \\\\ \\frac{45 - }{15 \\cdot 16} & \\text{} \\\\{30, \\dots, 44\\} \\\\ \\end{cases} \\]","code":"base_heterocp |> distinct(id, .keep_all = TRUE) |> mutate(event_age = cyear - byear) |> summarize(n = n(), .by = event_age) |> mutate(density = n / sum(n)) |> ggplot(aes(x = event_age, y = density)) + geom_col() + labs(x = \"Age at First Childbirth\", y = NULL) + theme_minimal() + theme(panel.grid.minor = element_blank(), panel.grid.major.x = element_blank())"},{"path":"http://kazuyanagimoto.com/unitdid/articles/base_heterocp/index.html","id":"generate-individual-fe-alpha_i-and-time-fe-lambda_t","dir":"Articles > Base_heterocp","previous_headings":"Data Generation Process","what":"2. Generate Individual FE \\(\\alpha_i\\) and Time FE \\(\\lambda_t\\)","title":"Data Generation Process of base_heterocp","text":"Generate individual fixed effects \\(\\alpha_i\\) time fixed effects \\(\\lambda_t\\) normal distributions. \\[ \\alpha_{} \\sim \\mathcal{N}(0.15, 0.1),\\, \\lambda_{t} \\sim \\mathcal{N}(0, 0.1) \\]","code":""},{"path":"http://kazuyanagimoto.com/unitdid/articles/base_heterocp/index.html","id":"generate-heterogeneous-treatment-effect-tau_i-k","dir":"Articles > Base_heterocp","previous_headings":"Data Generation Process","what":"3. Generate Heterogeneous Treatment Effect \\(\\tau_{i, k}\\)","title":"Data Generation Process of base_heterocp","text":"individual \\(\\) age first childbirth \\(a_i\\), draw \\(\\tau_{, k} \\sim \\mathcal{N}(\\mu(a_i, k), \\sigma(a_i, k))\\) \\[ \\begin{aligned} \\mu() &= -1.2 + 0.3\\log() \\\\ \\sigma() &= 2.8 - 0.8\\log() \\\\ \\mu(, k) &= \\begin{cases} \\frac{1}{3}\\mu() & \\text{} k = 0 \\\\ \\mu() & \\text{} k = 1 \\\\ 0.05 + 1.5\\mu() - 0.01k & \\text{} k \\ge 2 \\\\ \\end{cases} \\\\ \\sigma(, k) &= \\begin{cases} \\frac{1}{3}\\sigma() & \\text{} k = 0 \\\\ \\sigma() & \\text{} k = 1 \\\\ 0.01 + \\sigma() + 0.01k & \\text{} k \\ge 2 \\\\ \\end{cases} \\end{aligned} \\]","code":""},{"path":"http://kazuyanagimoto.com/unitdid/articles/base_heterocp/index.html","id":"generate-outcome-y_it","dir":"Articles > Base_heterocp","previous_headings":"Data Generation Process","what":"4. Generate Outcome \\(y_{it}\\)","title":"Data Generation Process of base_heterocp","text":"\\[ y_{} = \\alpha_i + \\lambda_t + \\tau_{, k} + \\varepsilon_{} \\] \\(\\varepsilon_{} \\sim \\mathcal{N}(0, 025)\\).","code":""},{"path":"http://kazuyanagimoto.com/unitdid/articles/base_heterocp/index.html","id":"simulation","dir":"Articles > Base_heterocp","previous_headings":"","what":"Simulation","title":"Data Generation Process of base_heterocp","text":"see estimation properties, simulate data set 100 times compare true values mean, 2.5% quantile, 97.5% quantile estimated values.","code":"set.seed(1234) n_sim <- 100 sim <- function() { mdl_base <- gen_heterocp(size_cohort = 1000) |> unitdid(yname = \"y\", iname = \"id\", tname = \"year\", ename = \"cyear\", bname = \"byear\", compute_varcov = \"var\") summary(mdl_base, agg = \"event_age\") } path_sim <- here::here(\"vignettes/base_heterocp/simulated.rds\") if (!file.exists(path_sim)) { simulated <- map(1:n_sim, ~sim(), .progress = TRUE) |> list_rbind() save(simulated, file = path_sim) } else { load(path_sim) } val_true <- tibble( event_age = 25:34, tau1 = -1.2 + 0.3 * log(event_age), sd1 = 3 - 0.75 * log(event_age)) |> slice(rep(1:n(), each = 6)) |> mutate(rel_time = rep(0:5, 10), mean = case_when( rel_time == 0 ~ tau1 / 3, rel_time == 1 ~ tau1, rel_time > 1 ~ 0.05 + 1.5 * tau1 - 0.02 * rel_time), sd = case_when( rel_time == 0 ~ sd1 / 3, rel_time == 1 ~ sd1, rel_time > 1 ~ 0.01 + sd1 + 0.02 * rel_time), lbl = \"Truth\") |> select(-c(tau1, sd1)) df_plot <- simulated |> mutate(sd = sqrt(var)) |> summarize(across(c(mean, sd), list(\"lci\" = ~quantile(.x, 0.025), \"rci\" = ~quantile(.x, 0.975))), across(c(mean, sd), mean), .by = c(event_age, rel_time)) |> mutate(lbl = \"Simulation\") |> bind_rows(val_true) |> mutate(lbl_facet = paste0(\"Age \", event_age)) |> filter(between(event_age, 25, 34))"},{"path":"http://kazuyanagimoto.com/unitdid/articles/base_heterocp/index.html","id":"mean-of-tau_ik","dir":"Articles > Base_heterocp","previous_headings":"Simulation","what":"Mean of \\(\\tau_{ik}\\)","title":"Data Generation Process of base_heterocp","text":"","code":"df_plot |> ggplot(aes(x = rel_time, y = mean, ymin = mean_lci, ymax = mean_rci, color = lbl, shape = lbl)) + geom_point(position = position_dodge(width = 0.3)) + geom_errorbar(width = 0.2, position = position_dodge(width = 0.3)) + geom_vline(xintercept = -1, linetype = \"dashed\") + geom_hline(yintercept = 0) + scale_color_manual(values = c(\"#009F8C\", \"#B75C9D\")) + facet_wrap(~lbl_facet, ncol = 5) + labs(x = \"Time to Treatment\", y = NULL, color = NULL, shape = NULL) + theme_minimal() + theme(panel.grid.minor = element_blank(), panel.grid.major.x = element_blank(), legend.position = \"bottom\")"},{"path":"http://kazuyanagimoto.com/unitdid/articles/base_heterocp/index.html","id":"standard-deviation-of-tau_ik","dir":"Articles > Base_heterocp","previous_headings":"Simulation","what":"Standard Deviation of \\(\\tau_{ik}\\)","title":"Data Generation Process of base_heterocp","text":"","code":"df_plot |> ggplot(aes(x = rel_time, y = sd, ymin = sd_lci, ymax = sd_rci, color = lbl, shape = lbl)) + geom_point(position = position_dodge(width = 0.3)) + geom_errorbar(width = 0.2, position = position_dodge(width = 0.3)) + geom_vline(xintercept = -1, linetype = \"dashed\") + geom_hline(yintercept = 0) + scale_color_manual(values = c(\"#009F8C\", \"#B75C9D\")) + facet_wrap(~lbl_facet, ncol = 5) + labs(x = \"Time to Treatment\", y = NULL, color = NULL, shape = NULL) + theme_minimal() + theme(panel.grid.minor = element_blank(), panel.grid.major.x = element_blank(), legend.position = \"bottom\")"},{"path":"http://kazuyanagimoto.com/unitdid/articles/example_psid/index.html","id":"panel-study-of-income-dynamics-psid","dir":"Articles > Example_psid","previous_headings":"","what":"Panel Study of Income Dynamics (PSID)","title":"Individual-level Child Penalties with PSID","text":"PSID data longitudinal household survey United States one longest famous panel surveys world. study began 1968 nationally representative sample 18,000 individuals living 5,000 families. data available registered users. avoid convoluted cleaning tasks, mostly use cleaning codes provided Comparative Panel File (CPF). cleaning codes developed creating harmonized panel data seven countries (Australia, Germany, UK, South Korea, Russia, Switzerland, US). replicate article, need run step 4 (Add vars constant across waves) CPF-Code/03_PSID/us_01_3_GEt_vars., add ER32024 (birth year first child) us_01.dta-us_03.dta produced Run following code create dataset child penalties","code":"library(haven) cpf_us01 <- read_dta(here(\"vignettes/example_psid/us_01.dta\")) cpf_us03 <- read_dta(here(\"vignettes/example_psid/us_03_CPF.dta\")) ever_birth <- cpf_us01 |> select(pid, cyear = ER32024) |> filter(cyear != 9999) |> distinct() cp_psid <- cpf_us03 |> filter(country == 3) |> select(id = pid, year = intyear, byear = yborn, gender = female, earn = incjobs_yg, hours = whweek, years_edu = eduy) |> mutate(gender = if_else(gender == 0, \"Men\", \"Women\"), particip = if_else(hours > 0, 1, 0), wage = if_else(particip == 1, earn / hours, 0), years_edu = if_else(years_edu < 0, NA_integer_, years_edu)) |> right_join(ever_birth, by = c(\"id\" = \"pid\")) |> mutate(rel_time = year - cyear) |> filter(!is.na(earn)) cpf_us01 |> glimpse() save(cp_psid, file = here(\"vignettes/example_psid/cp_psid.rds\"))"},{"path":"http://kazuyanagimoto.com/unitdid/articles/example_psid/index.html","id":"analysis-of-child-penalties-on-participation","dir":"Articles > Example_psid","previous_headings":"","what":"Analysis of Child Penalties on Participation","title":"Individual-level Child Penalties with PSID","text":"","code":"load(here(\"vignettes/example_psid/cp_psid.rds\")) cp_psid <- cp_psid |> mutate(eage = cyear - byear) |> filter(cyear >= 1968, year <= 1997, # The last year of the annual data between(eage, 22, 40)) # The Age range of the first childbirth mdl_particip <- cp_psid |> unitdid(yname = \"particip\", iname = \"id\", tname = \"year\", ename = \"cyear\", bname = \"byear\", by = c(\"gender\"), compute_varcov = \"var\")"},{"path":"http://kazuyanagimoto.com/unitdid/articles/example_psid/index.html","id":"mean-of-child-penalties-on-participation","dir":"Articles > Example_psid","previous_headings":"Analysis of Child Penalties on Participation","what":"Mean of Child Penalties on Participation","title":"Individual-level Child Penalties with PSID","text":"","code":"sum_particip <- summary(mdl_particip, agg = \"event_age\", na.rm = TRUE) |> filter(between(event_age, 26, 32)) df_plot <- sum_particip |> filter(rel_time == 0) |> mutate(rel_time = -1, mean = 0, var = 0) |> bind_rows(sum_particip) |> mutate(lbl_facet = paste0(\"Age \", event_age)) df_plot |> ggplot(aes(x = rel_time, y = mean, color = gender, shape = gender)) + geom_point() + geom_line() + geom_vline(xintercept = -1, linetype = \"dashed\") + geom_hline(yintercept = 0) + facet_wrap(~lbl_facet) + scale_color_manual(values = c(\"#009F8C\", \"#B75C9D\")) + labs(x = \"Time to First Childbirth\", y = NULL, color = NULL, shape = NULL) + theme_minimal() + theme(panel.grid.minor = element_blank(), panel.grid.major.x = element_blank(), legend.position = \"bottom\")"},{"path":"http://kazuyanagimoto.com/unitdid/articles/example_psid/index.html","id":"standard-deviation-of-child-penalties-on-participation","dir":"Articles > Example_psid","previous_headings":"Analysis of Child Penalties on Participation","what":"Standard Deviation of Child Penalties on Participation","title":"Individual-level Child Penalties with PSID","text":"","code":"df_plot |> ggplot(aes(x = rel_time, y = sqrt(var), color = gender, shape = gender)) + geom_point() + geom_line() + geom_vline(xintercept = -1, linetype = \"dashed\") + geom_hline(yintercept = 0) + facet_wrap(~lbl_facet) + scale_color_manual(values = c(\"#009F8C\", \"#B75C9D\")) + labs(x = \"Time to First Childbirth\", y = NULL, color = NULL, shape = NULL) + theme_minimal() + theme(panel.grid.minor = element_blank(), panel.grid.major.x = element_blank(), legend.position = \"bottom\")"},{"path":"http://kazuyanagimoto.com/unitdid/articles/example_psid/index.html","id":"scatter-plot-with-covariates","dir":"Articles > Example_psid","previous_headings":"","what":"Scatter Plot with Covariates","title":"Individual-level Child Penalties with PSID","text":"highlight flexibility individual child penalties, let’s plot relationship years education child penalties.","code":"library(patchwork) unitdid_particip <- get_unitdid(mdl_particip) plot_scatter <- function(gender) { clr <- ifelse(gender == \"Men\", \"#009F8C\", \"#B75C9D\") unitdid_particip |> filter(gender == {{gender}}, between(rel_time, 0, 5)) |> mutate(lbl_facet = paste0(\"k = \", rel_time)) |> ggplot(aes(x = years_edu, y = particip_tilde)) + geom_point(color = clr, alpha = 0.1) + geom_smooth(method = \"lm\", color = clr, fill = clr) + facet_wrap(~lbl_facet, nrow = 1) + labs(x = \"Years of Education\", y = \"CP on Participation\", title = gender) + coord_cartesian(xlim = c(6, NA), ylim = c(-1.25, 0.5)) + theme_minimal() } p1 <- plot_scatter(\"Men\") p2 <- plot_scatter(\"Women\") p1 / p2"},{"path":"http://kazuyanagimoto.com/unitdid/articles/example_psid/index.html","id":"on-binscatter","dir":"Articles > Example_psid","previous_headings":"Scatter Plot with Covariates","what":"On Binscatter","title":"Individual-level Child Penalties with PSID","text":"Another way visualize relationship use binscatter (Cattaneo et al. 2023).","code":"library(binsreg) compute_binsreg <- function(gender, rel_time) { est <- unitdid_particip |> filter(gender == {{ gender }}, rel_time == {{ rel_time }}) |> as.data.frame() |> binsreg(x = years_edu, y = particip_tilde, data = _) est$data.plot$`Group Full Sample`$data.dots |> mutate(gender = gender, rel_time = rel_time) } bs <- map2(rep(c(\"Men\", \"Women\"), times = 6), rep(0:5, each = 2), ~compute_binsreg(.x, .y)) |> list_rbind() plot_binsreg <- function(gender) { clr <- ifelse(gender == \"Men\", \"#009F8C\", \"#B75C9D\") bs |> filter(gender == {{ gender }}) |> mutate(lbl_facet = paste0(\"k = \", rel_time)) |> ggplot(aes(x = x, y = fit)) + geom_point(color = clr) + facet_wrap(~lbl_facet, nrow = 1) + labs(x = \"Years of Education\", y = \"CP on Participation\", title = gender) + theme_minimal() } p1 <- plot_binsreg(\"Men\") p2 <- plot_binsreg(\"Women\") p1 / p2"},{"path":[]},{"path":"http://kazuyanagimoto.com/unitdid/authors.html","id":null,"dir":"","previous_headings":"","what":"Authors","title":"Authors and Citation","text":"Kazuharu Yanagimoto. Author, maintainer.","code":""},{"path":"http://kazuyanagimoto.com/unitdid/authors.html","id":"citation","dir":"","previous_headings":"","what":"Citation","title":"Authors and Citation","text":"Yanagimoto K (2024). unitdid: Unit-level Difference--Difference Estimator. R package version 0.0.5, http://kazuyanagimoto.com/unitdid/.","code":"@Manual{, title = {unitdid: Unit-level Difference-in-Difference Estimator}, author = {Kazuharu Yanagimoto}, year = {2024}, note = {R package version 0.0.5}, url = {http://kazuyanagimoto.com/unitdid/}, }"},{"path":"http://kazuyanagimoto.com/unitdid/index.html","id":"unitdid","dir":"","previous_headings":"","what":"Unit-level Difference-in-Difference Estimator","title":"Unit-level Difference-in-Difference Estimator","text":"unitdid package provides set functions analysis unit-level difference--differences (Arkhangelsky, Yanagimoto, Zohar, 2024)","code":""},{"path":"http://kazuyanagimoto.com/unitdid/index.html","id":"installation","dir":"","previous_headings":"","what":"Installation","title":"Unit-level Difference-in-Difference Estimator","text":"can install development version unitdid GitHub :","code":"# install.packages(\"remotes\") remotes::install_github(\"kazuyanagimoto/unitdid\")"},{"path":"http://kazuyanagimoto.com/unitdid/index.html","id":"example","dir":"","previous_headings":"","what":"Example","title":"Unit-level Difference-in-Difference Estimator","text":"basic example simulated base_heterocp data set: Individual-level child penalties estimated unitdid():","code":"library(unitdid) library(dplyr) library(ggplot2) base_heterocp |> head() #> # A tibble: 6 × 5 #> id year byear cyear y #> #> 1 1 1999 1955 1985 -0.848 #> 2 1 2000 1955 1985 0.759 #> 3 1 2001 1955 1985 -1.03 #> 4 1 2002 1955 1985 0.858 #> 5 1 2003 1955 1985 -0.866 #> 6 1 2004 1955 1985 -0.651 mdl_base <- base_heterocp |> unitdid(yname = \"y\", iname = \"id\", tname = \"year\", ename = \"cyear\", bname = \"byear\") # Estimated individual-level child penalties (y_tilde) get_unitdid(mdl_base) #> # A tibble: 32,257 × 6 #> id year byear cyear y y_tilde #> #> 1 705 2000 1957 2000 0.138 -0.0287 #> 2 997 2000 1958 2000 0.138 0.0849 #> 3 998 2000 1958 2000 0.119 -0.104 #> 4 1013 2000 1958 2000 0.115 -0.0000709 #> 5 1082 2000 1958 2000 0.0362 0.00549 #> 6 1127 2000 1958 2000 0.386 0.125 #> 7 1225 2001 1959 2001 0.158 -0.118 #> 8 1228 2000 1959 2000 0.241 -0.0937 #> 9 1228 2001 1959 2000 0.443 0.0226 #> 10 1230 2000 1959 2000 0.143 -0.0266 #> # ℹ 32,247 more rows"},{"path":"http://kazuyanagimoto.com/unitdid/index.html","id":"aggregation-of-individual-level-child-penalties","dir":"","previous_headings":"Example","what":"Aggregation of Individual-level Child Penalties","title":"Unit-level Difference-in-Difference Estimator","text":"can aggregated full, event (year event (treatment). Mainly staggered design), event_age (age event. Mainly child penalties) levels:","code":"summary(mdl_base) # default agg = \"full\" #> # A tibble: 6 × 3 #> rel_time mean n #> #> 1 0 -0.0653 4357 #> 2 1 -0.193 4357 #> 3 2 -0.307 4357 #> 4 3 -0.310 4357 #> 5 4 -0.350 4357 #> 6 5 -0.349 4357 sum_eage <- summary(mdl_base, agg = \"event_age\") sum_eage |> filter(rel_time == 0) |> mutate(rel_time = -1, mean = 0) |> bind_rows(sum_eage) |> filter(between(event_age, 25, 34)) |> mutate(lbl_facet = paste0(\"Age \", event_age)) |> ggplot(aes(x = rel_time, y = mean)) + geom_point() + geom_line() + geom_vline(xintercept = -1, linetype = \"dashed\") + geom_hline(yintercept = 0) + facet_wrap(~lbl_facet, ncol = 5) + labs(x = \"Time to First Childbirth\", y = \"Child Penalties on y\") + theme_minimal() + theme(panel.grid.major.x = element_blank(), panel.grid.minor = element_blank())"},{"path":"http://kazuyanagimoto.com/unitdid/index.html","id":"variance-of-individual-level-child-penalties","dir":"","previous_headings":"Example","what":"Variance of Individual-level Child Penalties","title":"Unit-level Difference-in-Difference Estimator","text":"Since individual-level child penalties estimated measurement errors, variance y_tilde equal variance individual-level child penalties. compute_var option unitdid estimates variance measurement errors variance individual-level child penalties subtracting variance measurement errors variance y_tilde","code":"mdl_base <- base_heterocp |> unitdid(yname = \"y\", iname = \"id\", tname = \"year\", ename = \"cyear\", bname = \"byear\", compute_var = TRUE) sum_eage <- summary(mdl_base, agg = \"event_age\") sum_eage |> filter(rel_time == 0) |> mutate(rel_time = -1, var = 0) |> bind_rows(sum_eage) |> filter(between(event_age, 25, 34)) |> mutate(lbl_facet = paste0(\"Age \", event_age)) |> ggplot(aes(x = rel_time, y = sqrt(var))) + geom_point() + geom_line() + geom_vline(xintercept = -1, linetype = \"dashed\") + geom_hline(yintercept = 0) + facet_wrap(~lbl_facet, ncol = 5) + labs(x = \"Time to First Childbirth\", y = \"S.D. of Child Penalties\") + theme_minimal() + theme(panel.grid.major.x = element_blank(), panel.grid.minor = element_blank())"},{"path":"http://kazuyanagimoto.com/unitdid/reference/aggregate_unitdid.html","id":null,"dir":"Reference","previous_headings":"","what":"Aggregate the mean and variance of the estimated unit-level DiD effects — aggregate_unitdid","title":"Aggregate the mean and variance of the estimated unit-level DiD effects — aggregate_unitdid","text":"Aggregate mean variance estimated unit-level effects","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/aggregate_unitdid.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Aggregate the mean and variance of the estimated unit-level DiD effects — aggregate_unitdid","text":"","code":"aggregate_unitdid( object, agg = \"full\", na.rm = TRUE, by = NULL, normalized = NULL, allow_negative_var = FALSE, only_full_horizon = TRUE )"},{"path":"http://kazuyanagimoto.com/unitdid/reference/aggregate_unitdid.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Aggregate the mean and variance of the estimated unit-level DiD effects — aggregate_unitdid","text":"object unitdid object agg Aggregation method. One c(\"full\", \"event\", \"event_age\") default full. provided model, options separately aggregate group. event option aggregates group event timing. event_age option aggregates group age event time. event_age requires bname provided model. na.rm Logical. TRUE, remove NA values aggregation. default TRUE. character vector variables aggregate separately . Default inherited unitdid object can override . can estimate unit-level effects separately unitdid can also aggregate estimates (higher-level) . can use \"rel_time\" highest level aggregation. normalized Logical. TRUE, function normalize aggregated mean variance mean imputed outcome variable. Default inherited unitdid object. allow_negative_var Logical. FALSE, function return estimated variance trimmed zero. Default FALSE. only_full_horizon Logical. TRUE, aggregate unit-level treatment effect, event year (ename) full horizon (k_min:k_max) included. recommended case want change composition event year (age child penalties) estimated point k_min:k_max. Default TRUE.","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/aggregate_unitdid.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Aggregate the mean and variance of the estimated unit-level DiD effects — aggregate_unitdid","text":"tibble aggregated mean variance estimated unit-level effects","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/base_heterocp.html","id":null,"dir":"Reference","previous_headings":"","what":"Simulated Individual Child Panalty Data — base_heterocp","title":"Simulated Individual Child Panalty Data — base_heterocp","text":"Simulated Individual Child Panalty Data","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/base_heterocp.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Simulated Individual Child Panalty Data — base_heterocp","text":"","code":"base_heterocp"},{"path":[]},{"path":"http://kazuyanagimoto.com/unitdid/reference/base_heterocp.html","id":"base-heterocp","dir":"Reference","previous_headings":"","what":"base_heterocp","title":"Simulated Individual Child Panalty Data — base_heterocp","text":"dataframe 1000 individuals birth year 1965 1984: id Individual identifier year Year observation byear Birth year cage Age first birth rel_time Relative time first birth y Outcome variable","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/base_heterocp.html","id":"source","dir":"Reference","previous_headings":"","what":"Source","title":"Simulated Individual Child Panalty Data — base_heterocp","text":"Generated gen_heterocp() seed 1234","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/gen_heterocp.html","id":null,"dir":"Reference","previous_headings":"","what":"Generate Sample Heterogenous Child Penalty Data — gen_heterocp","title":"Generate Sample Heterogenous Child Penalty Data — gen_heterocp","text":"Generate Sample Heterogenous Child Penalty Data","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/gen_heterocp.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Generate Sample Heterogenous Child Penalty Data — gen_heterocp","text":"","code":"gen_heterocp(size_cohort = 300)"},{"path":"http://kazuyanagimoto.com/unitdid/reference/gen_heterocp.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Generate Sample Heterogenous Child Penalty Data — gen_heterocp","text":"size_cohort n_obsumber individuals per birth year","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/gen_heterocp.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Generate Sample Heterogenous Child Penalty Data — gen_heterocp","text":"sample dataframe heterogenous child penalty age first birth","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/gen_heterocp.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Generate Sample Heterogenous Child Penalty Data — gen_heterocp","text":"","code":"set.seed(1234) base_heterocp <- gen_heterocp()"},{"path":"http://kazuyanagimoto.com/unitdid/reference/get_unitdid.html","id":null,"dir":"Reference","previous_headings":"","what":"Get unit-level Difference-in-Differences estimates — get_unitdid","title":"Get unit-level Difference-in-Differences estimates — get_unitdid","text":"Get unit-level Difference--Differences estimates","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/get_unitdid.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Get unit-level Difference-in-Differences estimates — get_unitdid","text":"","code":"get_unitdid( object, normalized = NULL, export = TRUE, only_full_horizon = FALSE )"},{"path":"http://kazuyanagimoto.com/unitdid/reference/get_unitdid.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Get unit-level Difference-in-Differences estimates — get_unitdid","text":"object unitdid object normalized Logical. TRUE, function normalize mean imputed outcome variable. Default inherited unitdid object. export Logical. TRUE, function export columns zz000 prefix, used internal computation. only_full_horizon Logical. TRUE, event year (ename) full horizon (k_min:k_max) exported. recommended case want change composition event year (age child penalties) estimated point k_min:k_max aggregation. Default FALSE.","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/get_unitdid.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Get unit-level Difference-in-Differences estimates — get_unitdid","text":"dataframe new column unit-level estimates","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/pipe.html","id":null,"dir":"Reference","previous_headings":"","what":"Pipe operator — %>%","title":"Pipe operator — %>%","text":"See magrittr::%>% details.","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/pipe.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Pipe operator — %>%","text":"","code":"lhs %>% rhs"},{"path":"http://kazuyanagimoto.com/unitdid/reference/pipe.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Pipe operator — %>%","text":"lhs value magrittr placeholder. rhs function call using magrittr semantics.","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/pipe.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Pipe operator — %>%","text":"result calling rhs(lhs).","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/summary.unitdid.html","id":null,"dir":"Reference","previous_headings":"","what":"Aggregate the mean and variance of the estimated unit-level DiD effects — summary.unitdid","title":"Aggregate the mean and variance of the estimated unit-level DiD effects — summary.unitdid","text":"Aggregate mean variance estimated unit-level effects","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/summary.unitdid.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Aggregate the mean and variance of the estimated unit-level DiD effects — summary.unitdid","text":"","code":"# S3 method for unitdid summary(object, ...)"},{"path":"http://kazuyanagimoto.com/unitdid/reference/summary.unitdid.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Aggregate the mean and variance of the estimated unit-level DiD effects — summary.unitdid","text":"object unitdid object ... aggregate_unitdid arguments","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/summary.unitdid.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Aggregate the mean and variance of the estimated unit-level DiD effects — summary.unitdid","text":"tibble summary statistics","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/summary.unitdid.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Aggregate the mean and variance of the estimated unit-level DiD effects — summary.unitdid","text":"","code":"library(unitdid) mdl_base <- base_heterocp |> unitdid(yname = \"y\", iname = \"id\", tname = \"year\", ename = \"cyear\", bname = \"byear\") summary(mdl_base, agg = \"event_age\") #> # A tibble: 144 × 4 #> mean rel_time n event_age #> #> 1 -0.0344 0 13 15 #> 2 -0.0860 0 42 16 #> 3 -0.170 0 59 17 #> 4 -0.141 0 79 18 #> 5 -0.0883 0 99 19 #> 6 -0.0689 0 107 20 #> 7 -0.115 0 158 21 #> 8 -0.0684 0 165 22 #> 9 -0.0778 0 168 23 #> 10 -0.0567 0 206 24 #> # ℹ 134 more rows"},{"path":"http://kazuyanagimoto.com/unitdid/reference/unitdid.html","id":null,"dir":"Reference","previous_headings":"","what":"A function estimates unit-level difference-in-differences — unitdid","title":"A function estimates unit-level difference-in-differences — unitdid","text":"function estimates unit-level difference--differences","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/unitdid.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"A function estimates unit-level difference-in-differences — unitdid","text":"","code":"unitdid( data, yname, iname, tname, ename, first_stage = NULL, wname = NULL, ytildename = NULL, yvarname = NULL, ycovname = NULL, kprimename = \"kprime\", k_min = 0, k_max = 5, compute_varcov = \"none\", by = NULL, bname = NULL, normalized = FALSE )"},{"path":"http://kazuyanagimoto.com/unitdid/reference/unitdid.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"A function estimates unit-level difference-in-differences — unitdid","text":"data dataframe containing variables yname Outcome variable iname Unit identifier tname Time variable ename Event timing variable first_stage Formula Y(0). Formula follows fixest::feols. specified, unit (iname) time (tname) fixed effects used. wname Optional. name weight variable. ytildename Optional. name imputed outcome variable. provided, function use paste0(yname, \"_tilde\"). yvarname Optional. name unit-level variance outcome variable. provided, function use paste0(yname, \"_var\"). ycovname Optional. name unit-level covariance outcome variable. provided, function use paste0(yname, \"_cov\"). kprimename Optional. name relative time treatment. used second column name relative time unit-level covariance estimation. Default \"kprime\". k_min Relative time treatment treatment starts. Default 0. k_max Relative time treatment treatment ends. Default 5. compute_varcov One c(\"none\", \"var\", \"cov\") Default \"none\". \"var\", function estimate unit-level variance outcome variable. \"cov\", function estimate unit-level covariance outcome variable pair within k_min:k_max. character vector variables estimate separately . Default NULL. bname Birth year variable. Default NULL. Necessary aggregate estimates age event. normalized Logical. TRUE, function normalize outcome variable scale. Default FALSE.","code":""},{"path":"http://kazuyanagimoto.com/unitdid/reference/unitdid.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"A function estimates unit-level difference-in-differences — unitdid","text":"unitdid class object.","code":""},{"path":[]},{"path":"http://kazuyanagimoto.com/unitdid/news/index.html","id":"new-features-0-0-5","dir":"Changelog","previous_headings":"","what":"New Features","title":"unitdid 0.0.5","text":"Support unit-level covariance estimation, allows flexible aggregation using covariance matrix Add only_full_horizon option get_unitdid()","code":""},{"path":[]},{"path":"http://kazuyanagimoto.com/unitdid/news/index.html","id":"new-features-0-0-4","dir":"Changelog","previous_headings":"","what":"New Features","title":"unitdid 0.0.4","text":"Support unit-level variance estimation, allows flexible aggregation variance estimation Add allow_negative_var option allow negative variance estimation, used automatically trimmed 0 (v0.0.4.1).","code":""},{"path":"http://kazuyanagimoto.com/unitdid/news/index.html","id":"bug-fixes-0-0-4","dir":"Changelog","previous_headings":"","what":"Bug Fixes","title":"unitdid 0.0.4","text":"Measurement error (zz000varcont code) esimated weights (#11)","code":""},{"path":[]},{"path":"http://kazuyanagimoto.com/unitdid/news/index.html","id":"new-features-0-0-3","dir":"Changelog","previous_headings":"","what":"New Features","title":"unitdid 0.0.3","text":"Support first_stage argument parametric first stage estimation Support argument aggregation","code":""},{"path":"http://kazuyanagimoto.com/unitdid/news/index.html","id":"bug-fixes-0-0-3","dir":"Changelog","previous_headings":"","what":"Bug Fixes","title":"unitdid 0.0.3","text":"Bug data[[yname]] includes NA values Bug variance estimation includes NA values (v0.0.3.1) Bug higher category (v0.0.3.2)","code":""},{"path":[]},{"path":"http://kazuyanagimoto.com/unitdid/news/index.html","id":"new-features-0-0-2","dir":"Changelog","previous_headings":"","what":"New Features","title":"unitdid 0.0.2","text":"Support wname argument weighted estimation","code":""},{"path":"http://kazuyanagimoto.com/unitdid/news/index.html","id":"bug-fixes-0-0-2","dir":"Changelog","previous_headings":"","what":"Bug Fixes","title":"unitdid 0.0.2","text":"Normalization summary normalized = FALSE model Bug data[[tname]] includes NA values","code":""},{"path":"http://kazuyanagimoto.com/unitdid/news/index.html","id":"unitdid-001","dir":"Changelog","previous_headings":"","what":"unitdid 0.0.1","title":"unitdid 0.0.1","text":"Initial GitHub submission","code":""}]
index.Rmd
Yanagimoto K (2024). unitdid: Unit-level Difference-in-Difference Estimator. -R package version 0.0.4.1, http://kazuyanagimoto.com/unitdid/. +R package version 0.0.5, http://kazuyanagimoto.com/unitdid/.
@Manual{, title = {unitdid: Unit-level Difference-in-Difference Estimator}, author = {Kazuharu Yanagimoto}, year = {2024}, - note = {R package version 0.0.4.1}, + note = {R package version 0.0.5}, url = {http://kazuyanagimoto.com/unitdid/}, }
only_full_horizon
get_unitdid()
get_unitdid(object, normalized = NULL, export = TRUE)
get_unitdid( + object, + normalized = NULL, + export = TRUE, + only_full_horizon = FALSE +)
Optional. The name of the unit-level covariance of +the outcome variable. If not provided, the function will use +paste0(yname, "_cov").
paste0(yname, "_cov")
Optional. The name of the relative time to treatment. +This is used for the second column name of the relative time of +the unit-level covariance estimation. Default is "kprime".
Relative time to treatment at which treatment starts. Default is 0.