generate_stan_code_cont: Internal function to generate Stan Code for Continuous...

View source: R/generate_stan_code.R

generate_stan_code_contR Documentation

Internal function to generate Stan Code for Continuous Response Models

Description

This function generates Stan code for a Bayesian neural network model designed to predict continuous response variables. The Stan code is dynamically constructed based on the specified number of hidden layers and nodes per layer. It supports various activation functions for the hidden layers, including tanh, sigmoid, softplus and relu.

Usage

generate_stan_code_cont(num_layers, nodes)

Arguments

num_layers

An integer specifying the number of hidden layers in the neural network.

nodes

A vector of integers, where each element specifies the number of nodes in the corresponding hidden layer. The length of the vector must match num_layers.

Details

The generated Stan code models a continuous response variable using a neural network. The hidden layers apply the specified activation functions, while the output layer performs a linear transformation to predict the response. The likelihood assumes normally distributed residuals.

  • For one hidden layer: The function simplifies the Stan code structure.

  • For multiple hidden layers: The code dynamically includes additional layers based on the input arguments.

Supported activation functions for the hidden layers:

  • 1: Tanh

  • 2: Sigmoid

  • 3: Softplus

  • 4: ReLU

  • 5: linear

Value

A character string containing the Stan code for the specified Bayesian neural network model. The Stan model includes data, parameters, transformed parameters, and model blocks. The code is adjusted based on whether the network has one or multiple hidden layers.

Examples

# Generate Stan code for a single hidden layer with 10 nodes
stan_code <- generate_stan_code_cont(1, c(10))
cat(stan_code)

# Generate Stan code for two hidden layers with 8 and 4 nodes
stan_code <- generate_stan_code_cont(2, c(8, 4))
cat(stan_code)


bnns documentation built on April 3, 2025, 6:12 p.m.