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

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

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

8,049

Version

1.4.10

License

GPL-3

Maintainer

Brantly Callaway

Last Published

August 22nd, 2026

Functions in BMisc (1.4.10)

get_list_element

Return Particular Element from Each Element in a List
get_group_inner

get_group_inner
get_lagYi

get_lagYi
get_first_difference

get_first_difference
invert_ecdf

Invert Ecdf
make_dist

Make a Distribution Function
get_Yit_inner

get_Yit_inner
makeDist

makeDist
id2rownum

Take particular id and convert to row number
ids2rownum

Convert Vector of ids into Vector of Row Numbers
makeBalancedPanel

makeBalancedPanel
lhs_vars

Left-hand Side Variables
make_balanced_panel

Balance a Panel Data Set
lhs.vars

lhs.vars
rhs_vars

Right-hand Side Variables
multiplier_bootstrap

multiplier_bootstrap
sort_panel

sort_panel
set_balanced_panel

set_balanced_panel
rhs

Right-hand Side of Formula
panel2cs2

Panel Data to Repeated Cross Sections
panel2cs

Panel Data to Repeated Cross Sections
orig2t

orig2t
time_invariant_to_panel

time_invariant_to_panel
orig2t_inner

orig2t_inner
mv_mult

Matrix-Vector Multiplication
rhs.vars

rhs.vars
invertEcdf

invertEcdf
weighted.checkfun

weighted.checkfun
subsample

Subsample of Observations from Panel Data
weighted_combine_list

weighted_combine_list
weighted_ecdf

Weighted Distribution Function
weighted_checkfun

Weighted Check Function
weighted_mean

Weighted Mean
t2orig

t2orig
source_all

source_all
weighted_quantile

weighted_quantile
toformula

Variable Names to Formula
t2orig_inner

t2orig_inner
weighted_quantile_inner

Quantile of a Weighted Check Function
TorF

TorF
add_cov_to_formula

Add a Covariate to a Formula
block_boot_sample

Block Bootstrap
checkfun

Check Function
check_staggered_inner

check_staggered_inner
check_staggered

check_staggered
compare_binary

Compare Variables across Groups
cs2panel

Cross Section to Panel
element_wise_mult

element_wise_mult
drop_collinear

drop_collinear
drop_cov_from_formula

Drop a Covariate from a Formula
combineDfs

combineDfs
blockBootSample

blockBootSample
dropCovFromFormla

dropCovFromFormla
getWeightedQuantiles

getWeightedQuantiles
addCovToFormla

addCovToFormla
BMisc

BMisc
getListElement

getListElement
get_group

get_group
combine_ecdfs

Combine Two Distribution Functions
get_Yi1

get_Yi1
get_Yibar_inner

get_Yibar_inner
getWeightedDf

getWeightedDf
compare_binary_inner

Compare a single variable across two groups
get_Yit

get_Yit
get_Yibar

get_Yibar
getWeightedMean

getWeightedMean
compareBinary

compareBinary
get_YiGmin1_inner

get_YiGmin1_inner
get_Yibar_pre_inner

get_Yibar_pre_inner
get_YiGmin1

get_YiGmin1
get_Yi1_inner

get_Yi1_inner
get_Yibar_pre

get_Yibar_pre
get_principal_components

get_principal_components