generate_stan_code_bin {bnns} | R Documentation |
Internal function to generate Stan Code for Binary Response Models
Description
This function generates Stan code for a Bayesian neural network model designed to predict binary 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. The model uses a Bernoulli likelihood for binary outcomes.
Usage
generate_stan_code_bin(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 |
Details
The generated Stan code models a binary response variable using a neural network. The hidden layers apply the specified activation functions, while the output layer applies the logistic function to predict the probability of the binary outcome.
-
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
The output layer uses a logistic transformation (inv_logit
) to constrain
predictions between 0 and 1, which aligns with the Bernoulli likelihood.
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_bin(1, c(10))
cat(stan_code)
# Generate Stan code for two hidden layers with 8 and 4 nodes
stan_code <- generate_stan_code_bin(2, c(8, 4))
cat(stan_code)