predict(..., type="class")
predict(..., type="response")
predict(modelName, newdata=testData, type = "class")
和
predict(modelName, newdata=testData, type = "response")
predict(..., type="class")
predict(..., type="response")
predict(modelName, newdata=testData, type = "class")
和
predict(modelName, newdata=testData, type = "response")
响应(Response)提供数字结果,而分类(class)则给出赋予该值的标签。
响应(Response)使您能够确定阈值。例如,
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"
type = "response"
用于 glm 模型,type = "class"
用于 rpart 模型(CART)。
参见:
看一下?predict.lm
:
predict.lm
会产生一个预测向量或一个带有列名fit
、lwr
和upr
的预测矩阵,如果设置了区间。对于type = "terms"
,这是一个每个术语一列的矩阵,可能具有"constant"属性。
> 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")
行求和结果加常数。
predict
函数?predict
是用于根据不同模型拟合函数的结果进行预测的通用函数。该函数调用依赖于第一个参数的类别的特定方法。需要根据类别选择相应的方法。 - shadow