R/pnn.predone.R

Defines functions pnn.predone

Documented in pnn.predone

#' Calculate the predicted probability for each category of PNN
#'
#' The function \code{pnn.predone} calculates the predicted probability for each category of PNN
#'
#' @param net A PNN object created by pnn.fit()
#' @param x   A vector of input predictors
#'
#' @return A one-row matrix of predicted probabilities
#'
#' @seealso \code{\link{pnn.fit}}
#'
#' @examples
#' data(iris, package = "datasets")
#' Y <- iris[, 5]
#' X <- scale(iris[, 1:4])
#' pnet <- pnn.fit(x = X, y = Y)
#' for (i in seq(5)) print(pnn.predone(pnet, X[i, ]))

pnn.predone <- function(net, x) {

  if (class(net) != "Probabilistic Neural Net") stop("net needs to be a PNN object.", call. = F)

  cn <- ncol(net$y.ind)
  xl <- matrix(rep(x, nrow(net$x)), nrow = nrow(net$x), byrow = TRUE)
  d <- sum(exp(-rowSums((xl - net$x) ^ 2) / (2 * (net$sigma ^ 2))))
  n <- lapply(seq(cn), 
         function(i) sum(net$y.ind[, i] * exp(-rowSums((xl - net$x) ^ 2) / (2 * (net$sigma ^ 2)))))
#  xl <- split(net$x, seq(nrow(net$x)))
#  d <- sum(exp(-Reduce(c, lapply(xl, function(xi) sum((x - xi) ^ 2))) / (2 * (net$sigma ^ 2))))
#  n <- lapply(seq(cn), 
#         function(i) sum(net$y.ind[, i] * exp(-Reduce(c, lapply(xl, function(xi) sum((x - xi) ^ 2))) / (2 * (net$sigma ^ 2)))))
  p <- matrix(unlist(n) / d, ncol = cn)
  colnames(p) <- colnames(net$y.ind) 
  return(p)
}

Try the yap package in your browser

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

yap documentation built on Oct. 26, 2020, 1:06 a.m.