Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:rpart与多值预测_R_Prediction_Rpart - Fatal编程技术网

R:rpart与多值预测

R:rpart与多值预测,r,prediction,rpart,R,Prediction,Rpart,我正在比较不同方法的预测 第一种方法是线性回归(lm),第二种方法是rpart lm正常,我发送2个变量,得到2个变量 但是对于rpart,我没有得到相同的结果,我只得到1个变量 为什么不得到两个结果,一个是y1,另一个是y2 这是我的密码 ###################################### ## S E T U P ###################################### x1 <- c(11, 21, 20, 36, 27,

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

lm正常,我发送2个变量,得到2个变量

但是对于rpart,我没有得到相同的结果,我只得到1个变量

为什么不得到两个结果,一个是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)
######################################
##S E T U P
######################################
x1
为什么不得到两个结果,一个是y1,另一个是y2

这是因为根据您传递的参数,
predict
方法返回不同的类。

如果您尝试:

?Predict
然后它说:

Predict是一个通用函数,用于根据各种模型拟合函数的结果进行预测。该函数调用特定的 依赖于第一个参数的类的方法

其价值是:

predict返回的值的形式取决于其参数的类别

因此,
lm
方法的响应是类
lm
的对象,而
rpart
的返回值是类
rpart
的对象

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


如何才能获得相同的结果?

您的lm方法制作了一个模型来估计
y1
y2
的值。因此,您应该以这样的方式运行rpart:它还可以获得
y1
y2
的值

要在
rpart
方法中执行此操作,请定义
method=“class”
,但失败。因为它无法对两个特征进行分类。因此,最大的问题来自您的公式,其中您有
cbind(y1,y2)~。

阅读对你帮助很大