mywhiskers | R Documentation |
A function for adding vertical lines onto residual plots to show
95% confidence intervals of means or
95% confidence intervals for individual observations
in the classes of the variable on the x-axis. Plot of the first type is useful for analyzing the fit of the assumed fixed part and plots of type b can be used to analyze the homogeneity of residuals.
mywhiskers(x, y, nclass = 10, limits = NA, add = FALSE, se = TRUE, main = "", xlab = "x", ylab = "y", ylim = NA, lwd = 1, highlight = "red")
x |
The variable on the x-axis. Usually one of the predictors or the predicted value. |
y |
The variable on the y-axis. Usually model residual. |
nclass |
The maximum number of classes to be used. |
limits |
The class limits. Alternative to nclass. |
add |
logical. Whether a new graphic window is opened or the lines will be added into an exosting plot. |
se |
Logical. Use standard errors of means (se=TRUE, option (a) above) or class-specific standard deviations (se=FALSE, option (b) above). |
main, xlab, ylab, ylim, lwd |
Graphical parameters of the plot. ignored if |
highlight |
The color for lines that do not cross the y-axis. |
The function first classifies the data in nclass
classes of variable x so that
each class has approximately equal number of observations.
Then the class mean and deviation s is computed for each class, where s is either
the standard error of the mean (if se=TRUE
)
or standard deviation (if se=FALSE
).
A vertical line is plotted at the middle of each class showing the class mean by a dot and
lines of length 3.92*s. If the line does not cross the x- axis, then the highlight color is used in the line.
With small number of observations (or lot of ties), the number of classes is decreased until
each class includes the minimum of 2 observations.
The function is usually used for its side effects (i.e., the plot). However, the values used in producing the plot are returned in a list of elements
x: the class middlepoint x
values.
m: class-specific means of y
.
s: class-specific standard deviations or standard errors of y
(see details).
lb: lower ends of the class-specific lines.
ub: upper ends of the lines.
Lauri Mehtatalo <lauri.mehtatalo@uef.fi>
Mehtatalo, Lauri and Lappi, Juha 2020. Biometry for Forestry and Environmental Data: with examples in R. New York: Chapman and Hall/CRC. 426 p. doi: 10.1201/9780429173462
x<-seq(1,100,1) y<-x+10*log(x)+rnorm(100,0,5) fm1<-lm(y~x) plot(x,resid(fm1)) mywhiskers(x,resid(fm1),se=FALSE,add=TRUE) mywhiskers(x,resid(fm1),se=TRUE,lwd=2,add=TRUE) abline(h=0)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.