R/predict_model.R

Defines functions predict.stAirPol.model

Documented in predict.stAirPol.model

#' predict.stAirPol.model
#'
#'
#'
#' @param spT.model a model as returned by \link{fit_sp_model}
#' @param newdata a dataset with informations of the covariable
#' @param training_set an object generated by \link{get_test_and_training_set},
#' if those an object is delivered, the newdata argument will reduced to the
#' in training_set specified test sensors.
#' @param ... additional arguments
#'
#' @return the newdata with added columns of the prediction calculations
#' @export
#'
#' @examples
#' data("mini_dataset")
#' mini_dataset <- clean_model_data(mini_dataset)
#' formula = value ~ humi + temp + rainhist + windhist +
#'   trafficvol + log(sensor_age)
#' training_set <- get_test_and_training_set(mini_dataset, sampel_size = 0.75,
#'                                           random.seed = 220292)
#' model.gp <- fit_sp_model(data = mini_dataset, formula = formula,
#'                          model = 'GP', training_set = training_set)
#' pred.gp <- predict(model.gp, mini_dataset, training_set)
predict.stAirPol.model <- function(spT.model, newdata, training_set = NULL, ...) {
  if (!is.null(training_set)) {
    newdata <- newdata[sensor_id %in% training_set$test]
  }
  newdata <- data.table(newdata)
  newdata <- newdata[order(sensor_id)][order(timestamp)]
  coords <- unique(newdata[, c('lon', 'lat')])
  class(spT.model) <- class(spT.model)[-1]
  d <- predict(spT.model, newdata = newdata, newcoords = coords, tol = 1e-9999, ...)
  newdata$prediction <- c(d$Mean)
  newdata$sd <- c(d$SD)
  newdata$median <- c(d$Median)
  newdata$low <- c(d$Low)
  newdata$up <- c(d$Up)
  class(newdata) <- c('stAirPol.prediction', class(newdata))
  newdata
}
maxikellerbauer/stAirPol documentation built on May 3, 2019, 3:16 p.m.