mistnet: Build and fit a neural network with random effects

Description Usage Arguments Value Examples

Description

Build and fit a neural network with random effects

Usage

1
2
3
mistnet(x, y, n_z, layers, error_distribution,
  z_prior = make_distribution("NO", mu = 0, sigma = 1), fit = TRUE,
  mistnet_optimizer = mistnet_fit_optimx, ...)

Arguments

x

A numeric matrix of predictor variables

y

A numeric or integer matrix of response variables

n_z,

The number of latent random variables to include as predictors alongside x

layers

A list of layer objects. Note that n_nodes in the final layer must match ncol(y).

error_distribution

An distribution object determining the error distribution for the response variables y.

z_prior

A distribution object (standard Gaussian by default) determining the prior on the latent variables.

fit

Logical. Should the model be fitted or should an untrained model be returned? Defaults to TRUE.

mistnet_optimizer

passed to mistnet_fit. By default, models are fitted using mistnet_fit_optimx using method = "L-BFGS-B".

...

Additional arguments to mistnet_fit

Value

An object of class network and subclass mistnet_network. This object will contain the original x and y matrices, a list of adjustable parameters (par_list), [[etc.]]

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
set.seed(1)

# Load data from the `vegan` package
data(mite, mite.env, package = "vegan")

# x is a matrix of environmental predictors
x = scale(model.matrix(~., data = mite.env)[, -1])

# y is a matrix of abundances (counts) for 35 species of mites
y = as.matrix(mite)

# Fit a neural network with one hidden layer of 10 nodes and an elu
# activation function. The response variable has a Poisson distribution
# with a log link (exp_activator). The prior distributions for each layer
# are each standard normal distributions, and two latent variables are used.
net = mistnet(
   x = x,
   y = y,
   n_z = 2,
   layers = list(
     layer(
       activator = elu_activator,
       n_nodes = 10,
       weight_prior = make_distribution("NO", mu = 0, sigma = 1)
     ),
     layer(
       activator = exp_activator,
       n_nodes = ncol(y),
       weight_prior = make_distribution("NO", mu = 0, sigma = 1)
     )
   ),
   error_distribution = make_distribution("PO")
)

print(net)

# show the model's predictions for each layer
str(feedforward(net, par = unlist(net$par_list)))

# Calculate the log-likelihood for each observation under the fitted model
log_prob(net, par = unlist(net$par_list), include_penalties = FALSE)

# Include penalty terms from the prior to calculate the log-posterior instead
log_prob(net, par = unlist(net$par_list), include_penalties = TRUE)

davharris/mistnet2 documentation built on May 14, 2019, 9:28 p.m.