0

I have a code that works if I am using LM but not if I am using a GAM. I guess the that for whatever reason LM looks inside object for the variable and GAM just looks at the names of the object but I do not have a clue.

library(modelr)
library(purrr)
library(mgcv)


dataf <- data(cars)

dataf <- mtcars

df_split <- crossv_mc(dataf,10)

# LM - works
fn_model <- function(df){
  lm(mpg ~ wt, data = df)
}

df_split_model <-
  df_split %>%
  mutate(model = map(train, fn_model)) %>%
  print()

# GAM - does not

fn_model <- function(df){
  mgcv::gam(mpg ~ wt, data = df)
}

df_split_model <-
  df_split %>%
  mutate(model = map(train, fn_model)) %>%
  print()


#But i can do
gam(mpg~wt,data=mtcars)
0

1 Answer 1

4

You need to explicitly convert the resample object you are passing to fn_model to a data frame. It seems that gam is a little more particular than lm about the objects you can pass to the data argument.

fn_model <- function(df){
  mgcv::gam(mpg ~ wt, data = as.data.frame(df))
}

df_split_model <-
  df_split %>%
  mutate(model = map(train, fn_model)) %>%
  print()
#> # A tibble: 10 x 4
#>    train                test                .id   model 
#>    <list>               <list>              <chr> <list>
#>  1 <resample [25 x 11]> <resample [7 x 11]> 01    <gam> 
#>  2 <resample [25 x 11]> <resample [7 x 11]> 02    <gam> 
#>  3 <resample [25 x 11]> <resample [7 x 11]> 03    <gam> 
#>  4 <resample [25 x 11]> <resample [7 x 11]> 04    <gam> 
#>  5 <resample [25 x 11]> <resample [7 x 11]> 05    <gam> 
#>  6 <resample [25 x 11]> <resample [7 x 11]> 06    <gam> 
#>  7 <resample [25 x 11]> <resample [7 x 11]> 07    <gam> 
#>  8 <resample [25 x 11]> <resample [7 x 11]> 08    <gam> 
#>  9 <resample [25 x 11]> <resample [7 x 11]> 09    <gam> 
#> 10 <resample [25 x 11]> <resample [7 x 11]> 10    <gam> 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.