1

I can't get why the testing of dataset is not working in R neural networks (nnet package).

I have two datasets with similar structures - for training (trainset, 17 cases) and prediction (testset, 9 cases). Each dataset has columns: Age, Gender, Height, Weight. In the testing dataset the age is unknown (NaN).

The formula for training is obtained successfully below:

library(nnet)
trainednetwork<-nnet(age~gender+emLength+action5cnt,trainset, size=17) 

Anyway, if I try to use test dataset for prediction in the next string of the code,

prediction<-predict(trainednetwork,testset)

I get mistake "No component terms, no attribute". Can anyone help?

The data (obtained with dput() function):

  • testset:

    structure(list(
        age = c(NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
            NA_integer_, NA_integer_, NA_integer_, NA_integer_), 
        gender = structure(
            c(2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L), 
            .Label = c("f", "m"), 
            class = "factor"), 
        Height= c(9L, 11L, 9L, 11L, 9L, 11L, 9L, 11L, 9L), 
        Weight= c(1L, 41L, 2L, 1L, 2L, 29L, 12L, 6L, 12L)), 
        .Names = c("age", "gender", "Height", "Weight"), 
        class = "data.frame", 
        row.names = c(NA, 9L))
    
  • trainset:

    structure(list(
        age = c(43L, 35L, 22L, 28L, 20L, 47L, 41L, 23L, 
            42L, 27L, 22L, 60L, 62L, 47L, 42L, 26L, 54L), 
        gender = structure(
            c(2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 
                2L, 2L, 2L, 2L, 2L), 
            .Label = c("f", "m"), 
            class = "factor"), 
        Height= c(7L, 9L, 11L, 11L, 11L, 9L, 11L, 9L, 23L, 9L, 
            9L, 9L, 10L, 7L, 7L, 11L, 7L), 
        Weight= c(2L, 2L, 9L, 9L, 28L, 8L, 6L, 3L, 1L, 2L, 40L, 
            1L, 9L, 1L, 7L, 4L, 35L)), 
        .Names = c("age", "gender", "Height", "Weight"), 
        class = "data.frame", 
        row.names = c(NA, 17L))
    
Illya K
  • 319
  • 1
  • 3
  • 15
  • 1
    Can you make your problem reproducible? http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Roman Luštrik Sep 13 '13 at 12:00
  • 1
    It seems, that nnet doesn't work with categorial data. Excluding gender data from analysis led to making neural network work. – Illya K Sep 13 '13 at 13:09
  • 1
    You could have transformed the gender data into numbers and declared them as `factor` in order to include them into the modeling. –  Jan 18 '14 at 14:56

1 Answers1

0

I think in the R neuralnet package the command to use for prediction is "compute", not predict, which is very confusing. A

AJGronevelt
  • 551
  • 1
  • 6
  • 22