I am building a ANN to predict total healthcare costs (therefore a continuous variable) using a range of input variables such as age, gender, insurance coverage and number of chronic conditions. I have taken the following steps using R's neuralnet package:
I first standardised the values so all independent and the dependent variable values lie between 0 - 1 (using min-max rule)
I create a model matrix with the independent and dependent variables and also created a formula for the neural net
df_matrix <- model.matrix(~ total_cost_stand + sex + agestand + race + inscoverage + edu_year_stand + num_cc_stand,data = df)[,-1] f <- as.formula(paste(c(colnames(df_matrix))[1], paste(c(colnames(df1_matrix[,c(2:ncol(df_matrix))])), collapse = "+"), sep=" ~ "))
I created training and testing dataframes
smp_siz <- floor(0.75*nrow(df_matrix)) set.seed(465) train_ind <- sample(seq_len(nrow(df_matrix)),size = smp_siz) train_df <- as.data.frame(df_matrix[train_ind, ]) test_df <- as.data.frame(df_matrix[-train_ind, ])
Given I wanted to use ReLU as the activation function I created the following function to put into neural net
relu_copy <- function(x) ifelse(x>=0, x, 0)
Now I run the neuralnet
nn <- neuralnet::neuralnet(f , data = train_df , hidden = c(2,2) , act.fct = relu_copy, threshold = 0.01 , err.fct = "sse", algorithm = "rprop+" , linear.output = TRUE)
However, a few of my predicted outputs are negative, which shouldn't be possible given a) all my data was standardised between 0 and 1 and b) I used a ReLU activation function, which should transform all negative values to 0.
output <- neuralnet::compute(nn, test_df[,-1])
predict <- output$net.result * (max(df$total_cost) - min(df$total_cost)) + min(df$total_cost)
min(predict)
[1] -1447.274
I would be very grateful for any tips/explanations as to why I am getting negative prediction results.
Thanks!