Package 'mbadtools'

Title: MBAD Data Analytics Functions
Description: Some functions useful for my MBAD courses on Data Analytics. Loads packages from core packages list on library call Includes Residual plots, Pareto plots, and adding significance color to pvalues in a gt table. Includes Granger causality table and plot functions. Includes functions for handling VAR models with bvartools and plotting.
Authors: Bruce Hartman [aut, cre]
Maintainer: Bruce Hartman <[email protected]>
License: MIT + file LICENSE
Version: 0.0.3.1013
Built: 2026-10-14 08:11:44 UTC
Source: https://github.com/bhartman2/mbadtools

Help Index


.onAttach

Description

.onAttach

Usage

.onAttach(libname, pkgname)

Arguments

libname

character, name of library

pkgname

character, name of package

Value

package startup message, list of newly loaded packages in printable grid


bvarirf_to_varirf

Description

Transforms a bvarirf object from bvartools::irf() into a varirf skeleton object from vars::irf(), for ggvar_irf() to plot. The bvarirf object only contains one response at a time.

Usage

bvarirf_to_varirf(
  bvarirf,
  impulse,
  response,
  cumulative = TRUE,
  ortho = TRUE,
  boot = TRUE,
  ci = 0.05,
  runs = 100
)

Arguments

bvarirf

a bvarirf object made by bvartools::irf()

impulse

character, the impulse column name

response

character, the response column name

cumulative

logical, default TRUE

ortho

logical, default TRUE

boot

logical, default TRUE

ci

numeric p-value for confidence interval; in (0,1), default .05

runs

integer, number of runs, default 100

Value

a varirf skeleton object good enough for ggvar_irf to plot.

Examples

# Load data
data("e1", package="bvartools")
e1 <- diff(log(e1)) * 100

# Generate model data
model <- bvartools::gen_var(e1, p = 2, deterministic = 2,
                 iterations = 100, burnin = 10)
# Number of iterations and burnin should be much higher.

# Add prior specifications
model <- bvartools::add_priors(model)

# Obtain posterior draws
object <- bvartools::draw_posterior(model)

# Calculate IR
ir <- bvartools::irf(object, impulse = "invest", response = "cons")

# Plot IR
X = bvarirf_to_varirf(ir, impulse="invest",  response="cons")
ggvar_irf(X)

core_unloaded

Description

core_unloaded

Usage

core_unloaded()

Value

a list of packages that have not been loaded yet


Partial Residual Plots

Description

Draw partial residual plots with ggplot methods. Automatically plots all driver partial plots in the model.

Usage

gg_partial_residual_plots(.data, new_data = NULL, ...)

Arguments

.data

an lm object

new_data

data set model was trained on if class is ⁠_lm⁠; default NULL

...

parameters to be passed to patchwork::wrap_plots()

Value

a patchwork plot layout containing the partial residual plots.

Examples

data(freeny, package="datasets")
fit = lm(y ~ ., data = freeny)
gg_partial_residual_plots(fit)

gg_pdp

Description

Plots partial dependence plot for a variable selected with pdp

Usage

gg_pdp(.data, ...)

Arguments

.data

a pdp object with variable selected

...

additional arguments to pass to geom_point()

Value

a ggplot

Examples

mpg_fit = lm(mpg ~ disp + hp, data=mtcars)
gg_pdp(pdp::partial(mpg_fit, pred.var="disp", train=mtcars,
            type="regression"))

gg_pdp3D

Description

Creates a 3D partial dependence plot from data created from a tidyverse model by pdp

Usage

gg_pdp3D(.data)

Arguments

.data

a pdp object with two variables selected

Value

a ggplot object


gg_residual_plots

Description

ggplot versions of common residual plots meant to mimic the plot.lm and ggfortify::autoplot.lm residual plots.

Usage

gg_residual_plots(.data, items = c(1:3, 7), new_data = NULL, ...)

Arguments

.data

an lm() regression object

items

a vector of integers from 0 to 9; which of 9 plots to select; default c(1:3,7)

new_data

data set model was trained on if class is ⁠_lm⁠; default NULL

...

parameters to be passed to patchwork::wrap_plots()

Value

a patchwork array of ggplot objects

Examples

data(freeny, package="datasets")
fit = lm(y ~ ., data=freeny)
gg_residual_plots(fit)

gg_vip

Description

gg_vip

Usage

gg_vip(.data, ...)

Arguments

.data

a data frame created by vip::vi, with column names c("Variable", "Importance")

...

arguments to pass to ggplot2::geom_col

Value

a ggplot

Examples

mpg_fit = lm(mpg ~ ., data=mtcars)
gg_vip(vip::vi(mpg_fit))

ggvar_fevdplot

Description

Forecast Error Variance Decomposition plots for bvartools::bvar objects from bvartools; plots all responses or any one

Usage

ggvar_fevdplot(bvarobject, type = "oir", ...)

Arguments

bvarobject

a bvarest object from bvartools

type

character, type of fevd to create, no default, allowed types are c("oir","gir","sir", "sgir"); only "oir" has sum to unity.

...

other parameters for bvartools:fevd.bvar(); see documentation for details.

Value

a named list of ggplots, named by response variables of model. Display with patchwork or just plot one list member.

Examples

#' # Load data
library(bvartools)
data("e1")
e1 <- diff(log(e1)) * 100

# Generate model data
model <- bvartools::gen_var(e1, p = 2, deterministic = 2,
                 iterations = 100, burnin = 10)

model <- bvartools::add_priors(model)

# Obtain posterior draws
object <- bvartools::draw_posterior(model)

ggvar_fevdplot(object, type="oir", n.ahead=7)

ggvar_forecastplot

Description

plot multiple timeseries and forecasts made with predict; use design and patchwork::wrap_plots to plot from list.

Usage

ggvar_forecastplot(bvar_pred, trun = 1)

Arguments

bvar_pred

predictions of class bvarprd from bvartools::predict.bvar() or varprd from vars::predict().

trun

integer, start index of data to plot for all predictions; default 1

Value

a list of ggplots

Examples

# Load data
require(bvartools)
data("e1")
e1 <- diff(log(e1)) * 100
# Generate model data
model <- bvartools::gen_var(e1, p = 2, deterministic = 2,
                 iterations = 100, burnin = 10)
model <- bvartools::add_priors(model)
# Obtain posterior draws
object <- draw_posterior(model)
# Calculate forecasts
pred <- predict(object, new_d = rep(1, 10))
# Plot forecasts
ggvar_forecastplot(pred)

ggvar_irf

Description

Plots impulse response functions from a vars::varirf object

Usage

ggvar_irf(varirf)

Arguments

varirf

a varirf object created using the vars package

Value

a ggplot

Examples

data("Canada", package="vars")
var.2c <- vars::VAR(Canada, p = 2, type = "const")
IRF = vars::irf(var.2c, impulse = "e", response = "prod")
ggvar_irf(IRF)

ggvar_plot_stability

Description

Plots the list of empirical fluctuation processes created by the stability test; one is created for each equation. Use wrap_plots to display, where n is the number of plots. It wraps ggvar_plot_stability_break and and ggvar_plot_stability_lines

If you want one or just some plots, make them into a list first

Usage

ggvar_plot_stability(varstabil, lines = TRUE, breaks = TRUE, a = 1.2516)

Arguments

varstabil

a varstabil list object from stability or a list of efp objects

lines

logical, plot the boundary lines? default TRUE

breaks

logical, plot vertical line at break and value, default TRUE

a

numeric, limit for boundary lines, default 1.2516; see ggvar_plot_stability_break for rationale

Value

a list of ggplots one for each variable

Examples

data("Canada", package="vars")
var.2c <- vars::VAR(Canada, p = 2, type = "const")
stab = vars::stability(var.2c)
ggvar_plot_stability(stab, lines=FALSE, breaks=FALSE) # override defaults for generic plot

ggvar_plot_stability_break

Description

Internal function that adds layers to a ggvar_plot_stability plot; a vertical line and a text notation of the max value, at the max of the stability index. Called by ggvar_plot_stability optionally.

Usage

ggvar_plot_stability_break(pi, bv)

Arguments

pi

breakpoint index; obtain from process maximum point

bv

breakpoint value; use pi to obtain from process

Value

ggplot layers to add to a ggvar_plot_stability() plot

Examples

data("Canada", package="vars")
var.2c <- vars::VAR(Canada, p = 2, type = "const")
stab = vars::stability(var.2c)
ggvar_plot_stability(stab, lines=FALSE) # override default

ggvar_plot_stability_lines

Description

Internal function that adds layers to a ggvar_plot_stability plot; adds horizontal boundary lines. Called by ggvar_plot_stability optionally.

Usage

ggvar_plot_stability_lines(a = 1.2516)

Arguments

a

limit for upper and lower bounds; default 1.2516, taken from Gemini discussion of limits for efp.

Value

a ggplot

Examples

data("Canada", package="vars")
var.2c <- vars::VAR(Canada, p = 2, type = "const")
stab = vars::stability(var.2c)
ggvar_plot_stability(stab, breaks=FALSE) # lines=T is default

GrangerPlot

Description

Creates list of plots of Granger test p-values for multiple lags from dataframe created by GrangerTable. Use this function on a data frame if the data column observations were taken at the same times

Usage

GrangerPlot(.data)

Arguments

.data

a dataframe object created by GrangerTable.

Value

a list of two plots for each direction of Granger Causality test

See Also

Examples, grangertest

Examples

## Not run: 
data(moody, package="mbadtools")
patchwork::wrap_plots(
    GrangerPlot(
        GrangerTable(moody, ICS, Wgrowth)
        ),nrow=2)

## End(Not run)

GrangerTable

Description

Computes p.values for Granger cause test of two time series, in both directions. Can be supplied as input to mbadtools::GrangerPlot(). Use this if the data is in a dataframe and you are sure the two series observations are taken at the same times.

Usage

GrangerTable(.data, s1, s2, max.lags = 10)

Arguments

.data

dataframe containing two numeric columns

s1

series 1 numeric column name

s2

series 2 numeric column name

max.lags

integer, maximum number of lags to consider, default =10.

Value

data frame with lags and p.values of two granger tests

See Also

Examples, grangertest

Examples

data(moody, package="mbadtools")
moody %>% GrangerTable(ICS, Wgrowth)

GrangerTestPvals

Description

Use this function for two time series that were taken at the same times.

Usage

GrangerTestPvals(ts1, ts2, max.lags = 10)

Arguments

ts1

time series of type ts

ts2

time series of type ts

max.lags

number of lags to consider, default = 10

Details

Creates a data frame of p-values for bi-directional Granger Causality test for multiple lags, using lmtest::grangertest.

Value

data frame with max.lags rows and three columns: lag, the lag number, p12, the p-value for ts1->ts2, and p21, the reverse.

Examples

data(EuStockMarkets, package="datasets")
# extract two time series
DAX = EuStockMarkets[,"DAX"] 
FTSE = EuStockMarkets[,"FTSE"]
# create the p-values table for the two directions 
GrangerTestPvals(DAX, FTSE)

GrangerTestTune

Description

Run a Granger Causality test in both directions for one time series versus another, over a range of different orders. Use this function for two time series that were taken at the same times.

Usage

GrangerTestTune(ts1, ts2, max.lags = 10)

Arguments

ts1

time series

ts2

time series

max.lags

maximum number of lags to try, default 10

Value

a list containing two ggplot objects, showing variation in p-value of Granger causality test in each directionwith number of lags; can be plotted by new('ggmultiplot') or other means.

Examples

## Not run: 
data(EuStockMarkets, package="datasets")
# extract two time series
DA = EuStockMarkets[,"DAX"] 
FT = EuStockMarkets[,"FTSE"]
# plot the two plots with patchwork
GrangerTestTune(DA, FT) %>% patchwork::wrap_plots(nrow=2) 

## End(Not run)

gt_add_significance

Description

Makes a gt table, and adds colored significance notation, to a data frame containing p.value column. Useful for glance and tidy output from lm models.

Usage

gt_add_significance(.data, level = 0.05, digits = 4)

Arguments

.data

A tidy or glance tibble resulting from an lm model.

level

The significance level selected. default = .05

digits

number of digits to retain in numeric columns; default = 4

Value

a gt table with the pvalue column colored where result is significant.

Examples

## Not run: 
data(cars)
fit = lm(dist~speed, data=cars);
gt_add_significance(broom::tidy(fit))

## End(Not run)

Kleinsrule

Description

Implements Klein's rule of thumb for linear models using lm to detect multicollinearity.

Usage

Kleinsrule(.data, response)

Arguments

.data

a dataframe with the target and drivers under study.

response

response for the regression

Details

Klein's rule of thumb is to run auxiliary regressions with each driver variable against all the others. Then compare these r-squareds to the full regression on all the drivers. The function builds a tibble to make this comparison easy.

Value

a tibble with the r-squared values of the main and all auxiliary regressions

Examples

## Not run: 
data(longley, package="datasets")
# this data is known to be collinear
Kleinsrule(longley, response = "Employed")

## End(Not run)

lib_packages

Description

enables packages listed in a character vector to an R script

Usage

lib_packages(pkgtxtvector, quiet = FALSE)

Arguments

pkgtxtvector

character vector of package names

quiet

logical suppress startup messages; default = TRUE

Value

same as library

See Also

library, suppressPackageStartupMessages

Examples

## Not run: 
pkgs = c("tidyverse","car")
lib_packages(pkgs)

## End(Not run)

moody

Description

A dataset containing wage growth and consumer sentiment data.

Usage

moody

Format

A data frame with 299 rows and 3 columns:

Date

date-time of observation

Wgrowth

numeric, wage growth at that time

ICS

numeric consumer sentiment survey at that time

Source

consumer sentiment data from University of Michigan Survey; Wage growth from Atlanta Fed Wage Tracker.


pareto_arrange_data

Description

Arrange compute data for a pareto plot

Usage

pareto_arrange_data(.data, countcol)

Arguments

.data

data frame with a category variable and a count column, often produced with dplyr::summarize

countcol

name of the column with the counts, no quotes required

Value

a dataframe with the category and count columns plus the Cumulative and Percentage columns.

See Also

Pareto Plot Examples, pareto.chart

Examples

data(chickwts, package="datasets")
K = chickwts %>% group_by(feed) %>% summarize(avwt = mean(weight))
K1 = pareto_arrange_data(K, avwt)
print(K1)

pareto_cutoff_line

Description

Puts a cutoff line on a pareto_plot

Usage

pareto_cutoff_line(maxcount, percent = 80)

Arguments

maxcount

maximum count

percent

cutoff percent, default=80

Value

added ggplot layer with the Pareto percentage line

See Also

Pareto Plot Examples, pareto.chart

Examples

data(chickwts, package="datasets")
K = chickwts %>% group_by(feed) %>% summarize(avwt = mean(weight))
K1 = pareto_arrange_data(K, avwt)
pareto_plot(K1, feed, avwt) +
  pareto_cutoff_line(max(K1$avwt))

pareto_pct_labels

Description

Add percent labels on cumulative percent line. Use additional parameters to size and position the labels on the plot. Data object must have a Percentage column.

Usage

pareto_pct_labels(maxcount, ...)

Arguments

maxcount

maximum count

...

additional parameters to geom_text

Value

added ggplot layer

See Also

Pareto Plot Examples, pareto.chart

Examples

data(chickwts, package="datasets")
K = chickwts %>% group_by(feed) %>% summarize(avwt = mean(weight))
K1 = pareto_arrange_data(K, avwt)
pareto_plot(K1, feed, avwt) +
  pareto_pct_labels(max(K1$avwt), nudge_y=1, size=3)

pareto_plot

Description

Make a pareto plot from data arranged with pareto_arrange

Usage

pareto_plot(.data, category, count)

Arguments

.data

a pareto_arranged data frame

category

name of the category column

count

name of the count column

Value

a ggplot object

See Also

Pareto Plot Examples, pareto.chart

Examples

data(chickwts, package="datasets")
K = chickwts %>% group_by(feed) %>% summarize(avwt = mean(weight))
K1 = pareto_arrange_data(K, avwt)
pareto_plot(K1, feed, avwt)

pareto_sec_axis

Description

pareto_sec_axis

Usage

pareto_sec_axis(maxcount)

Arguments

maxcount

numeric, maximum value of the count column

Value

a ggplot layer

See Also

Pareto Plot Examples, pareto.chart

Examples

data(chickwts, package="datasets")
K = chickwts %>% group_by(feed) %>% summarize(avwt = mean(weight))
K1 = pareto_arrange_data(K, avwt)
pareto_plot(K1, feed, avwt) +
  pareto_sec_axis(max(K1$avwt))

rpart_unscale_object

Description

Rescales the driver splits in an rpart object using means and sds of training data. Caution: the new object only has splits scaled that are used in the tree plot and rules.

Usage

rpart_unscale_object(surr_model_object, train_data_p)

Arguments

surr_model_object

an rpart model object

train_data_p

training data, with unscaled drivers

Value

rpart object with splits index column rescaled


Tukey_test

Description

Tukey test of curvature in linear regression models

Usage

Tukey_test(.data, fmt = c("gt", "summary"))

Arguments

.data

an lm() regression object

fmt

character c("gt", "summary"), default "gt"

Value

a list; summary a data frame with the variables, statistics and p-values gt a gt table with the same

Examples

fit = lm(mpg~hp, data=mtcars)
Tukey_test(fit, fmt = "summary")
Tukey_test(fit)

vi_pre

Description

Creates vip:vi() style data frame for a model of class pre Features are sorted in descending order of importance. Models of this class are essentially linear so a sign is reported as well. The result can be plotted with gg_vip.

Usage

vi_pre(model, num_features = 15)

Arguments

model

model generated by pre::pre

num_features

number of features to retain; default 15

Value

a data frame with num_features rows and several columns:

  • code, rule id; created by pre

  • Variable, character variable; shows the rule in English

  • Importance, numeric variable importance

  • coefficient, numeric; coefficient in linear (lasso) regression

  • sd, numeric; standard deviation of coefficient

  • Sign, factor; -1 or 1 depending on sign of coefficient