qq_data: *!!* Compute data for a qq-plot

Description Usage Arguments Details Value See Also Examples

View source: R/qq_data.R

Description

Compute data necessary to plot a quantile comparison plot (qq-plot).

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
qq_data(
  y,
  data = NULL,
  distribution = "norm",
  ...,
  line = c("quartiles", "robust", "int=0,slope=1", "0,1", "none"),
  envelope = 0.95,
  method = c("mle-normal", "trimmed-normal", "moment-normal", "any"),
  labels = NULL,
  groups = NULL,
  sep = " | "
)

## S3 method for class 'qqdata'
coef(object, ...)

## S3 method for class 'qqdata'
plot(x, ..., use_colors = FALSE, scales = "free")

Arguments

y

(formula|numeric|character)
Either a formula, a numeric vector or a name of a column in data.

  • If y is a formula (e.g. variable ~ factor), left-hand side provides the name of a variable to be tested. In the right-hand side there are names of factor variables to be used to create subsets. If the left-hand side is empty (e.g. ~ factor), right-hand side is treated as variable name to test.

data

(data frame|NULL)
Either a data frame that contains the variables mentioned in y or NULL (if the variables are in the function's environment).

distribution

root name of comparison distribution – e.g., "norm" for the normal distribution; t for the t-distribution.

...

Parameters to be passed to function, selected in distribution. In print method, further parameters to function print.

line

(string) A parameter, that controls how reference line is drawn. Options:

  • "0,1" or "int=0,slope=1" to plot a line with intercept = 0 and slope = 1;

  • "quartiles" to pass a line through the quartile-pairs;

  • "robust" for a robust-regression line; the latter uses the rlm function from the MASS package;

  • option "none" is not implemented yet.

envelope

(numeric | FALSE) confidence level for point-wise confidence envelope.

method

(string: "trimmed-normal", "normal", "any"). A method, that controls how estimates of parameters for distribution are computed. Options:

  • "mle-normal" (default) all data values are used to estimate parameters of theoretical normal distribution using method of maximum likelihood;

  • "trimmed-normal" 10\ data values are trimmed before parameters of theoretical normal distribution are estimated using method of moments;

  • "moment-normal" all data values are used to estimate parameters of theoretical normal distribution using method of moments;

  • "any" either parameters are provided manually by user or default parameters are used (if any).

        Options `"mle-normal"`, `"trimmed-normal"` and
        `"moment-normal"` are applicable only if
        `distribution = "norm"`.
        Otherwise `"any"` is selected automatically.
    
groups

(NULL|factor|character)
An alternative way to provide groups. If y is a numeric vector, groups must be a factor (or NULL). If y is a sting, groups must also be a string (or NULL).

sep

(not used yet).

object

A qqdata object.

x

A qqdata object.

use_colors

(logical) use colors for multiple groups

scales

("free"|"free_x"|"free_y"|"fixed") a parmeter to be passed to ggplot2::facet_wrap().

Details

Function qq_data is inspired by qqPlot() in package car (writen by J. Fox).

Value

An object, which inherits from classes qqdata and data.frame. The object contains information, needed to plot a qqplot with reference line and its confidence intervals. These variables are contained:

See Also

car::qqPlot() in car package, stats::qqplot() in stats package.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
library(biostat)
data(chickwts, package = "datasets")

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Input as formula + data:

my_qq_df <- qq_data(~weight, data = chickwts)
head(my_qq_df)
coef(my_qq_df)

# Column ".group" is added if applied by group:

my_qq_df <- qq_data(weight ~ feed, data = chickwts)
head(my_qq_df)
coef(my_qq_df)

qq_plot(weight ~ feed, data = chickwts)

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Input as variable name + data:

my_qq_df <- qq_data("weight", data = chickwts)
head(my_qq_df)
coef(my_qq_df)

my_qq_df <- qq_data("weight", groups = "feed", data = chickwts)
head(my_qq_df)
coef(my_qq_df)

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Input as vector

my_qq_df <- qq_data(chickwts$weight)
head(my_qq_df)
coef(my_qq_df)

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Input as vector, several groups.
# Column ".group" is added

my_qq_df <- qq_data(chickwts$weight, groups = chickwts$feed)
head(my_qq_df)
coef(my_qq_df)
library(biostat)
data(chickwts, package = "datasets")

# Input as formula + data:

my_qq_df <- qq_data(weight ~ feed, data = chickwts)
plot(my_qq_df)

my_qq_df2 <- qq_data(weight ~ feed, data = chickwts, method = "moment-normal")
plot(my_qq_df2)

my_qq_df3 <- qq_data(weight ~ feed, data = chickwts, method = "any")
plot(my_qq_df3)


# The same x and y scale limits for all plots
plot(my_qq_df, scales = "fixed")

# Plot in color
plot(my_qq_df, use_colors = TRUE)

# Plot a qq-plot (with no grouping)
qq_single <- qq_data(~weight, data = chickwts)
plot(qq_single)

class(qq_single)


# More than one grouping variable
data(CO2, package = "datasets")

qq_co2 <- qq_data(uptake ~ Type + Treatment, data = CO2)
plot(qq_co2)

qq_co2_B <- qq_data(uptake ~ Type + Treatment, data = CO2, line = "robust")
plot(qq_co2_B)

GegznaV/BioStat documentation built on Aug. 14, 2020, 9:30 p.m.