The “nnet” Function in R

  • Package: nnet

  • Purpose: Fits a neural network model.

  • General class: Machine learning

  • Required argument(s):

    • formula: A formula specifying the model.

    • data: The dataset to be used.

  • Notable optional arguments:

    • size: Number of units in the hidden layer.

    • maxit: Maximum number of iterations.

    • linout: Logical indicating whether to use linear output.

  • Example:

  • # Load the nnet library
    library(nnet)

    # Fit a neural network model using the iris dataset
    model <- nnet(Species ~ ., data = iris, size = 5, maxit = 100)

    # Sanity check to make sure the model is fitting (not for evaluating performance)
    predict(model, iris[,1:4]) |> round(2)

  • This example demonstrates how to use the nnet function from the nnet package to fit a neural network model on the iris dataset. The formula Species ~ . specifies that Species is the response variable and all other columns are predictors. Optional arguments like size and maxit control the size of the hidden layer and the maximum number of iterations, respectively.

Previous
Previous

The “multinom” Function in R

Next
Next

The “alpha” Function in R