2

So I'm working with this dataset

tibble [1,000 x 17] (S3: tbl_df/tbl/data.frame)
 $ Solde         : num [1:1000] 1 2 4 1 1 4 4 2 4 2 ...
 $ Duree         : num [1:1000] 6 48 12 42 24 36 24 36 12 30 ...
 $ Historique    : num [1:1000] 4 2 4 2 3 2 2 2 2 4 ...
 $ Motif         : num [1:1000] 3 3 6 2 0 6 2 1 3 0 ...
 $ Montant       : num [1:1000] 1169 5951 2096 7882 4870 ...
 $ Epargne       : num [1:1000] 5 1 1 1 1 5 3 1 4 1 ...
 $ Employe_depuis: num [1:1000] 5 3 4 4 3 3 5 3 4 1 ...
 $ Statut_sexe   : num [1:1000] 3 2 3 3 3 3 3 3 1 4 ...
 $ Debit         : num [1:1000] 1 1 1 3 1 1 1 1 1 1 ...
 $ Residence     : num [1:1000] 4 2 3 4 4 4 4 2 4 2 ...
 $ Age           : num [1:1000] 67 22 49 45 53 35 53 35 61 28 ...
 $ Logement      : num [1:1000] 2 2 2 3 3 3 2 1 2 2 ...
 $ n_credit      : num [1:1000] 2 1 1 1 2 1 1 1 1 2 ...
 $ Emploi        : num [1:1000] 3 3 2 3 3 2 3 4 2 4 ...
 $ n_pers        : num [1:1000] 1 1 2 2 2 2 1 1 1 1 ...
 $ Statut        : num [1:1000] 1 2 1 1 2 1 1 1 1 2 ...
 $ y             : num [1:1000] 1 0 1 1 0 1 1 1 1 0 ...

I'm trying to build a model that predicts the variable Y.
To do that I made this function

modele_nnet=function(base,p){
  library(nnet)
  #Echantillonage
  
  train_id=createDataPartition(base$y,p)
  data_train=base[train_id$Resample1,]#Base Train
  data_test=base[-train_id$Resample1,]
  attach(base)
  nnet <-nnet(y~Solde+Duree+Historique+Motif+Epargne+Employe_depuis+Statut_sexe+Debit+Residence+Age+Montant+Logement+n_credit+n_pers+Emploi,data=data_train,family=binomial,size=2)
  return(nnet)
  
}

Then when trying to use a new observation to predict Y I coded this :

newdata=c(Solde=3,Duree=60,Historique=4,Motif=1,Montant=2040,Epargne=5,Employe_depuis=5,Statut_sexe=3,Debit=1,Residence=4,Age=22,Logement=2,n_credit=2,Emploi=3,n_pers=1)

nnet_mod=modele_nnet(base,0.8)
predict(nnet_mod,newdata)

but I get this error

predict(nnet_mod,newdata)
Error in z[keep, ] <- matrix(.C(VR_nntest, as.integer(ntr), as.double(x),  : 
  NAs are not allowed in subscripted assignments
In addition: Warning message:
'newdata' had 15 rows but variables found have 1000 rows 

I don't understand how to fix it, is the problem with the model itself? or the prediction function?

wageeh
  • 13
  • 1
  • 5
  • 18

1 Answers1

0

The problem appears to be how you are specifying your new data. The partitioning and model look good. Like @RuiBarradas suggests, specify your new data as a dataframe, like I do below.

Data:

base <- data.frame(
  Solde = sample(1:5, size = 100, replace = TRUE),
  Duree = sample(1:100, size = 100, replace = TRUE),
  Historique = sample(1:5, size = 100, replace = TRUE),
  y = sample(0:1, size = 100, replace = TRUE))

newdat <- data.frame(Solde = 3, Duree = 60, Historique = 4)

Fit model:

library(nnet)
library(caret)

modele_nnet <- function(base, p){

  train_id=caret::createDataPartition(base$y, p)
  data_train=base[train_id$Resample1,]
  data_test=base[-train_id$Resample1,]
  
  nnet_mod <-nnet(y~Solde+Duree+Historique,
              data=data_train, family=binomial, size=2)
  
  return(nnet_mod)
}

my_model <- modele_nnet(base, 0.8)
predict(my_model, newdata = newdat)
Skaqqs
  • 4,010
  • 1
  • 7
  • 21