[R] Cross-validation : can't get the predicted response on the testing data
varin sacha
v@rin@@ch@ @ending from y@hoo@fr
Sat Apr 21 20:48:55 CEST 2018
Dear R-experts,
Doing cross-validation for 2 robust regressions (HBR and fast Tau). I can't get the 2 errors rates (RMSE and MAPE). The problem is to predict the response on the testing data. I get 2 error messages.
Here below the reproducible (fictional example) R code.
#install.packages("MLmetrics")
# install.packages( "robustbase" )
# install.packages( "MASS" )
# install.packages( "quantreg" )
# install.packages( "RobPer" )
# install.packages( "scatterplot3d" )
# install.packages("devtools") # library("devtools") # install_github("kloke/hbrfit") #install.packages('http://www.stat.wmich.edu/mckean/Stat666/Pkgs/npsmReg2_0.1.1.tar.gz')
library(robustbase)
library(MASS)
library(quantreg)
library(RobPer)
library(scatterplot3d)
library(hbrfit)
library(MLmetrics)
# numeric variables
A=c(2,3,4,3,2,6,5,6,4,3,5,55,6,5,4,5,6,6,7,52)
B= c(45,43,23,47,65,21,12,7,18,29,56,45,34,23,12,65,4,34,54,23)
C=c(21,54,34,12,4,56,74,3,12,71,14,15,63,34,35,23,24,21,69,32)
# Create a dataframe
BIO<-data.frame(A,B,C)
# randomize sampling seed
set.seed(1)
n=dim(BIO)[1]
p=0.667
# Sample size
sam=sample(1 :n,floor(p*n),replace=FALSE)
# Sample training data
Training =BIO [sam,]
# Sample testing data
Testing = BIO [-sam,]
# Build the 2 models
fit<- FastTau(model.matrix(~Training$A+Training$B),Training$C)
HBR<-hbrfit(C ~ A+B)
# Predict the response on the testing data
ypred=predict(fit,newdata=Testing)
ypred=predict(HBR,newdata=Testing)
# Get the true response from testing data
y=BIO[-sam,]$D
# Get error rate
RMSE=sqrt(mean((y-ypred)^2))
RMSE
MAPE = mean(abs(y-ypred/y))
MAPE
More information about the R-help
mailing list