Plot polynomials for (generalized) linear regression — sjp.poly (2024)

Source: R/sjPlotPolynomials.R

sjp.poly.Rd

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", prnt.plot = TRUE)

Arguments

x

A vector, representing the response variable of a linear (mixed) model; ora linear (mixed) model as returned by lm or lmer.

poly.term

If x is a vector, poly.term should also be a vector, representingthe polynomial term (independent variabl) in the model; if x is afitted model, poly.term should be the polynomial term's name as character string.See 'Examples'.

poly.degree

Numeric, or numeric vector, indicating the degree of the polynomial.If poly.degree is a numeric vector, multiple polynomial curves foreach degree are plotted. See 'Examples'.

poly.scale

Logical, if TRUE, poly.term will be scaled beforelinear regression is computed. Default is FALSE. Scaling the polynomialterm may have an impact on the resulting p-values.

fun

Linear function when modelling polynomial terms. Use fun = "lm"for linear models, or fun = "glm" for generalized linear models.When x is not a vector, but a fitted model object, the functionis detected automatically. If x is a vector, fun defaultsto "lm".

axis.title

Character vector of length one or two (depending onthe plot function and type), used as title(s) for the x and y axis.If not specified, a default labelling is chosen. To set multipleaxis titles (e.g. with type = "eff" for many predictors),axis.title must be a character vector of same length of plotsthat are printed. In this case, each plot gets an own axis title(applying, for instance, to the y-axis for type = "eff").Note: Some plot types do not support this argument. In suchcases, use the return value and add axis titles manually withlabs, e.g.: $plot.list[[1]] + labs(x = ...)

geom.colors

User defined color palette for geoms. If group.estimatesis not specified, must either be vector with two color values or a specificcolor palette code (see 'Details' in sjp.grpfrq). Else, ifgroup.estimates is specified, geom.colors must be a vectorof same length as groups. See 'Examples'.

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 mostlyneed smaller values than dot sizes.

show.loess

Logical, if TRUE, an additional loess-smoothed line is plotted.

show.loess.ci

Logical, if TRUE, a confidence region for the loess-smoothed linewill be plotted.

show.p

Logical, if TRUE (default), p-values for polynomial terms areprinted to the console.

show.scatter

Logical, if TRUE (default), adds a scatter plot ofdata points to the plot. Only applies for slope-type or predictions plots.For most plot types, dots are jittered to avoid overplotting, hence thepoints don't reflect exact values in the data.

point.alpha

Alpha value of point-geoms in the scatter plots. Only applies,if show.scatter = TRUE.

point.color

Color of of point-geoms in the scatter plots. Only applies,if show.scatter = TRUE.

loess.color

Color of the loess-smoothed line. Only applies, if show.loess = TRUE.

prnt.plot

logical, if TRUE (default), plots the results as graph. Use FALSE if you don'twant to plot any graphs. In either case, the ggplot-object will be returned as value.

Value

(Insisibily) returns

plot

the ggplot-object with the complete plot

df

the data frame that was used for setting up the ggplot-object

cutpoints

a data frame that indicates x-values and predicted y-values of each direction change in the loess curvature

Details

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.

See also

To plot marginal effects of polynomial terms, call sjp.lm with type = "poly", or sjp.lmer respectively for linear mixed models.

Examples

library(sjmisc)data(efc)# linear fit. loess-smoothed line indicates a more# or less cubic curvesjp.poly(efc$c160age, efc$quol_5, 1)

#> Polynomial degrees: 1#> ---------------------#> p(x^1): 0.000#>

Plot polynomials for (generalized) linear regression — sjp.poly (1)

# quadratic fitsjp.poly(efc$c160age, efc$quol_5, 2)

#> Polynomial degrees: 2#> ---------------------#> p(x^1): 0.078#> p(x^2): 0.533#>

Plot polynomials for (generalized) linear regression — sjp.poly (2)

# linear to cubic fitsjp.poly(efc$c160age, efc$quol_5, 1:4, show.scatter = FALSE)

#> Polynomial degrees: 1#> ---------------------#> p(x^1): 0.000#> #> Polynomial degrees: 2#> ---------------------#> p(x^1): 0.078#> p(x^2): 0.533#> #> Polynomial degrees: 3#> ---------------------#> p(x^1): 0.012#> p(x^2): 0.001#> p(x^3): 0.000#> #> Polynomial degrees: 4#> ---------------------#> p(x^1): 0.777#> p(x^2): 0.913#> p(x^3): 0.505#> p(x^4): 0.254#>

Plot polynomials for (generalized) linear regression — sjp.poly (3)

# fit sample modelfit <- lm(tot_sc_e ~ c12hour + e17age + e42dep, data = efc)# inspect relationship between predictors and responsesjp.lm(fit, type = "slope", show.loess = TRUE, show.scatter = FALSE)

#> Warning: 'sjp.lm' is deprecated.#> Use 'plot_model' instead.#> See help("Deprecated")

#> This plot type has been removed, as it was misleading. Use `plot_model` with `type = "pred"` to plot marginal effects, or with `type = "slope"` for model diagnostisc.

Plot polynomials for (generalized) linear regression — sjp.poly (4)

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

#> Polynomial degrees: 2#> ---------------------#> p(x^1): 0.734#> p(x^2): 0.721#> #> Polynomial degrees: 3#> ---------------------#> p(x^1): 0.010#> p(x^2): 0.011#> p(x^3): 0.011#> #> Polynomial degrees: 4#> ---------------------#> p(x^1): 0.234#> p(x^2): 0.267#> p(x^3): 0.303#> p(x^4): 0.343#>

Plot polynomials for (generalized) linear regression — sjp.poly (5)

# NOT RUN {# fit new modelfit <- lm(tot_sc_e ~ c12hour + e42dep + e17age + I(e17age^2) + I(e17age^3), data = efc)# plot marginal effects of polynomial termsjp.lm(fit, type = "poly", poly.term = "e17age")# }
Plot polynomials for (generalized) linear regression — sjp.poly (2024)

References

Top Articles
Latest Posts
Article information

Author: Lilliana Bartoletti

Last Updated:

Views: 5761

Rating: 4.2 / 5 (73 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Lilliana Bartoletti

Birthday: 1999-11-18

Address: 58866 Tricia Spurs, North Melvinberg, HI 91346-3774

Phone: +50616620367928

Job: Real-Estate Liaison

Hobby: Graffiti, Astronomy, Handball, Magic, Origami, Fashion, Foreign language learning

Introduction: My name is Lilliana Bartoletti, I am a adventurous, pleasant, shiny, beautiful, handsome, zealous, tasty person who loves writing and wants to share my knowledge and understanding with you.