View source: R/sjPlotPolynomials.R

sjp.poly | R Documentation |

This function plots a scatter plot of a term `poly.term`

against a response variable `x`

and adds - depending on
the amount of numeric values in `poly.degree`

- multiple
polynomial curves. A loess-smoothed line can be added to see
which of the polynomial curves fits best to the data.

```
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"
)
```

`x` |
A vector, representing the response variable of a linear (mixed) model; or
a linear (mixed) model as returned by |

`poly.term` |
If |

`poly.degree` |
Numeric, or numeric vector, indicating the degree of the polynomial.
If |

`poly.scale` |
Logical, if |

`fun` |
Linear function when modelling polynomial terms. Use |

`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. |

`geom.colors` |
user defined color for geoms. See 'Details' in |

`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. |

`show.loess` |
Logical, if |

`show.loess.ci` |
Logical, if |

`show.p` |
Logical, if |

`show.scatter` |
Logical, if TRUE (default), adds a scatter plot of data points to the plot. |

`point.alpha` |
Alpha value of point-geoms in the scatter plots. Only
applies, if |

`point.color` |
Color of of point-geoms in the scatter plots. Only applies,
if |

`loess.color` |
Color of the loess-smoothed line. Only applies, if |

For each polynomial degree, a simple linear regression on `x`

(resp.
the extracted response, if `x`

is a fitted model) is performed,
where only the polynomial term `poly.term`

is included as independent variable.
Thus, `lm(y ~ x + I(x^2) + ... + I(x^i))`

is repeatedly computed
for all values in `poly.degree`

, and the predicted values of
the reponse are plotted against the raw values of `poly.term`

.
If `x`

is a fitted model, other covariates are ignored when
finding the best fitting polynomial.

This function evaluates raw polynomials, *not orthogonal* polynomials.
Polynomials are computed using the `poly`

function,
with argument `raw = TRUE`

.

To find out which polynomial degree fits best to the data, a loess-smoothed
line (in dark grey) can be added (with `show.loess = TRUE`

). The polynomial curves
that comes closest to the loess-smoothed line should be the best
fit to the data.

A ggplot-object.

```
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)
## Not run:
# 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")
## End(Not run)
```

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.