1

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:

  1. I first standardised the values so all independent and the dependent variable values lie between 0 - 1 (using min-max rule)

  2. 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=" ~ "))
    
  3. 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, ])
    
  4. 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)
    
  5. 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!

Isobel M
  • 55
  • 1
  • 6
  • Can you provide some data to make it reproducible? – Christoph Aug 07 '20 at 08:51
  • Hi @Christoph - Unfortunately I am unable to share any real, raw data. Would mock data make sense? Apologies, I am relatively new to SO. – Isobel M Aug 07 '20 at 09:03
  • Yes, just have a look at 'minimal reproducible example'. In many cases this helps to understand whats going on. Link https://stackoverflow.com/help/minimal-reproducible-example – Christoph Aug 07 '20 at 09:39

0 Answers0