输入predict()函数的参数

时间:2014-04-15 13:25:23

标签: r

type="class"函数中type="response"predict之间的区别是什么?

例如:

predict(modelName, newdata=testData, type = "class")

predict(modelName, newdata=testData, type = "response")

3 个答案:

答案 0 :(得分:7)

Response为您提供数值结果,而class为您提供分配给该值的标签。

响应可让您确定阈值。例如,

glm.fit = glm(Direction~., data=data, family = binomial, subset = train)
glm.probs = predict(glm.fit, test, type = "response")

glm.probs中,我们有一些介于0和1之间的数值。现在我们可以确定阈值,让我们说0.6。方向有两种可能的结果,向上或向下。

glm.pred = rep("Down",length(test))
glm.pred[glm.probs>.6] = "Up"

答案 1 :(得分:3)

请参阅?predict.lm: 如果设置了区间,predict.lm会生成预测矢量或预测和范围矩阵,列名为fitlwrupr。对于type = "terms",这是一个矩阵,每个术语有一列,可能有一个属性"常数"。

> d <- data.frame(x1=1:10,x2=rep(1:5,each=2),y=1:10+rnorm(10)+rep(1:5,each=2))
> l <- lm(y~x1+x2,d)
> predict(l)
        1         2         3         4         5         6         7         8         9        10 
 2.254772  3.811761  4.959634  6.516623  7.664497  9.221486 10.369359 11.926348 13.074222 14.631211 

> predict(l,type="terms")
           x1         x2
1  -7.0064511  0.8182315
2  -5.4494620  0.8182315
3  -3.8924728  0.4091157
4  -2.3354837  0.4091157
5  -0.7784946  0.0000000
6   0.7784946  0.0000000
7   2.3354837 -0.4091157
8   3.8924728 -0.4091157
9   5.4494620 -0.8182315
10  7.0064511 -0.8182315
attr(,"constant")
[1] 8.442991

即。 predict(l)predict(l,type="terms") +常量

的行和

答案 2 :(得分:3)

type = "response"用于 glm 模型,type = "class"用于 rpart 模型(CART)。 参见: