topepo / caret

caret (Classification And Regression Training) R package that contains misc functions for training and plotting classification and regression models
http://topepo.github.io/caret/index.html
1.61k stars 634 forks source link

No rqlasso quantile regression ntile output #1325

Open Steviey opened 1 year ago

Steviey commented 1 year ago

How to caret predict ntiles of rqlasso?

tried but failed...

Complete Example:

`
suppressPackageStartupMessages(library(caret)) suppressPackageStartupMessages(library(quantreq))

df <- matrix(rnorm(1000), ncol = 2) %>% 
    as.data.frame() %>%
    dplyr::mutate(y = sample(1000/2))
colnames(df)<-c('one','two','y')

rownames(df)    <- NULL
df  <- df %>% dplyr::mutate(id=row_number())
myLimit     <- base::max(df[['id']])
myLimit     <- myLimit-30

df_train   <- df %>% filter(id <= myLimit)
df_test    <- df %>% filter(id > myLimit)
allData    <- dplyr::bind_rows(df_train,df_test)

myFormula <- paste0('y ~ one + two')  
myFormula <- as.formula(myFormula)

Control <- caret::trainControl(method = "cv",number = 5)
myModel <- caret::train(myFormula, df_train,trCtrl= Control,method = "rqlasso",tuneLength=10)
preds   <- predict(myModel, df_test) # , type = "raw"

print('preds')
print(as_tibble(preds),n=20,max_extra_cols=0,width=110)`

output (no quantile preds):

[1] "preds"

A tibble: 30 x 1

value

1 250 2 250 3 250 4 250 5 250 6 250 7 250 8 250 output standalone-algo 'quantreg' : myModel <- quantreg::rq(myFormula,data=df_train,tau=seq(0.1, 0.9, by = 0.1)) preds <- stats::predict(myModel,newdata=df_test) print('preds') print(as_tibble(preds),n=20,max_extra_cols=0,width=110) [1] "preds" # A tibble: 30 x 9 `tau= 0.1` `tau= 0.2` `tau= 0.3` `tau= 0.4` `tau= 0.5` `tau= 0.6` `tau= 0.7` `tau= 0.8` `tau= 0.9` 1 44.8 102. 163. 204. 256. 289. 331. 393. 439. 2 33.4 76.4 150. 193. 239. 299. 340. 380. 435. 3 52.5 84.1 143. 195. 249. 315. 361. 406. 456. 4 57.7 111. 162. 207. 265. 296. 342. 409. 452. 5 60.1 117. 166. 209. 270. 293. 339. 412. 453. 6 49.4 102.