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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/sjPlotPolynomials.R
\name{sjp.poly}
\alias{sjp.poly}
\title{Plot polynomials for (generalized) linear regression}
\usage{
sjp.poly(
x,
poly.term,
poly.degree,
poly.scale = FALSE,
fun = NULL,
axis.title = NULL,
geom.colors = NULL,
geom.size = 0.8,
show.loess = TRUE,
show.loess.ci = TRUE,
show.p = TRUE,
show.scatter = TRUE,
point.alpha = 0.2,
point.color = "#404040",
loess.color = "#808080"
)
}
\arguments{
\item{x}{A vector, representing the response variable of a linear (mixed) model; or
a linear (mixed) model as returned by \code{\link{lm}} or \code{\link[lme4]{lmer}}.}
\item{poly.term}{If \code{x} is a vector, \code{poly.term} should also be a vector, representing
the polynomial term (independent variabl) in the model; if \code{x} is a
fitted model, \code{poly.term} should be the polynomial term's name as character string.
See 'Examples'.}
\item{poly.degree}{Numeric, or numeric vector, indicating the degree of the polynomial.
If \code{poly.degree} is a numeric vector, multiple polynomial curves for
each degree are plotted. See 'Examples'.}
\item{poly.scale}{Logical, if \code{TRUE}, \code{poly.term} will be scaled before
linear regression is computed. Default is \code{FALSE}. Scaling the polynomial
term may have an impact on the resulting p-values.}
\item{fun}{Linear function when modelling polynomial terms. Use \code{fun = "lm"}
for linear models, or \code{fun = "glm"} for generalized linear models.
When \code{x} is not a vector, but a fitted model object, the function
is detected automatically. If \code{x} is a vector, \code{fun} defaults
to \code{"lm"}.}
\item{axis.title}{Character vector of length one or two (depending on the
plot function and type), used as title(s) for the x and y axis. If not
specified, a default labelling is chosen. \strong{Note:} Some plot types
may not support this argument sufficiently. In such cases, use the returned
ggplot-object and add axis titles manually with
\code{\link[ggplot2]{labs}}. Use \code{axis.title = ""} to remove axis
titles.}
\item{geom.colors}{user defined color for geoms. See 'Details' in \code{\link{plot_grpfrq}}.}
\item{geom.size}{size resp. width of the geoms (bar width, line thickness or point size,
depending on plot type and function). Note that bar and bin widths mostly
need smaller values than dot sizes.}
\item{show.loess}{Logical, if \code{TRUE}, an additional loess-smoothed line is plotted.}
\item{show.loess.ci}{Logical, if \code{TRUE}, a confidence region for the loess-smoothed line
will be plotted.}
\item{show.p}{Logical, if \code{TRUE} (default), p-values for polynomial terms are
printed to the console.}
\item{show.scatter}{Logical, if TRUE (default), adds a scatter plot of data
points to the plot.}
\item{point.alpha}{Alpha value of point-geoms in the scatter plots. Only
applies, if \code{show.scatter = TRUE}.}
\item{point.color}{Color of of point-geoms in the scatter plots. Only applies,
if \code{show.scatter = TRUE.}}
\item{loess.color}{Color of the loess-smoothed line. Only applies, if \code{show.loess = TRUE}.}
}
\value{
A ggplot-object.
}
\description{
This function plots a scatter plot of a term \code{poly.term}
against a response variable \code{x} and adds - depending on
the amount of numeric values in \code{poly.degree} - multiple
polynomial curves. A loess-smoothed line can be added to see
which of the polynomial curves fits best to the data.
}
\details{
For each polynomial degree, a simple linear regression on \code{x} (resp.
the extracted response, if \code{x} is a fitted model) is performed,
where only the polynomial term \code{poly.term} is included as independent variable.
Thus, \code{lm(y ~ x + I(x^2) + ... + I(x^i))} is repeatedly computed
for all values in \code{poly.degree}, and the predicted values of
the reponse are plotted against the raw values of \code{poly.term}.
If \code{x} is a fitted model, other covariates are ignored when
finding the best fitting polynomial. \cr \cr
This function evaluates raw polynomials, \emph{not orthogonal} polynomials.
Polynomials are computed using the \code{\link{poly}} function,
with argument \code{raw = TRUE}. \cr \cr
To find out which polynomial degree fits best to the data, a loess-smoothed
line (in dark grey) can be added (with \code{show.loess = TRUE}). The polynomial curves
that comes closest to the loess-smoothed line should be the best
fit to the data.
}
\examples{
library(sjmisc)
data(efc)
# linear fit. loess-smoothed line indicates a more
# or less cubic curve
sjp.poly(efc$c160age, efc$quol_5, 1)
# quadratic fit
sjp.poly(efc$c160age, efc$quol_5, 2)
# linear to cubic fit
sjp.poly(efc$c160age, efc$quol_5, 1:4, show.scatter = FALSE)
# fit sample model
fit <- lm(tot_sc_e ~ c12hour + e17age + e42dep, data = efc)
# inspect relationship between predictors and response
plot_model(fit, type = "slope")
# "e17age" does not seem to be linear correlated to response
# try to find appropiate polynomial. Grey line (loess smoothed)
# indicates best fit. Looks like x^4 has the best fit,
# however, only x^3 has significant p-values.
sjp.poly(fit, "e17age", 2:4, show.scatter = FALSE)
\dontrun{
# fit new model
fit <- lm(tot_sc_e ~ c12hour + e42dep + e17age + I(e17age^2) + I(e17age^3),
data = efc)
# plot marginal effects of polynomial term
plot_model(fit, type = "pred", terms = "e17age")}
}
|