These are miscellaneous functions for working with panel data, quantiles, and printing results. For panel data, the package includes functions for making a panel data balanced (that is, dropping missing individuals that have missing observations in any time period), converting id numbers to row numbers, and to treat repeated cross sections as panel data under the assumption of rank invariance. For quantiles, there are functions to make distribution functions from a set of data points (this is particularly useful when a distribution function is created in several steps), to combine distribution functions based on some external weights, and to invert distribution functions. Finally, there are several other miscellaneous functions for obtaining weighted means, weighted distribution functions, and weighted quantiles; to generate summary statistics and their differences for two groups; and to add or drop covariates from formulas. Additional utilities support staggered treatment adoption settings, including functions for identifying treatment groups, recovering pre-treatment outcomes and covariate averages, and computing lagged outcomes and first differences in panel data.

BMisc includes miscellaneous functions useful for applied econometrics, with a focus on panel data and distributional analysis. Utilities cover balancing panels, working with distribution functions, computing weighted statistics, manipulating formulas, and supporting staggered treatment adoption settings (e.g., identifying treatment groups and recovering pre-treatment outcomes).
You can install BMisc from CRAN with:
install.packages("BMisc")
Or install the development version from GitHub with:
# install.packages("devtools")
devtools::install_github("bcallaway11/BMisc")
make_dist creates a distribution function from a vector of values and
their corresponding CDF values.
library(BMisc)
y <- rnorm(100)
y <- y[order(y)]
u <- runif(100)
u <- u[order(u)]
F <- make_dist(y, u)
class(F)
#> [1] "ecdf" "stepfun" "function"
# plot(F)
make_balanced_panel drops observations from a panel dataset that are
not available in all time periods.
id <- rep(seq(1, 100, 1), 2) ## individual ids for setting up a two period panel
t <- rep(seq(1, 2), 100) ## time periods
y <- rnorm(200) ## outcomes
dta <- data.frame(id = id, t = t, y = y) ## make into data frame
dta <- dta[-7, ] ## drop the 7th row from the dataset (which creates an unbalanced panel)
nrow(dta)
#> [1] 199
dta <- make_balanced_panel(dta, idname = "id", tname = "t")
nrow(dta) ## now all the observations with missing data in any period are dropped
#> [1] 198
get_group identifies the time period in which each unit first becomes
treated. check_staggered verifies that treatment is absorbing (no
de-treatments).
n <- 100
id <- rep(seq_len(n), each = 4)
t <- rep(1:4, n)
## assign units to groups: 0 = never treated, 2 = treated from period 2, 3 = from period 3
g <- rep(sample(c(0, 2, 3), n, replace = TRUE), each = 4)
treat <- as.integer(t >= g & g > 0)
dta <- data.frame(id = id, t = t, treat = treat)
dta$group <- get_group(dta, idname = "id", tname = "t", treatname = "treat")
head(unique(dta[, c("id", "group")]))
#> id group
#> 1 1 2
#> 5 2 3
#> 9 3 2
#> 13 4 0
#> 17 5 0
#> 21 6 2
check_staggered(dta, idname = "id", treatname = "treat")
#> [1] TRUE
rhs extracts the right-hand side of a formula. toformula builds a
formula from variable name strings — useful when constructing formulas
programmatically.
ff <- y ~ x1 + x2 + x3
rhs(ff)
#> ~x1 + x2 + x3
#> <environment: 0x651fca30df70>
toformula("y", c("x1", "x2", "x3"))
#> y ~ x1 + x2 + x3
#> <environment: 0x651fca396268>
get_list_element extracts the same element from every entry in a list
— a common pattern when results are stored as a list of lists.
results <- lapply(1:5, function(i) list(est = i * 1.5, se = i * 0.1))
get_list_element(results, "est")
#> [[1]]
#> [1] 1.5
#>
#> [[2]]
#> [1] 3
#>
#> [[3]]
#> [1] 4.5
#>
#> [[4]]
#> [1] 6
#>
#> [[5]]
#> [1] 7.5
get_list_element(results, "se")
#> [[1]]
#> [1] 0.1
#>
#> [[2]]
#> [1] 0.2
#>
#> [[3]]
#> [1] 0.3
#>
#> [[4]]
#> [1] 0.4
#>
#> [[5]]
#> [1] 0.5