简体   繁体   中英

Error while running DFA in R at the predict function

I'm attempting to run a cv DFA on a set of species X site data (species are columns, sites are rows) with a grouping row named "ZONE".

I'm using a stock script I've successfully used before, but now I'm getting a new error from the predict function that I can't make heads or tails of.

My code is simply:

data2.lda<-lda(ZONE~SP1+SP2+SP3+SP4+SP5+SP6+SP7+SP8+SP9+SP10+SP11+SP12+SP13+SP14+SP15
,data=data2.x, Cna.action="na.omit",CV=TRUE)

list(data2.lda)

data2.lda.p<-predict(data2.lda,newdata=data2.lda.x(,c[2:17]))$class
data2.lda.p

the error I receive is:

Error in UseMethod("predict") : no applicable method for 'predict' applied to an object of class "list"

My data are in the same form as previous uses of this code. Where did I go wrong? Any help is appreciated, thanks!

UPDATE: I've figured out that the issue involves the cross validation portion of the code. Are there additional rules for cross validating an LDA that I'm missing when it comes to coding in R?

Your problem is that predict requires a model object for its first argument. When you run lda with the CV=T option, it returns a list object not a model object. The lda documentation says

If CV = TRUE the return value is a list with components class, the MAP classification (a factor), and posterior, posterior probabilities for the classes.

Otherwise it is an object of class "lda" containing the following components:

As per PCantalupo's answer, I managed to arrive at my goal. The cross validation procedure needs to be applied during the predict model, not the original model. The functional code is:

data2.lda<-lda(ZONE~SP1+SP2+SP3+SP4+SP5+SP6+SP7+SP8+SP9+SP10+SP11+SP12+SP13+SP14+SP15
,data=data2.x, Cna.action="na.omit")

list(data2.lda)

data2.lda.p<-predict(data2.lda,CV=TRUE,newdata=data1[c(2:17)])$class
data2.lda.p
tab<-table(data2.lda.p,data2[,1])
tab
summary(table(data2.lda.p,data2[,1]))
diag(prop.table(tab,1))
sum(diag(prop.table(tab)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM