Plot method for dbbinsreg objects

Description

Visualizes binned regression results from dbbinsreg. Plots dots at bin means with optional confidence intervals and/or confidence bands, and optionally overlays a smooth line if computed. Uses tinyplot for rendering but works with both plot() and tinyplot() generics.

Usage

## S3 method for class 'dbbinsreg'
plot(x, type = NULL, ci = TRUE, cb = TRUE, line = TRUE, ...)

## S3 method for class 'dbbinsreg'
tinyplot(x, type = NULL, ci = TRUE, cb = TRUE, line = TRUE, ...)

Arguments

x A dbbinsreg object
type The type of plot. If NULL (the default), then the type will be inferred based on the underlying object (e.g, “pointrange” for points with confidence intervals).
ci Logical. Show confidence intervals for dots? Default is TRUE.
cb Logical. Show confidence bands as a ribbon? Default is TRUE if available in the object.
line Logical. Show the line overlay if available? Default is TRUE.
Additional arguments passed to , e.g. theme, main, file’, etc.

Examples

library("dbreg")

#
## In-memory data ----

# Like `dbreg`, we can pass in-memory R data frames to an ephemeral DuckDB
# connection via the `data` argument. 

# Canonical binscatter: bin means (default)
dbbinsreg(weight ~ Time, data = ChickWeight, nbins = 10)

Binscatter Plot
Formula: weight ~ Time 
points = c(0,0) | line = NULL | nbins = 10 (quantile-spaced)
Observations: 578 (original) | 10 (compressed)
# For plot customization, save the model object so you can pass additional args
# to (tiny)plot.dbbinsreg
bs = dbbinsreg(weight ~ Time, data = ChickWeight, nbins = 10)
plot(bs, theme = "clean", main = "A simple binscatter example")

# Alternatively: you can also set a global (tiny)plot theme
tinyplot::tinytheme("classic")

# Piecewise linear (p = 1), no smoothness (s = 0)
dbbinsreg(weight ~ Time, data = ChickWeight, nbins = 10, points = c(1, 0))

Binscatter Plot
Formula: weight ~ Time 
points = c(1,0) | line = NULL | nbins = 10 (quantile-spaced)
Observations: 578 (original) | 21 (compressed)
# Piecewise linear (p = 1) with continuity (s = 1)
dbbinsreg(weight ~ Time, data = ChickWeight, nbins = 10, points = c(1, 1))
Binscatter Plot
Formula: weight ~ Time 
points = c(1,1) | line = NULL | nbins = 10 (quantile-spaced)
N = 578
# With line overlay for smooth visualization
dbbinsreg(weight ~ Time, data = ChickWeight, nbins = 10, points = c(1, 1), line = TRUE)

Binscatter Plot
Formula: weight ~ Time 
points = c(1,1) | line = c(1,1) | nbins = 10 (quantile-spaced)
N = 578
# Different line smoothness to points
dbbinsreg(weight ~ Time, data = ChickWeight, nbins = 10, points = c(0, 0), line = c(1, 1))

Binscatter Plot
Formula: weight ~ Time 
points = c(0,0) | line = c(1,1) | nbins = 10 (quantile-spaced)
Observations: 578 (original) | 10 (compressed)
# With uniform confidence bands (much greater uncertainty)
set.seed(99)
dbbinsreg(weight ~ Time, data = ChickWeight, nbins = 10, cb = TRUE)

Binscatter Plot
Formula: weight ~ Time 
points = c(0,0) | line = NULL | nbins = 10 (quantile-spaced)
Observations: 578 (original) | 10 (compressed)
# Accounting for Diet "fixed effects" helps to resolve the situation
dbbinsreg(weight ~ Time | Diet, data = ChickWeight, nbins = 10, cb = TRUE)

Binscatter Plot
Formula: weight ~ Time | Diet 
points = c(0,0) | line = NULL | nbins = 10 (quantile-spaced)
Observations: 578 (original) | 40 (compressed)
#
## DBI connection ----

library(DBI)
con = dbConnect(duckdb::duckdb())
dbWriteTable(con, "cw", as.data.frame(ChickWeight))

dbbinsreg(weight ~ Time | Diet, conn = con, table = "cw", nbins = 10)

Binscatter Plot
Formula: weight ~ Time | Diet 
points = c(0,0) | line = NULL | nbins = 10 (quantile-spaced)
Observations: 578 (original) | 40 (compressed)
# etc.

# See ?dbreg for more connection examples

# Clean up
dbDisconnect(con)
tinyplot::tinytheme() # reset plot theme