I am comparing predictions between different methods the 1st method is linear regression (lm) and the 2nd is rpart
the lm is ok, i send 2 variables and i get a 2 variables.
but with rpart, i dont get the same , i just get 1 variable.
why not getting 2 results 1 for y1 and the other for y2
this is my code
######################################
## 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
This is because the predict
method returns different classes, depending on the argument which you have passed.
If you try:
?Predict
Then it says:
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.
and its value is:
The form of the value returned by predict depends on the class of its argument.
So, the response of lm
method is an object of class lm
, meanwhile the return value of rpart
is An object of class rpart
Therefore, the predict method provides you different answers.
What can you do to get the same result?
your lm method made a model to estimate the values of y1
, and y2
. So, you should run the rpart in a way that it also obtains the values of y1
, and y2
.
To do this in your rpart
method define method="class"
, but it fails. Because it cannot classify 2 features. Therefore, the biggest problem comes from your formula, where you have cbind(y1,y2)~.
Reading Rpart document helps you a lot.