vwReg: Visually Weighted Regression.

View source: R/vwReg.R

vwRegR Documentation

Visually Weighted Regression.

Description

Create watercolor plot to visualize weighted regression.

Usage

vwReg(
  formula,
  data,
  title = "",
  B = 1000,
  shade = TRUE,
  shade.alpha = 0.1,
  spag = FALSE,
  spag.color = "darkblue",
  mweight = TRUE,
  show.lm = FALSE,
  show.median = TRUE,
  median.col = "white",
  shape = 21,
  show.CI = FALSE,
  method = loess,
  bw = FALSE,
  slices = 200,
  palette = colorRampPalette(c("#FFEDA0", "#DD0000"), bias = 2)(20),
  ylim = NULL,
  quantize = "continuous",
  add = FALSE,
  ...
)

Arguments

formula

regression model.

data

dataset.

title

plot title.

B

number of bootstrapped smoothers.

shade

whether to plot the shaded confidence region.

shade.alpha

whether to fade out the confidence interval shading at the edges (by reducing alpha; 0 = no alpha decrease, 0.1 = medium alpha decrease, 0.5 = strong alpha decrease).

spag

whether to plot spaghetti lines.

spag.color

the fitting function for the spaghettis; default: loess.

mweight

logical indicating whether to make the median smoother visually weighted.

show.lm

logical indicating whether to plot the linear regression line.

show.median

logical indicating whether to plot the median smoother.

median.col

color of the median smoother.

shape

shape of points.

show.CI

logical indicating whether to plot the 95% confidence interval limits.

method

color of spaghetti lines.

bw

logical indicating whether to use a b&w palette; default: TRUE.

slices

number of slices in x and y direction for the shaded region. Higher numbers make a smoother plot, but takes longer to draw. I would not set slices to more than 500.

palette

provide a custom color palette for the watercolors.

ylim

restrict range of the watercoloring.

quantize

either continuous, or SD. In the latter case, we get three color regions for 1, 2, and 3 SD (an idea of John Mashey).

add

if add == FALSE, a new ggplot is returned. If add == TRUE, only the elements are returned, which can be added to an existing ggplot (with the + operator).

...

further parameters passed to the fitting function, in the case of loess, for example, span = .9, or family = "symmetric".

Details

Creates a watercolor plot to visualize weighted regression.

Value

plot

See Also

https://www.nicebread.de/visually-weighted-regression-in-r-a-la-solomon-hsiang/

https://www.nicebread.de/visually-weighted-watercolor-plots-new-variants-please-vote/

http://www.fight-entropy.com/2012/07/visually-weighted-regression.html

http://www.fight-entropy.com/2012/08/visually-weighted-confidence-intervals.html

http://www.fight-entropy.com/2012/08/watercolor-regression.html

https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2265501

Other plot: addText(), plot2WayInteraction(), ppPlot()

Other correlations: addText(), cor.table(), crossTimeCorrelation(), crossTimeCorrelationDF(), partialcor.table()

Examples


# Prepare Data
data("mtcars")
df <- data.frame(x = mtcars$hp, y = mtcars$mpg)

## Visually Weighted Regression

# Default
vwReg(y ~ x, df)

# Shade
vwReg(y ~ x, df, shade = TRUE, show.lm = TRUE, show.CI = TRUE,
quantize = "continuous")
vwReg(y ~ x, df, shade = TRUE, show.lm = TRUE, show.CI = TRUE,
quantize = "SD")

# Spaghetti
vwReg(y ~ x, df, shade = FALSE, spag = TRUE, show.lm = TRUE, show.CI = TRUE)
vwReg(y ~ x, df, shade = FALSE, spag = TRUE)

# Black/white
vwReg(y ~ x, df, shade = TRUE, spag = FALSE, show.lm = TRUE, show.CI = TRUE,
bw = TRUE, quantize = "continuous")
vwReg(y ~ x, df, shade = TRUE, spag = FALSE, show.lm = TRUE, show.CI = TRUE,
bw = TRUE, quantize = "SD")
vwReg(y ~ x, df, shade = FALSE, spag = TRUE, show.lm = TRUE, show.CI = TRUE,
bw = TRUE, quantize = "SD")

# Change the bootstrap smoothing
vwReg(y ~ x, df, family = "symmetric") # use an M-estimator for
# bootstrap smoothers. Usually yields wider confidence intervals
vwReg(y ~ x, df, span = 1.7) # increase the span of the smoothers
vwReg(y ~ x, df, span = 0.5) # decrease the span of the smoothers

# Change the color scheme
vwReg(y ~ x, df, palette = viridisLite::viridis(4)) # viridis
vwReg(y ~ x, df, palette = viridisLite::magma(4)) # magma
vwReg(y ~ x, df, palette = RColorBrewer::brewer.pal(9, "YlGnBu")) # change the
# color scheme, using a predefined ColorBrewer palette. You can see all
# available palettes by using this command:
# `library(RColorBrewer); display.brewer.all()`
vwReg(y ~ x, df, palette = grDevices::colorRampPalette(c("white","yellow",
"green","red"))(20)) # use a custom-made palette
vwReg(y ~ x, df, palette = grDevices::colorRampPalette(c("white","yellow",
"green","red"), bias = 3)(20)) # use a custom-made palette, with the
# parameter bias you can shift the color ramp to the “higher” colors
vwReg(y ~ x, df, bw = TRUE) # black and white version
vwReg(y ~ x, df, shade.alpha = 0, palette = grDevices::colorRampPalette(
c("black","grey30","white"), bias = 4)(20)) # Milky-Way Plot
vwReg(y ~ x, df, shade.alpha = 0, slices = 400, palette =
grDevices::colorRampPalette(c("black","green","yellow","red"),
bias = 5)(20), family = "symmetric") # Northern Light Plot/ fMRI plot
vwReg(y ~ x, df, quantize = "SD") # 1-2-3-SD plot



petersenlab documentation built on May 29, 2024, 8:34 a.m.