| Title: | Helper Functions for Orthogonal Polynomials |
|---|---|
| Description: | Tools for reshaping, plotting, and manipulating matrices of orthogonal polynomials. |
| Authors: | Tristan Mahr [aut, cre] (ORCID: <https://orcid.org/0000-0002-8890-5116>) |
| Maintainer: | Tristan Mahr <[email protected]> |
| License: | GPL-3 |
| Version: | 0.0.3 |
| Built: | 2026-05-18 06:18:18 UTC |
| Source: | https://github.com/tjmahr/polypoly |
Add orthogonal polynomial columns to a dataframe
poly_add_columns( .data, .col, degree = 1, prefix = NULL, scale_width = NULL, na_values = c("error", "warn", "allow") )poly_add_columns( .data, .col, degree = 1, prefix = NULL, scale_width = NULL, na_values = c("error", "warn", "allow") )
.data |
a dataframe |
.col |
a bare column name |
degree |
number of polynomial terms to add to the dataframe |
prefix |
prefix for the names to add to the dataframe. default is the
name of |
scale_width |
optionally rescale the dataframe using |
na_values |
How to handle missing values. Default is |
the dataframe with additional columns of orthogonal polynomial terms
of .col
df <- data.frame(time = rep(1:5, 3), y = rnorm(15)) # adds columns "time1", "time2", "time3" poly_add_columns(df, time, degree = 3) # adds columns "t1", "t2", "t3 and rescale poly_add_columns(df, time, degree = 3, prefix = "t", scale_width = 1)df <- data.frame(time = rep(1:5, 3), y = rnorm(15)) # adds columns "time1", "time2", "time3" poly_add_columns(df, time, degree = 3) # adds columns "t1", "t2", "t3 and rescale poly_add_columns(df, time, degree = 3, prefix = "t", scale_width = 1)
Melt a polynomial matrix
poly_melt(x)poly_melt(x)
x |
a matrix created by |
The degree values are returned as a character vector because they
should be treated categorically (as when plotting). Moreover, matrices
made with multiple vectors (e.g., poly(rnorm(10), rnorm(10), degree = 2))
have names that are not numerically meaningful (e.g., 1.0, 2.0, 0.1,
1.1, 0.2),
a tibble::tibble() with three columns: observation (row number of
the matrix), polynomial degree, and value.
m <- poly(rnorm(10), degree = 3) poly_melt(m)m <- poly(rnorm(10), degree = 3) poly_melt(m)
Plot a polynomial matrix
poly_plot(x, by_observation = TRUE, x_col = 1) poly_plot_data(x, by_observation = TRUE, x_col = 1)poly_plot(x, by_observation = TRUE, x_col = 1) poly_plot_data(x, by_observation = TRUE, x_col = 1)
x |
a matrix created by |
by_observation |
whether the x axis should be mapped to the
observation/row number ( |
x_col |
integer indicating which column to plot as the x-axis when
|
a ggplot2::ggplot() plot of the degree terms from the matrix. For
poly_plot_data(), the dataframe used to create the plot is returned
instead.
# Defaults to plotting using the row number as x-axis m <- poly(1:100, degree = 3) poly_plot(m) # Not good because observations were not sorted m2 <- poly(rnorm(100), degree = 3) poly_plot(m2) # Instead set by_observation to FALSE to plot along the degree 1 values poly_plot(m2, by_observation = FALSE) # Get a dataframe instead of plot poly_plot_data(m2, by_observation = FALSE)# Defaults to plotting using the row number as x-axis m <- poly(1:100, degree = 3) poly_plot(m) # Not good because observations were not sorted m2 <- poly(rnorm(100), degree = 3) poly_plot(m2) # Instead set by_observation to FALSE to plot along the degree 1 values poly_plot(m2, by_observation = FALSE) # Get a dataframe instead of plot poly_plot_data(m2, by_observation = FALSE)
Rescale the range of a polynomial matrix
poly_rescale(x, scale_width = 1)poly_rescale(x, scale_width = 1)
x |
a matrix created by |
scale_width |
the desired range (max - min) for the first column of the matrix |
This function strips away the poly class and the coefs
attribute of the matrix. This is because those attributes no longer
describe the transformed matrix.
the rescaled polynomial matrix (as a plain matrix with coefs
attribute removed)
m <- poly(1:10, degree = 4) # Difference between min and max values of first column is 10 scaled <- poly_rescale(m, scale_width = 10) scaled # Rescaled values are still orthogonal zapsmall(cor(scaled))m <- poly(1:10, degree = 4) # Difference between min and max values of first column is 10 scaled <- poly_rescale(m, scale_width = 10) scaled # Rescaled values are still orthogonal zapsmall(cor(scaled))
This package provides helpful functions for orthogonal polynomials created by
stats::poly(). These include plotting poly_plot(), tidying poly_melt(),
rescaling poly_rescale(), and manipulating a dataframe
poly_add_columns().
Tristan Mahr