Skip to contents

Generates predictions from an ftmglm or ftmlm object based on new input data. This method utilizes the intersecting variables between the model's predictors and the new dataset.

Usage

# S4 method for class 'ftmglm'
predict(object, newdata, type = c("link", "response"), ...)

# S4 method for class 'ftmlm'
predict(object, newdata, s = NULL, ...)

Arguments

object

Object of class inheriting from ftmglm or ftmlm.

newdata

Data frame of new data for prediction. Must contain at least some of the predictors used in the model training.

type

A character string specifying the type of prediction: either "response" for predicted probabilities, or "link" (default) for linear predictors.

...

Additional arguments passed to the predict method.

s

Ridge penalty to apply during flexible reweighting. Default is the stored optimal penalty, or 0 if missing.

Value

A numeric vector of predictions. If type is "link", these are the linear predictors; if "response", these are the probabilities, transformed via the logistic function.

Details

The predict method works by:

  • Identifying variables common to both the model and the new data.

  • Preparing the data by subsetting to these intersecting variables and including an intercept.

  • Inverting the XtWX or XtX matrix, including a ridge penalty or using Moore-Penrose generalized inverse to handle potentially singular matrices.

  • Estimating coefficients and calculating predictions based on the specified type.

It is crucial that newdata contains variables that intersect with the model's predictors. If no intersecting variables are found, the function throws an error.

Examples

if (FALSE) { # \dontrun{
# Load mtcars dataset
data(mtcars)

# Fit a glmnet model
fit <- glmnet::cv.glmnet(as.matrix(mtcars[, c("hp", "wt", "cyl")]), mtcars$am, family = "binomial")

# Create an ftmglm object
ftmglm_model <- createFromGlmnet(fit, as.matrix(mtcars[, c("hp", "wt", "cyl")]))

# Predict on "new" data
new_data <- mtcars[1:10, c("hp", "wt", "cyl")]
predictions <- predict(ftmglm_model, newdata = new_data)
print(predictions)

# Fit a linear model
lm_model <- lm(mpg ~ cyl + hp + wt, data = mtcars)

# Create an ftmlm object
ftmlm_model <- createFromLm(lm_model)

# Predict on "new" data
new_data <- mtcars[1:10, c("hp", "wt", "cyl")]
predictions <- predict(ftmlm_model, newdata = new_data)
print(predictions)
} # }