View source: R/make_new_data.R
make_new_data | R Documentation |
This is a convenience function that helps automate the process
of generating predicted data from regression model from a predictor(s). It
is designed to give you the data frame for the predict
method's newdata
argument.
make_new_data(
model,
pred,
pred.values = NULL,
at = NULL,
data = NULL,
center = TRUE,
set.offset = NULL,
num.preds = 100,
...
)
model |
The model (e.g., |
pred |
The name of the focal predictor as a string. This is the variable for which, if you are plotting, you'd likely have along the x-axis (with the dependent variable as the y-axis). |
pred.values |
The values of |
at |
If you want to manually set the values of other variables in the model, do so by providing a named list where the names are the variables and the list values are vectors of the values. This can be useful especially when you are exploring interactions or other conditional predictions. |
data |
The data frame used in fitting the model. Default is NULL, in
which case the data will be retrieved via |
center |
Set numeric covariates to their mean? Default is TRUE. You
may also just provide a vector of names (as strings) of covariates to
center. Note that for |
set.offset |
If the model has an offset, the value to use for the offset variable. Default is NULL, in which case the median of the offset variable is used. |
num.preds |
The number of predictions to generate. Default is 100.
Ignored if |
... |
Extra arguments passed to |
Please bear in mind that this does not generate the predictions. You will
need to do that with a predict
function for your model or another
interface, such as the prediction
package's titular function.
A data frame.
fit <- lm(Income ~ Frost + Illiteracy + Murder, data = as.data.frame(state.x77))
# Basic use
new_data <- make_new_data(fit, pred = "Frost")
# Set covariate to specific value
new_data <- make_new_data(fit, pred = "Frost", at = list(Murder = 5))
# Set covariate to several specific values
new_data <- make_new_data(fit, pred = "Frost", at = list(Murder = c(5, 10, 15)))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.