Skip to content

Instantly share code, notes, and snippets.

@BioSciEconomist
Created November 6, 2016 17:23
Show Gist options
  • Save BioSciEconomist/d2d4f28aeaa9b74496076c4dba18ecf6 to your computer and use it in GitHub Desktop.
Save BioSciEconomist/d2d4f28aeaa9b74496076c4dba18ecf6 to your computer and use it in GitHub Desktop.
R Code Example for Neural Networks
# ------------------------------------------------------------------
# |PROGRAM NAME: NEURALNET_PKG_R
# |DATE: 12/3/10
# |CREATED BY: MATT BOGARD
# |PROJECT FILE: http://econometricsense.blogspot.com/2010/12/r-code-example-for-neural-networks.html
# |----------------------------------------------------------------
# | PURPOSE: DEMO OF THE 'neuralnet' PACKAGE AND OUTPUT INTERPRETATION
# |
# | ADAPTED FROM: neuralnet: Training of Neural Networks
# | by Frauke Günther and Stefan Fritsch The R Journal Vol. 2/1, June 2010
# | ISSN 2073-4859 (LOCATED: P:\TOOLS AND REFERENCES (Copy)\R References\Neural Networks
# |
# |
# |------------------------------------------------------------------
# |DATA USED: 'infert' FROM THE 'datasets' LIBRARY
# |------------------------------------------------------------------
# |CONTENTS:
# |
# | PART 1: get the data
# | PART 2: train the network
# | PART 3:
# | PART 4:
# | PART 5:
# |------------------------------------------------------------------
# |COMMENTS:
# |
# |-----------------------------------------------------------------
# |UPDATES:
# |
# |
# |------------------------------------------------------------------
# *------------------------------------------------------------------*
# | get the data
# *------------------------------------------------------------------*
library(datasets)
names(infert)
# *------------------------------------------------------------------*
# | train the network
# *------------------------------------------------------------------*
library(neuralnet)
nn <- neuralnet(
case~age+parity+induced+spontaneous,
data=infert, hidden=2, err.fct="ce",
linear.output=FALSE)
# *------------------------------------------------------------------*
# | output training results
# *------------------------------------------------------------------*
# basic
nn
# reults options
names(nn)
# result matrix
nn$result.matrix
# The given data is saved in nn$covariate and
# nn$response as well as in nn$data for the whole data
# set inclusive non-used variables. The output of the
# neural network, i.e. the fitted values o(x), is provided
# by nn$net.result:
out <- cbind(nn$covariate,nn$net.result[[1]])
dimnames(out) <- list(NULL, c("age", "parity","induced","spontaneous","nn-output"))
head(out)
# generalized weights
# The generalized weight expresses the effect of each
# ovariate xi and thus has an analogous interpretation
# as the ith regression parameter in regression models.
# However, the generalized weight depends on all
# other covariates. Its distribution indicates whether
# the effect of the covariate is linear since a small variance
# suggests a linear effect
# The columns refer to the four covariates age (j =
# 1), parity (j = 2), induced (j = 3), and spontaneous (j=4)
head(nn$generalized.weights[[1]])
# visualization
plot(nn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment