plotchisqtest: Illustrate a chi-squared test graphically.

View source: R/plot.chisqtest.R

plotchisqtestR Documentation

Illustrate a \chi^2 test graphically.

Description

This function plots the density probability distribution of a \chi^2 statistic, with a vertical cutline at the observed \chi^2 value specified. The p-value and the observed \chi^2 value are plotted. Although largely customizable, only two arguments are required (the observed \chi^2 and the degrees of freedom).

Usage

plotchisqtest(
  chisq,
  df = chisq$parameter,
  blank = FALSE,
  xmax = "auto",
  title = parse(text = expression(chi^2 ~ "Test")),
  xlabel = parse(text = expression(chi^2)),
  ylabel = "Density of probability\nunder the null hypothesis",
  fontfamily = "serif",
  colorleft = "aliceblue",
  colorright = "firebrick3",
  colorleftcurve = "black",
  colorrightcurve = "black",
  colorcut = "black",
  colorplabel = colorright,
  theme = "default",
  signifdigitschisq = 3,
  curvelinesize = 0.4,
  cutlinesize = curvelinesize,
  p_value_position = "auto"
)

Arguments

chisq

A numeric value indicating the observed \chi^2 statistic. Alternatively, you can use an object of class htest created by the function chisq.test().

df

A numeric value indicating the degrees of freedom. This argument is optional if you are using an htest object as the chisq argument.

blank

A logical that indicates whether to hide (blank = TRUE) the test statistic value, p value and cutline. The corresponding colors are actually only made transparent when blank = TRUE, so that the output is scaled exactly the same (this is useful and especially intended for step-by-step explanations).

xmax

A numeric including the maximum for the x-axis. Defaults to "auto", which scales the plot automatically (optional).

title

A character or expression indicating a custom title for the plot (optional).

xlabel

A character or expression indicating a custom title for the x axis (optional).

ylabel

A character or expression indicating a custom title for the y axis (optional).

fontfamily

A character indicating the font family of all the titles and labels (e.g. "serif" (default), "sans", "Helvetica", "Palatino", etc.) (optional).

colorleft

A character indicating the color for the "left" area under the curve (optional).

colorright

A character indicating the color for the "right" area under the curve (optional).

colorleftcurve

A character indicating the color for the "left" part of the curve (optional).

colorrightcurve

A character indicating the color for the "right" part of the curve (optional). By default, for color consistency, this color is also passed to the label, but this can be changed by providing an argument for the colorlabel parameter.

colorcut

A character indicating the color for the cut line at the observed test statistic (optional).

colorplabel

A character indicating the color for the label of the p-value (optional). By default, for color consistency, this color is the same as color of colorright.

theme

A character indicating one of the predefined color themes. The themes are "default" (light blue and red), "blackandwhite", "whiteandred", "blueandred", "greenandred" and "goldandblue") (optional). Supersedes colorleft and colorright if another argument than "default" is provided.

signifdigitschisq

A numeric indicating the number of desired significant figures reported for the \chi^2 label (optional).

curvelinesize

A numeric indicating the size of the curve line (optional).

cutlinesize

A numeric indicating the size of the cut line (optional). By default, the size of the curve line is used.

p_value_position

A numeric vector of length 2, indicating the x and y coordinates of the p-value label. By default, the position is set to "auto". Note that the absolute value is used, and the sign is ignored.

Value

A plot with the density of probability of \chi^2 under the null hypothesis, annotated with the observed test statistic and the p-value.

Author(s)

Nils Myszkowski <nmyszkowski@pace.edu>

Examples

#Making a chi-squared plot with Chi-squared of 8 and df of 4
plotchisqtest(chisq = 8, df = 4)

#The same plot without the Chi-Squared or p value
plotchisqtest(8,4, blank = TRUE)

#Plot using a chisq.test()
test <- chisq.test(c(A = 37, B = 18, C = 25))
plotchisqtest(test)

#Plot from anova() model comparison
set.seed(1)
y <- rbinom(10, 1, .4) ; x <- 2*y + rnorm(10)
fit1 <- glm(y ~ 1, family = binomial)
fit2 <- glm(y ~ x, family = binomial)
comp <- anova(fit1, fit2, test = "Chisq")
plotchisqtest(comp)


nhstplot documentation built on May 29, 2024, 9:49 a.m.