| normcheck | R Documentation |
Plots two plots side by side. First, it draws a normal Q-Q plot of the
residuals, along with a line with intercept equal to the mean of the
residuals and slope equal to the standard deviation of the residuals. If
shapiro.wilk = TRUE, the P-value from the Shapiro-Wilk test for
normality is shown in the top-left corner of the Q-Q plot. Second, it draws
a histogram of the residuals. A normal distribution is fitted and
superimposed over the histogram. Note: if you want to leave the
x-axis blank in the histogram then use xlab = c("Theoretical Quantiles", " ")
, i.e. leave a space between the quotes. If you do not leave a space,
information will be extracted from x.
normcheck(x, ...)
## Default S3 method:
normcheck(
x,
xlab = c("Theoretical Quantiles", ""),
ylab = c("Sample Quantiles", ""),
main = c("", ""),
col = "light blue",
bootstrap = FALSE,
B = 5,
bpch = 3,
bcol = "lightgrey",
shapiro.wilk = FALSE,
whichPlot = 1:2,
usePar = TRUE,
engine = c("base", "ggplot2"),
...
)
## S3 method for class 'lm'
normcheck(
x,
xlab = c("Theoretical Quantiles", ""),
ylab = c("Sample Quantiles", ""),
main = c("", ""),
col = "light blue",
bootstrap = FALSE,
B = 5,
bpch = 3,
bcol = "lightgrey",
shapiro.wilk = FALSE,
whichPlot = 1:2,
usePar = TRUE,
engine = c("base", "ggplot2"),
...
)
## S3 method for class 'tslm'
normcheck(
x,
xlab = c("Theoretical Quantiles", ""),
ylab = c("Sample Quantiles", ""),
main = c("", ""),
col = "light blue",
bootstrap = FALSE,
B = 5,
bpch = 3,
bcol = "lightgrey",
shapiro.wilk = FALSE,
whichPlot = 1:2,
usePar = TRUE,
residualType = "normalised",
engine = c("base", "ggplot2"),
...
)
x |
the residuals from fitting a linear model. Alternatively, a fitted |
... |
additional arguments which are passed to both |
xlab |
a title for the x-axis of both the Q-Q plot and the histogram: see |
ylab |
a title for the y-axis of both the Q-Q plot and the histogram: see |
main |
a title for both the Q-Q plot and the histogram: see |
col |
a colour for the bars of the histogram. |
bootstrap |
if |
B |
the number of bootstrap samples to take. Five should usually be sufficient. |
bpch |
the plotting symbol used for the bootstrap samples. Legal values are the same as any legal
value for |
bcol |
the plotting colour used for the bootstrap samples. Legal values are the same as any legal
value for |
shapiro.wilk |
if |
whichPlot |
legal values are |
usePar |
if |
engine |
plotting engine to use. The default, |
residualType |
for |
The default base graphics engine preserves the original teaching plots and draws directly on the active graphics device. The optional ggplot2 engine is intended for users who want reusable plot objects for reports or further customisation; it requires ggplot2 to be installed and returns ggplot objects instead of drawing base graphics side effects.
Draws the selected normality diagnostic plots when using the base
engine. With engine = "ggplot2", returns a ggplot object for a single
selected plot or a named list of ggplot objects for multiple selected plots.
When multiple ggplot2 plots are selected, printing the returned object draws
the plots side by side to match the base graphics teaching layout.
shapiro.test.
# Synthetic teaching example: an exponential growth curve
set.seed(123)
e = rnorm(100, 0, 0.1)
x = rnorm(100)
y = exp(5 + 3 * x + e)
fit = lm(y ~ x)
normcheck(fit)
# An exponential growth curve with the correct transformation
fit = lm(log(y) ~ x)
normcheck(fit)
# Same example as above except we use normcheck.default
normcheck(residuals(fit))
# Peruvian Indians data
data(peru.df)
peruFit = lm(BP ~ weight, data = peru.df)
normcheck(peruFit)
# Optional ggplot2 engine for reusable plot objects
if (requireNamespace("ggplot2", quietly = TRUE)) {
normPlots = normcheck(peruFit, engine = "ggplot2")
names(normPlots)
normcheck(peruFit, engine = "ggplot2", whichPlot = 1)
normcheck(peruFit, engine = "ggplot2", whichPlot = 2)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.