Learn R Programming

BMisc

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).

Installation

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")

Example 1: Working with distribution functions

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)

Example 2: Working with panel data

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

Example 3: Staggered treatment adoption

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     3
#> 5   2     3
#> 9   3     2
#> 13  4     0
#> 17  5     3
#> 21  6     2
check_staggered(dta, idname = "id", treatname = "treat")
#> [1] FALSE

Example 4: Formula utilities

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: 0x5823b0801128>
toformula("y", c("x1", "x2", "x3"))
#> y ~ x1 + x2 + x3
#> <environment: 0x5823b0847790>

Example 5: Working with lists

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

Copy Link

Version

Install

install.packages('BMisc')

Monthly Downloads

11,978

Version

1.4.9

License

GPL-3

Maintainer

Brantly Callaway

Last Published

June 11th, 2026

Functions in BMisc (1.4.9)

rhs

Right-hand Side of Formula
panel2cs2

Panel Data to Repeated Cross Sections
makeDist

makeDist
weighted_ecdf

Weighted Distribution Function
weighted_combine_list

weighted_combine_list
t2orig

t2orig
lhs_vars

Left-hand Side Variables
rhs_vars

Right-hand Side Variables
mv_mult

Matrix-Vector Multiplication
weighted_checkfun

Weighted Check Function
get_Yit

get_Yit
ids2rownum

Convert Vector of ids into Vector of Row Numbers
orig2t

orig2t
multiplier_bootstrap

multiplier_bootstrap
panel2cs

Panel Data to Repeated Cross Sections
source_all

source_all
get_list_element

Return Particular Element from Each Element in a List
t2orig_inner

t2orig_inner
subsample

Subsample of Observations from Panel Data
makeBalancedPanel

makeBalancedPanel
get_Yibar_pre_inner

get_Yibar_pre_inner
invertEcdf

invertEcdf
weighted.checkfun

weighted.checkfun
rhs.vars

rhs.vars
orig2t_inner

orig2t_inner
weighted_mean

Weighted Mean
get_lagYi

get_lagYi
weighted_quantile

weighted_quantile
make_balanced_panel

Balance a Panel Data Set
time_invariant_to_panel

time_invariant_to_panel
weighted_quantile_inner

Quantile of a Weighted Check Function
toformula

Variable Names to Formula
checkfun

Check Function
add_cov_to_formula

Add a Covariate to a Formula
blockBootSample

blockBootSample
block_boot_sample

Block Bootstrap
check_staggered

check_staggered
TorF

TorF
drop_cov_from_formula

Drop a Covariate from a Formula
drop_collinear

drop_collinear
combine_ecdfs

Combine Two Distribution Functions
element_wise_mult

element_wise_mult
cs2panel

Cross Section to Panel
getWeightedMean

getWeightedMean
addCovToFormla

addCovToFormla
compareBinary

compareBinary
getWeightedDf

getWeightedDf
combineDfs

combineDfs
check_staggered_inner

check_staggered_inner
BMisc

BMisc
getListElement

getListElement
make_dist

Make a Distribution Function
get_Yi1_inner

get_Yi1_inner
compare_binary

Compare Variables across Groups
get_Yibar

get_Yibar
get_Yit_inner

get_Yit_inner
get_first_difference

get_first_difference
get_YiGmin1_inner

get_YiGmin1_inner
dropCovFromFormla

dropCovFromFormla
get_Yibar_pre

get_Yibar_pre
get_YiGmin1

get_YiGmin1
get_Yibar_inner

get_Yibar_inner
id2rownum

Take particular id and convert to row number
get_principal_components

get_principal_components
compare_binary_inner

Compare a single variable across two groups
getWeightedQuantiles

getWeightedQuantiles
get_group

get_group
get_Yi1

get_Yi1
invert_ecdf

Invert Ecdf
lhs.vars

lhs.vars
get_group_inner

get_group_inner