0
我想使用預測函數。我有兩組數據幀(一個用於校準,第二個用於驗證)。 這裏有兩組數據:R預測NAs在下標指配中不被允許
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)
錯誤mindist [1] < - ndist [1]: NA的不是下標分配
你能幫我解決這個問題可以嗎?
這裏對數據的一些附加信息:
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" ...
一個優秀的帖子[這裏](http://stackoverflow.com/questions/30172523/error-in-predicting-test-data-when-apply-prediction-fda-model-flexible-discrimi)有同樣的問題。它可能會幫助你 – etienne
「公式(descrim)」返回什麼? –
檢查這[post](http://stackoverflow.com/questions/30172523/error-in-predicting-test-data-when-apply-prediction-fda-model-flexible-discrimi),它可能會有所幫助。 – Prradep