R/predict.seinfitR.R

Defines functions predict.seinfitR

Documented in predict.seinfitR

#' Predict method for seinfitR objects
#'
#' This function generates predictions based on a fitted Seinhorst model.
#' @title Predict SeinfitR
#' @param object An object of class `seinfitR` (output from `seinfitR()`).
#' @param newdata Optional. A data frame containing the independent variable for which predictions should be made.
#' If not provided, predictions are made for the original data.
#' @param \dots currently unused.
#'
#' @return A data frame with the independent variable and the corresponding predicted values.
#' @export

predict.seinfitR <- function(object, newdata = NULL, ...) {
  # If newdata is NULL, use the original dataset
  if (is.null(newdata)) {
    newdata <- object$data
  }

  # Ensure the independent variable exists in newdata
  if (!(object$x %in% names(newdata))) {
    stop("New dataset must contain the independent variable: ", object$x)
  }

  new_x <- newdata[[object$x]]

  # Compute predictions
  predictions <- predict(object$fit, newdata = newdata, ...)

  return(data.frame(x = new_x, predicted = predictions))
}

Try the seinfitR package in your browser

Any scripts or data that you put into this service are public.

seinfitR documentation built on April 11, 2025, 5:54 p.m.