Closed tchakravarty closed 6 years ago
You were just putting as.data.frame()
in the wrong place
library(tidyverse)
library(modelr)
library(randomForest, warn.conflicts = FALSE)
#> Warning: package 'randomForest' was built under R version 3.4.4
#> randomForest 4.6-14
#> Type rfNews() to see new features/changes/bug fixes.
df <- data_frame(
x1 = rnorm(500),
x2 = rnorm(500),
y = rnorm(500)
)
mods <- df %>%
crossv_kfold(k = 4) %>%
mutate(model = map(train, ~ randomForest(y ~ ., data = .x)))
mods %>% mutate(resid = map2(train, model, ~ add_residuals(as.data.frame(.x), .y)))
#> # A tibble: 4 x 5
#> train test .id model resid
#> <list> <list> <chr> <list> <list>
#> 1 <S3: resample> <S3: resample> 1 <S3: randomForest.formula> <tibble …
#> 2 <S3: resample> <S3: resample> 2 <S3: randomForest.formula> <tibble …
#> 3 <S3: resample> <S3: resample> 3 <S3: randomForest.formula> <tibble …
#> 4 <S3: resample> <S3: resample> 4 <S3: randomForest.formula> <tibble …
Created on 2018-05-10 by the reprex package (v0.2.0).
I could not get
add_residuals
to work on a random forest model when working with resampled data:I also tried to
broom::augment
the residuals in, but that didn't work either.I then tried to then write the logic myself:
but this terminates with the error:
Error:
.x(30) and
.y(10) are different lengths
. Not sure if I am doing something wrong but the end goal is to be able to add a list-column of vectors of residuals.PS. Can the
as.data.frame
calls be made redundant whenmap
ping non-modelr
functions toresample
s, perhaps by adding adata_frame
class to them?Edit:
So I tried the advice here to get the call to
augment
to work, but that fails:but this failed with an error:
Error: augment doesn't know how to deal with data of class randomForest.formularandomForest
.I tried the same approach using
add_residuals
:but that appears to have "failed gracefully" -- giving the
resample
s back.