下标分配中不允许 R 预测 NA

R predict NAs are not allowed in subscripted assignments

我想使用预测功能。我有两组数据框(一组用于校准,第二组用于验证)。 下面是两组数据:

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    

在我的程序中,我首先定义预测,然后

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

discrim 似乎工作正常(如果我绘制 discrim)但是当我计算 pred 时,我收到此消息:

pred <- predict(discrim,df_valid)

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

你能帮我解决这个问题吗?

这里有一些关于数据的附加信息:

formula(discrim)规定:

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" 

关于 df_valid 和 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" ...

感谢@CathG 和@Roman。问题是 COND 的定义应该是一个因素而不是一个字符。