View source: R/S03_Utilities.R
term_prep | R Documentation |
Function that takes a list specifying base variables and new terms to create and updates a data frame appropriately. Allows for easy pre-processing of multiple data frames in the same manner for analyses (e.g., when prepping training and validation sets for data).
term_prep(x, settings, output = "x")
x |
A data frame. |
settings |
A named list, in the form:
|
output |
A character string, the type of output to return,
where |
Either a data frame, a list of settings, or a list with both the data frame and list of settings.
data("mtcars")
#' Split into two sets of data
x1 <- mtcars[ seq( 1, 32, 2), ] # Odd
x2 <- mtcars[ seq( 1, 32, 2), ] # Even
lst <- list(
new = list(
mpg = list(
outcome = term_new(
label = 'Miles per gallon'
)
),
hp = list(
log_hp = term_new(
label = 'Log(Horsepower)',
transformation = 'log(x)',
scale = TRUE,
order = c( 't', 's' )
)
),
vs = list(
vs_0v1 = term_new(
label = 'Engine: V-shaped vs. straight',
coding = term_coding_effect( 1, 0 ),
scale = TRUE,
order = c( 'c', 's' )
)
),
am = list(
am_0v1 = term_new(
label = 'Transmission: Automatic vs. manual',
coding = term_coding_effect( 1, 0 ),
scale = TRUE,
order = c( 'c', 's' )
)
)
),
combo = list(
vs_x_am = term_combo(
combine = c( vs = 'vs_0v1', am = 'am_0v1' ),
transformation = 'vs*am',
scale = TRUE
)
)
)
# Add info on mean/SD from 'x1' data
lst <- x1 |> term_prep( lst, output = 'settings' )
# Update 'x1' and 'x2'
x1 <- x1 |> term_prep( lst )
x2 <- x2 |> term_prep( lst )
# Fit 'x1' data
fit <- lm( outcome ~ log_hp + vs_0v1 + am_0v1 + vs_x_am, data = x1)
# Predict 'x2' data
predict( fit, newdata = x2 )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.