From 21fc7154f88c0886c6f55b23fd070dfea439bc85 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Chasset Date: Thu, 16 May 2013 16:18:50 +0200 Subject: [PATCH] Functional package without documentation and tests --- .gitignore | 3 +++ DESCRIPTION | 16 ++++++++++++++++ NAMESPACE | 3 +++ NEWS | 4 ++++ R/create.R | 7 +++++++ R/grnn-package.r | 13 +++++++++++++ R/guess.r | 30 ++++++++++++++++++++++++++++++ R/kernel.R | 20 ++++++++++++++++++++ R/learn.R | 24 ++++++++++++++++++++++++ R/smooth.R | 15 +++++++++++++++ inst/CITATION | 15 +++++++++++++++ man/grnn-package.Rd | 23 +++++++++++++++++++++++ man/guess.Rd | 38 ++++++++++++++++++++++++++++++++++++++ man/learn.Rd | 25 +++++++++++++++++++++++++ man/smooth.Rd | 18 ++++++++++++++++++ 15 files changed, 254 insertions(+) create mode 100644 .gitignore create mode 100644 DESCRIPTION create mode 100644 NAMESPACE create mode 100644 NEWS create mode 100644 R/create.R create mode 100644 R/grnn-package.r create mode 100644 R/guess.r create mode 100644 R/kernel.R create mode 100644 R/learn.R create mode 100644 R/smooth.R create mode 100644 inst/CITATION create mode 100644 man/grnn-package.Rd create mode 100644 man/guess.Rd create mode 100644 man/learn.Rd create mode 100644 man/smooth.Rd diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..807ea25 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.Rproj.user +.Rhistory +.RData diff --git a/DESCRIPTION b/DESCRIPTION new file mode 100644 index 0000000..8ee859e --- /dev/null +++ b/DESCRIPTION @@ -0,0 +1,16 @@ +Package: grnn +Title: General regression neural network +Description: The program GRNN implements the algorithm proposed by Specht + (1991). +URL: http://flow.chasset.net/r-grnn/ +Version: 0.1.0 +Author: Pierre-Olivier Chasset +Maintainer: Pierre-Olivier Chasset +License: AGPL +Collate: + 'create.R' + 'grnn-package.r' + 'guess.r' + 'kernel.R' + 'learn.R' + 'smooth.R' diff --git a/NAMESPACE b/NAMESPACE new file mode 100644 index 0000000..5850f76 --- /dev/null +++ b/NAMESPACE @@ -0,0 +1,3 @@ +export(guess) +export(learn) +export(smooth) diff --git a/NEWS b/NEWS new file mode 100644 index 0000000..98e9af7 --- /dev/null +++ b/NEWS @@ -0,0 +1,4 @@ +grnn 0.1 +======== + +* Package structure implemented. \ No newline at end of file diff --git a/R/create.R b/R/create.R new file mode 100644 index 0000000..f05864f --- /dev/null +++ b/R/create.R @@ -0,0 +1,7 @@ +create.grnn <- function() { + nn <- list( + model="General regression neural network", + set=NULL + ) + return(nn) +} diff --git a/R/grnn-package.r b/R/grnn-package.r new file mode 100644 index 0000000..31a4e68 --- /dev/null +++ b/R/grnn-package.r @@ -0,0 +1,13 @@ +#' GRNN +#' +#' General regression neural network. +#' +#' The program GRNN implements the algorithm proposed by Specht (1991). +#' +#' @author Pierre-Olivier Chasset +#' @docType package +#' @keywords Neural network, Regression +#' @references Specht D.F. (1991). A general regression neural network. IEEE Transactions on Neural Networks, 2(6):568-576. +#' @aliases grnn +#' @name grnn-package +NULL diff --git a/R/guess.r b/R/guess.r new file mode 100644 index 0000000..9345eb4 --- /dev/null +++ b/R/guess.r @@ -0,0 +1,30 @@ +#' Guess +#' +#' Infers the value of a new observation. +#' +#' @examples +#' n <- 100 +#' set.seed(1) +#' x <- runif(n, -2, 2) +#' y0 <- x^3 +#' epsilon <- rnorm(n, 0, .1) +#' y <- y0 + epsilon +#' grnn <- learn(data.frame(y,x)) +#' grnn <- smooth(grnn, sigma=0.1) +#' guess(grnn, -2) +#' guess(grnn, -1) +#' guess(grnn, -0.2) +#' guess(grnn, -0.1) +#' guess(grnn, 0) +#' guess(grnn, 0.1) +#' guess(grnn, 0.2) +#' guess(grnn, 1) +#' guess(grnn, 2) +#' @param nn A trained and smoothed General regression neural network. +#' @param X A vector describing a new observation. +#' @seealso \code{\link{grnn-package}} +#' @export +guess <- function(nn, X) { + results <- Y(nn$Xa, nn$Ya, X, nn$sigma) + return(results) +} diff --git a/R/kernel.R b/R/kernel.R new file mode 100644 index 0000000..172f7e6 --- /dev/null +++ b/R/kernel.R @@ -0,0 +1,20 @@ +ds <- function(Xa, X) { + value <- (X - Xa) %*% t(X - Xa) + return(as.numeric(value)) +} + +pattern <- function(Xa, X, sigma) { + res <- exp( - ds(Xa, X) / (2 * sigma ^ 2) ) + return(as.numeric(res)) +} + +patterns <- function(Xa, X, sigma) + apply(Xa, 1, pattern, X, sigma) + +Y <- function(Xa, Ya, X, sigma) { + p <- length(X) # Dimensionality of measurement space + m <- length(Xa[,1]) # Total number of training patterns from category A + patterns1 <- patterns(Xa, X, sigma) + f <- sum(Ya * patterns1) / sum(patterns1) + return(f) +} diff --git a/R/learn.R b/R/learn.R new file mode 100644 index 0000000..fd289cb --- /dev/null +++ b/R/learn.R @@ -0,0 +1,24 @@ +#' Learn +#' +#' Create or update a General regression neural network. +#' +#' @param set Data frame representing the training set. The first column is used to define the category of each observation (set \code{category.column} if it is not the case). +#' @param nn A General regression neural network with or without training. +#' @param variable.column The field number of the variable (1 by default). +#' @seealso \code{\link{grnn-package}} +#' @export +learn <- function(set, nn, variable.column=1) { + if(missing(set)) stop("Set is missing!") + if(missing(nn)) nn <- create.grnn() + if(is.null(nn$set)) { + nn$variable.column <- variable.column + nn$set <- set + } else { + nn$set <- rbind(nn$set, set) + } + nn$Xa <- as.matrix(nn$set[,-nn$variable.column]) + nn$Ya <- as.matrix(nn$set[,nn$variable.column]) + nn$k <- length(nn$set[1,]) - 1 + nn$n <- length(nn$set[,1]) + return(nn) +} diff --git a/R/smooth.R b/R/smooth.R new file mode 100644 index 0000000..c602afa --- /dev/null +++ b/R/smooth.R @@ -0,0 +1,15 @@ +#' Smooth +#' +#' Smooth a General regression neural network. +#' +#' @param nn A trained General regression neural network. +#' @param sigma A scalar. +#' @seealso \code{\link{grnn-package}} +#' @export +smooth <- function(nn, sigma) { + if(!missing(sigma)) { + nn$sigma <- sigma + return(nn) + } + stop() +} diff --git a/inst/CITATION b/inst/CITATION new file mode 100644 index 0000000..9fcf6ed --- /dev/null +++ b/inst/CITATION @@ -0,0 +1,15 @@ +bibentry("Manual", + title = "GRNN: General regression neural network for the statistical software R", + author = person("P.-O. Chasset"), + organization = "Independant scientist", + address = "Nancy, France", + year = 2013, + note = "Software", + url = "http://flow.chasset.net/r-grnn/", + + mheader = "To cite the library GRNN in publications use:", + + mfooter = + paste("We have invested a lot of time and effort in creating GRNN,", + "please cite it when using it for data analysis.", sep = " ") +) diff --git a/man/grnn-package.Rd b/man/grnn-package.Rd new file mode 100644 index 0000000..3b24001 --- /dev/null +++ b/man/grnn-package.Rd @@ -0,0 +1,23 @@ +\docType{package} +\name{grnn-package} +\alias{grnn} +\alias{grnn-package} +\title{GRNN} +\description{ + General regression neural network. +} +\details{ + The program GRNN implements the algorithm proposed by + Specht (1991). +} +\author{ + Pierre-Olivier Chasset +} +\references{ + Specht D.F. (1991). A general regression neural network. + IEEE Transactions on Neural Networks, 2(6):568-576. +} +\keyword{Neural} +\keyword{Regression} +\keyword{network,} + diff --git a/man/guess.Rd b/man/guess.Rd new file mode 100644 index 0000000..d17558e --- /dev/null +++ b/man/guess.Rd @@ -0,0 +1,38 @@ +\name{guess} +\alias{guess} +\title{Guess} +\usage{ + guess(nn, X) +} +\arguments{ + \item{nn}{A trained and smoothed General regression + neural network.} + + \item{X}{A vector describing a new observation.} +} +\description{ + Infers the value of a new observation. +} +\examples{ +n <- 100 +set.seed(1) +x <- runif(n, -2, 2) +y0 <- x^3 +epsilon <- rnorm(n, 0, .1) +y <- y0 + epsilon +grnn <- learn(data.frame(y,x)) +grnn <- smooth(grnn, sigma=0.1) +guess(grnn, -2) +guess(grnn, -1) +guess(grnn, -0.2) +guess(grnn, -0.1) +guess(grnn, 0) +guess(grnn, 0.1) +guess(grnn, 0.2) +guess(grnn, 1) +guess(grnn, 2) +} +\seealso{ + \code{\link{grnn-package}} +} + diff --git a/man/learn.Rd b/man/learn.Rd new file mode 100644 index 0000000..25870aa --- /dev/null +++ b/man/learn.Rd @@ -0,0 +1,25 @@ +\name{learn} +\alias{learn} +\title{Learn} +\usage{ + learn(set, nn, variable.column = 1) +} +\arguments{ + \item{set}{Data frame representing the training set. The + first column is used to define the category of each + observation (set \code{category.column} if it is not the + case).} + + \item{nn}{A General regression neural network with or + without training.} + + \item{variable.column}{The field number of the variable + (1 by default).} +} +\description{ + Create or update a General regression neural network. +} +\seealso{ + \code{\link{grnn-package}} +} + diff --git a/man/smooth.Rd b/man/smooth.Rd new file mode 100644 index 0000000..914b1e8 --- /dev/null +++ b/man/smooth.Rd @@ -0,0 +1,18 @@ +\name{smooth} +\alias{smooth} +\title{Smooth} +\usage{ + smooth(nn, sigma) +} +\arguments{ + \item{nn}{A trained General regression neural network.} + + \item{sigma}{A scalar.} +} +\description{ + Smooth a General regression neural network. +} +\seealso{ + \code{\link{grnn-package}} +} +