R:具有多值预测的 rpart

R : rpart with multiple values prediction

我正在比较不同方法之间的预测 第一种方法是线性回归 (lm),第二种方法是 rpart

lm 没问题,我发送了 2 个变量,我得到了 2 个变量。

但是对于 rpart,我不一样,我只得到 1 个变量。

为什么不得到 2 个结果,一个是 y1,另一个是 y2

这是我的代码

######################################
##  S E T U P
######################################

x1 <- c(11,  21,  20,  36,  27,  15,  7,   19,  40,  5 )
x2 <- c(142, 175, 175, 180, 181, 160, 110, 170, 177, 92)
x3 <- c(44,  78,  79,  82,  92,  56,  31,  66,  91,  29)
y1 <- c(36,  41,  42,  44,  45,  40,  34,  41,  45,  32)
y2 <- c(7,   13,  13,  17,  19,  11,  6,   12,  19,  4)

TData <- data.frame(x1=x1[1:7], x2=x2[1:7], x3=x3[1:7], y1=y1[1:7], y2=y2[1:7])
PData <- data.frame(x1=x1[8:10], x2=x2[8:10], x3=x3[8:10], y1=y1[8:10], y2=y2[8:10])

######################################
## LINEAR REGRESSION
######################################

lm_Result <- lm(cbind(y1,y2)~., TData)

lm_pred <- predict(lm_Result, PData)

lr_pred[,"y1"]
lr_pred[,"y2"]


######################################
## RPART
######################################

library(rpart)

rpart_Result <- rpart(cbind(y1,y2)~., TData)

rpart_pred <- predict(rpart_Result, PData)

why not getting 2 results 1 for y1 and the other for y2

这是因为 predict 方法 return 不同 class,具体取决于您传递的参数。

如果你尝试:

?Predict

然后它说:

Predict is a generic function for predictions from the results of various model fitting functions. The function invokes particular methods which depend on the class of the first argument.

其值为:

The form of the value returned by predict depends on the class of its argument.

所以,lm方法的响应是classlm的一个对象,同时rpart的return值是[的一个对象=54=] rpart

因此,预测方法为您提供了不同的答案。


怎样才能得到相同的结果?

您的 lm 方法建立了一个模型来估计 y1y2 的值。因此,您应该 运行 rpart 的方式使其也获得 y1y2 的值。

要在您的 rpart 方法中定义 method="class",但它失败了。因为它不能 classify 2 个特征。因此,最大的问题来自你的公式,其中你有cbind(y1,y2)~.

阅读Rpart document对你有很大帮助。