R/prediction_knnreg.R

Defines functions prediction.knnreg

Documented in prediction.knnreg

#' @rdname prediction
#' @export
prediction.knnreg <- function(model, data, at = NULL, calculate_se = FALSE, ...) {
    
    # extract predicted values
    if (missing(data) || is.null(data)) {
        pred <- make_data_frame(fitted = predict(object = model, ...)[,1L])
    } else {
        # setup data
        if (is.null(at)) {
            out <- data
        } else {
            out <- build_datalist(data, at = at, as.data.frame = TRUE)
            at_specification <- attr(out, "at_specification")
        }
        pred <- make_data_frame(fitted = predict(model, newdata = data, ...))
    }
    pred[["se.fitted"]] <- NA_real_
    
    # variance(s) of average predictions
    vc <- NA_real_
    
    # output
    structure(pred, 
              class = c("prediction", "data.frame"),
              at = if (is.null(at)) at else at_specification,
              type = NA_character_,
              call = if ("call" %in% names(model)) model[["call"]] else NULL,
              model_class = class(model),
              row.names = seq_len(nrow(pred)),
              vcov = vc,
              jacobian = NULL,
              weighted = FALSE)
}
leeper/prediction documentation built on Jan. 1, 2020, 6:10 p.m.