2012-09-19 26 views

回答

4

您正在查找的是作爲"lda"(參見?predict.lda)對象的predict()方法的一部分計算得出的。它被返回作爲由predict(z)產生的對象的分量x

## follow example from ?lda 
Iris <- data.frame(rbind(iris3[,,1], iris3[,,2], iris3[,,3]), 
        Sp = rep(c("s","c","v"), rep(50,3))) 
set.seed(1) ## remove this line if you want it to be pseudo random 
train <- sample(1:150, 75) 
table(Iris$Sp[train]) 
## your answer may differ 
## c s v 
## 22 23 30 
z <- lda(Sp ~ ., Iris, prior = c(1,1,1)/3, subset = train) 

## get the whole prediction object 
pred <- predict(z) 
## show first few sample scores on LDs 
head(z$x) 

的最後一行示出了對象的分數的對線性判別式的前幾行

> head(pred$x) 
      LD1  LD2 
40 -8.334664 0.1348578 
56 2.462821 -1.5758927 
85 2.998319 -0.6648073 
134 4.030165 -1.4724530 
30 -7.511226 -0.6519301 
131 6.779570 -0.8675742 

這些得分可以被繪製,像這樣

plot(LD2 ~ LD1, data = pred$x) 

產生如下圖(此訓練樣本!)

lda scores plot

+0

非常有用,然後很簡單,我不能相信我以前沒有見過。謝謝! –

1

當您調用功能plot(z)時,您實際上正在調用功能plot.lda - 這是一種S3方法。基本上,對象zlda類:

class(z) 

大家可以看一下正在使用的實際功能:

getS3method("plot", "lda") 

這真可謂是相當複雜的。但關鍵點是:

x = z 
Terms <- x$terms 
data <- model.frame(x) 
X <- model.matrix(delete.response(Terms), data) 
g <- model.response(data) 
xint <- match("(Intercept)", colnames(X), nomatch = 0L) 
X <- X[, -xint, drop = FALSE] 
means <- colMeans(x$means) 
X <- scale(X, center = means, scale = FALSE) %*% x$scaling 

我們不能情節像以前一樣:

plot(X[,1], X[,2]) 

限制性條文有很可能是得到你想要的東西更簡單的方法 - 我只是不知道lda功能。

+0

哇,謝謝!幾乎看起來像魔術! –

+0

+1,額外的神奇來源是這是在''lda''對象的'predict()'方法中完成的,然後是一些,因爲它提供了幾種不同的方法來生成預測。我在答覆中提供了一個例子。 –

+0

'預測' - 我應該猜到了。絕對是正確的選擇。 – csgillespie