2
$\begingroup$

The goal is to run Poisson regression for neural networks (multi-layer perceptron) in R.

I am currently using the neuralnet package in R.

I have read Is there an R package which uses neural networks to explicitly model count data? and http://www.mathstat.dal.ca/~hgu/Neural%20Comput%20&%20Applic.pdf. I have figured out that I should change the error function to err.fct = function(x, y) { -(-x+y*log(x))} (for Poisson).

Code:

fit <- neuralnet(nclaims ~ age_ph + gender + tar_region + weight + age_car + ptw, data = df, hidden = 1, err.fct = function(x, y) { -(-x+y*log(x))}, act.fct = "logistic").

I know that act.fct = "logistic" specifies the sigmoid function in the hidden layer, but how can I change to the exponential function in the output layer?

$\endgroup$
  • $\begingroup$ This question is a bit old and I have not used neuralnet much, but in the documentation the function neuralnet has an argument called linear.output. If you set linear.output to true, the activation function is not applied to the output layer and you are left with just XB the linear predictor as your output, otherwise the act.fun is applied to the output layer mapping your predictions to [0,1] which is clearly not the support of a Poisson rate parameter. Linear.output = TRUE by default so one would think that all you need to do is exponetiate your vector of predictions...i.e. $\endgroup$ – aranglol Jul 10 '19 at 19:29
  • $\begingroup$ Apply exp(x) to your vector of predictions. $\endgroup$ – aranglol Jul 10 '19 at 19:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.