2012-09-19 25 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