0

I would like to use the predict function. I have two sets of data frames (one for calibration, the second for validation). Here are the two data sets :

head(df_calib)
#      A   B   C   D       COND
#1     0  11  11   9          A    
#2     5   2   5  19          A    
#3     4   3  10  14          A    
#4    18  13   0   0          B    
#5    22   9   0   0          C    
#6     4   9   2  16          B    

head(df_valid)
#     A   B   C   D       COND    
#1   14  16   1   0          A    
#2   20   9   2   0          A    
#3   16   6   9   0          A    
#4   16   2  11   2          A    
#5    4   8  14   5          C    
#6    4   3  13  11          C    

In my program I first define the predict and then I

library(mda)
discrim <- fda(COND~ ., data=df_calib)
pred <- predict(discrim,df_valid)

discrim seems to work fine (if I plot discrim) but when I calculate pred, I get this message :

pred <- predict(discrim,df_valid)

Error in mindist[l] <- ndist[l] : NAs are not allowed in subscripted assignments

Could you help me to solve that problem ?

Here some additional information about the data :

formula(discrim) provides that:

COND ~ A + B + C + D
attr(,"variables")
#list(COND, A, B, C, D)
attr(,"factors")
#     A B C D
#COND 0 0 0 0
#A    1 0 0 0
#B    0 1 0 0
#C    0 0 1 0
#D    0 0 0 1
attr(,"term.labels")
#[1] "A" "B" "C" "D"
attr(,"order")
#[1] 1 1 1 1
attr(,"intercept")
#[1] 1
attr(,"response")
#[1] 1
attr(,"predvars")
#list(COND, A, B, C, D)
attr(,"dataClasses")
# COND         A         B         C         D 
#"character"   "numeric"   "numeric"   "numeric"   "numeric" 

Info about df_valid and df_calib:

str(df_valid)
#'data.frame':  26 obs. of  5 variables:
# $ A       : num  0 0 0 0 8 15 17 19 18 14 ...
# $ B       : num  17 15 8 7 2 2 3 5 5 4 ...
# $ C       : num  2 2 2 2 14 12 6 3 2 1 ...
# $ D       : num  12 14 21 22 7 2 5 4 6 12 ...
# $ COND: chr [1:26(1d)] "A" "A" "A" "A" ...

str(df_calib)
#'data.frame':  520 obs. of  5 variables:
# $ A       : num  0 5 4 18 22 4 1 9 12 15 ...
# $ B       : num  11 2 3 13 9 9 3 1 3 15 ...
# $ C       : num  11 5 10 0 0 2 11 5 0 1 ...
# $ D       : num  9 19 14 0 0 16 16 16 16 0 ...
# $ COND: chr [1:520(1d)] "A" "A" "A" "B" ...
Cath
  • 23,906
  • 5
  • 52
  • 86
Titosh
  • 1
  • 1
  • 3

1 Answers1

0

Thanks to @CathG and @Roman. The problem was the definition of COND that should be a factor not a character.

Titosh
  • 1
  • 1
  • 3