| 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 |
.onAttach
.onAttach(libname, pkgname).onAttach(libname, pkgname)
libname |
character, name of library |
pkgname |
character, name of package |
package startup message, list of newly loaded packages in printable grid
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.
bvarirf_to_varirf( bvarirf, impulse, response, cumulative = TRUE, ortho = TRUE, boot = TRUE, ci = 0.05, runs = 100 )bvarirf_to_varirf( bvarirf, impulse, response, cumulative = TRUE, ortho = TRUE, boot = TRUE, ci = 0.05, runs = 100 )
bvarirf |
a |
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 |
a varirf skeleton object good enough for ggvar_irf to plot.
# 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)# 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
core_unloaded()core_unloaded()
a list of packages that have not been loaded yet
Draw partial residual plots with ggplot methods. Automatically plots all driver partial plots in the model.
gg_partial_residual_plots(.data, new_data = NULL, ...)gg_partial_residual_plots(.data, new_data = NULL, ...)
.data |
an lm object |
new_data |
data set model was trained on if class is |
... |
parameters to be passed to |
a patchwork plot layout containing the partial residual plots.
data(freeny, package="datasets") fit = lm(y ~ ., data = freeny) gg_partial_residual_plots(fit)data(freeny, package="datasets") fit = lm(y ~ ., data = freeny) gg_partial_residual_plots(fit)
Plots partial dependence plot for a variable selected with pdp
gg_pdp(.data, ...)gg_pdp(.data, ...)
.data |
a |
... |
additional arguments to pass to |
a ggplot
mpg_fit = lm(mpg ~ disp + hp, data=mtcars) gg_pdp(pdp::partial(mpg_fit, pred.var="disp", train=mtcars, type="regression"))mpg_fit = lm(mpg ~ disp + hp, data=mtcars) gg_pdp(pdp::partial(mpg_fit, pred.var="disp", train=mtcars, type="regression"))
Creates a 3D partial dependence plot from data created from a tidyverse model by pdp
gg_pdp3D(.data)gg_pdp3D(.data)
.data |
a |
a ggplot object
ggplot versions of common residual plots meant to mimic the plot.lm and ggfortify::autoplot.lm
residual plots.
gg_residual_plots(.data, items = c(1:3, 7), new_data = NULL, ...)gg_residual_plots(.data, items = c(1:3, 7), new_data = NULL, ...)
.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 |
... |
parameters to be passed to |
a patchwork array of ggplot objects
data(freeny, package="datasets") fit = lm(y ~ ., data=freeny) gg_residual_plots(fit)data(freeny, package="datasets") fit = lm(y ~ ., data=freeny) gg_residual_plots(fit)
gg_vip
gg_vip(.data, ...)gg_vip(.data, ...)
.data |
a data frame created by |
... |
arguments to pass to |
a ggplot
mpg_fit = lm(mpg ~ ., data=mtcars) gg_vip(vip::vi(mpg_fit))mpg_fit = lm(mpg ~ ., data=mtcars) gg_vip(vip::vi(mpg_fit))
Forecast Error Variance Decomposition plots for bvartools::bvar objects
from bvartools; plots all responses or any one
ggvar_fevdplot(bvarobject, type = "oir", ...)ggvar_fevdplot(bvarobject, type = "oir", ...)
bvarobject |
a |
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 |
a named list of ggplots, named by response variables of model. Display with patchwork or just plot one list member.
#' # 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)#' # 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)
plot multiple timeseries and forecasts made with predict;
use design and patchwork::wrap_plots to plot from list.
ggvar_forecastplot(bvar_pred, trun = 1)ggvar_forecastplot(bvar_pred, trun = 1)
bvar_pred |
predictions of class |
trun |
integer, start index of data to plot for all predictions; default 1 |
a list of ggplots
# 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)# 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)
Plots impulse response functions from a vars::varirf object
ggvar_irf(varirf)ggvar_irf(varirf)
varirf |
a |
a ggplot
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)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)
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
ggvar_plot_stability(varstabil, lines = TRUE, breaks = TRUE, a = 1.2516)ggvar_plot_stability(varstabil, lines = TRUE, breaks = TRUE, a = 1.2516)
varstabil |
a |
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 |
a list of ggplots one for each variable
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 plotdata("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
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.
ggvar_plot_stability_break(pi, bv)ggvar_plot_stability_break(pi, bv)
pi |
breakpoint index; obtain from process maximum point |
bv |
breakpoint value; use pi to obtain from process |
ggplot layers to add to a ggvar_plot_stability() plot
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 defaultdata("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
Internal function that adds layers to a ggvar_plot_stability plot;
adds horizontal boundary lines. Called by
ggvar_plot_stability optionally.
ggvar_plot_stability_lines(a = 1.2516)ggvar_plot_stability_lines(a = 1.2516)
a |
limit for upper and lower bounds; default 1.2516, taken from
Gemini discussion of limits for |
a ggplot
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 defaultdata("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
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
GrangerPlot(.data)GrangerPlot(.data)
.data |
a dataframe object created by |
a list of two plots for each direction of Granger Causality test
## Not run: data(moody, package="mbadtools") patchwork::wrap_plots( GrangerPlot( GrangerTable(moody, ICS, Wgrowth) ),nrow=2) ## End(Not run)## Not run: data(moody, package="mbadtools") patchwork::wrap_plots( GrangerPlot( GrangerTable(moody, ICS, Wgrowth) ),nrow=2) ## End(Not run)
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.
GrangerTable(.data, s1, s2, max.lags = 10)GrangerTable(.data, s1, s2, max.lags = 10)
.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. |
data frame with lags and p.values of two granger tests
data(moody, package="mbadtools") moody %>% GrangerTable(ICS, Wgrowth)data(moody, package="mbadtools") moody %>% GrangerTable(ICS, Wgrowth)
Use this function for two time series that were taken at the same times.
GrangerTestPvals(ts1, ts2, max.lags = 10)GrangerTestPvals(ts1, ts2, max.lags = 10)
ts1 |
time series of type ts |
ts2 |
time series of type ts |
max.lags |
number of lags to consider, default = 10 |
Creates a data frame of p-values for bi-directional Granger Causality test for
multiple lags, using lmtest::grangertest.
data frame with max.lags rows and three columns: lag, the lag number, p12, the p-value for ts1->ts2, and p21, the reverse.
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)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)
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.
GrangerTestTune(ts1, ts2, max.lags = 10)GrangerTestTune(ts1, ts2, max.lags = 10)
ts1 |
time series |
ts2 |
time series |
max.lags |
maximum number of lags to try, default 10 |
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.
## 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)## 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)
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.
gt_add_significance(.data, level = 0.05, digits = 4)gt_add_significance(.data, level = 0.05, digits = 4)
.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 |
a gt table with the pvalue column colored where result is significant.
## Not run: data(cars) fit = lm(dist~speed, data=cars); gt_add_significance(broom::tidy(fit)) ## End(Not run)## Not run: data(cars) fit = lm(dist~speed, data=cars); gt_add_significance(broom::tidy(fit)) ## End(Not run)
Implements Klein's rule of thumb for linear models using lm to detect multicollinearity.
Kleinsrule(.data, response)Kleinsrule(.data, response)
.data |
a dataframe with the target and drivers under study. |
response |
response for the regression |
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.
a tibble with the r-squared values of the main and all auxiliary regressions
## Not run: data(longley, package="datasets") # this data is known to be collinear Kleinsrule(longley, response = "Employed") ## End(Not run)## Not run: data(longley, package="datasets") # this data is known to be collinear Kleinsrule(longley, response = "Employed") ## End(Not run)
enables packages listed in a character vector to an R script
lib_packages(pkgtxtvector, quiet = FALSE)lib_packages(pkgtxtvector, quiet = FALSE)
pkgtxtvector |
character vector of package names |
quiet |
logical suppress startup messages; default = TRUE |
same as library
library, suppressPackageStartupMessages
## Not run: pkgs = c("tidyverse","car") lib_packages(pkgs) ## End(Not run)## Not run: pkgs = c("tidyverse","car") lib_packages(pkgs) ## End(Not run)
A dataset containing wage growth and consumer sentiment data.
moodymoody
A data frame with 299 rows and 3 columns:
date-time of observation
numeric, wage growth at that time
numeric consumer sentiment survey at that time
consumer sentiment data from University of Michigan Survey; Wage growth from Atlanta Fed Wage Tracker.
Arrange compute data for a pareto plot
pareto_arrange_data(.data, countcol)pareto_arrange_data(.data, countcol)
.data |
data frame with a category variable and a count column,
often produced with |
countcol |
name of the column with the counts, no quotes required |
a dataframe with the category and count columns plus the Cumulative and Percentage columns.
Pareto Plot Examples,
pareto.chart
data(chickwts, package="datasets") K = chickwts %>% group_by(feed) %>% summarize(avwt = mean(weight)) K1 = pareto_arrange_data(K, avwt) print(K1)data(chickwts, package="datasets") K = chickwts %>% group_by(feed) %>% summarize(avwt = mean(weight)) K1 = pareto_arrange_data(K, avwt) print(K1)
Puts a cutoff line on a pareto_plot
pareto_cutoff_line(maxcount, percent = 80)pareto_cutoff_line(maxcount, percent = 80)
maxcount |
maximum count |
percent |
cutoff percent, default=80 |
added ggplot layer with the Pareto percentage line
Pareto Plot Examples,
pareto.chart
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))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))
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.
pareto_pct_labels(maxcount, ...)pareto_pct_labels(maxcount, ...)
maxcount |
maximum count |
... |
additional parameters to |
added ggplot layer
Pareto Plot Examples,
pareto.chart
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)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)
Make a pareto plot from data arranged with pareto_arrange
pareto_plot(.data, category, count)pareto_plot(.data, category, count)
.data |
a pareto_arranged data frame |
category |
name of the category column |
count |
name of the count column |
a ggplot object
Pareto Plot Examples,
pareto.chart
data(chickwts, package="datasets") K = chickwts %>% group_by(feed) %>% summarize(avwt = mean(weight)) K1 = pareto_arrange_data(K, avwt) pareto_plot(K1, feed, avwt)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
pareto_sec_axis(maxcount)pareto_sec_axis(maxcount)
maxcount |
numeric, maximum value of the count column |
a ggplot layer
Pareto Plot Examples,
pareto.chart
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))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))
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.
rpart_unscale_object(surr_model_object, train_data_p)rpart_unscale_object(surr_model_object, train_data_p)
surr_model_object |
an |
train_data_p |
training data, with unscaled drivers |
rpart object with splits index column rescaled
Tukey test of curvature in linear regression models
Tukey_test(.data, fmt = c("gt", "summary"))Tukey_test(.data, fmt = c("gt", "summary"))
.data |
an lm() regression object |
fmt |
character c("gt", "summary"), default "gt" |
a list; summary a data frame with the variables, statistics and p-values gt a gt table with the same
fit = lm(mpg~hp, data=mtcars) Tukey_test(fit, fmt = "summary") Tukey_test(fit)fit = lm(mpg~hp, data=mtcars) Tukey_test(fit, fmt = "summary") Tukey_test(fit)
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.
vi_pre(model, num_features = 15)vi_pre(model, num_features = 15)
model |
model generated by |
num_features |
number of features to retain; default 15 |
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