Giter Site home page Giter Site logo

Comments (3)

juliasilge avatar juliasilge commented on August 28, 2024

To use resampling, you'll need to set up your workflow like this with the special formula argument to add_model(). One thing to keep in mind that your recipe is creating new features:

library(tidymodels)
#> Registered S3 method overwritten by 'tune':
#>   method                   from   
#>   required_pkgs.model_spec parsnip
library(tidyverse)

testdf <- structure(list(RER = c(0.949, 0.929, 0.935, 0.89, 0.911, 0.85, 
                                 0.868, 0.85, 0.913, 0.969), 
                         Time = c(30, 60, 120, 60, 70, 15, 
                                  74.711, 50, 140, 40), 
                         Intensity = c(58, 72.6, 70, 68, 68.527, 
                                       60, 70, 45, 70, 70), 
                         Study = c("Duhamel 2006", "Helge 1998", 
                                   "Van Zant 1997", "Parcell 1999", "Palmer 1999", "Lane 2015", 
                                   "Helge 1996", "Arkinstall 2004b", "Van Zant 1997", "Tarnopolsky 1995"
                         )), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
                         )) %>% as.data.frame()

mixed_rec  <- 
  recipe(RER ~ ., data = testdf) %>% 
  step_bs(Intensity) 

prep(mixed_rec) %>% bake(new_data = NULL)
#> # A tibble: 10 × 6
#>     Time Study              RER Intensity_bs_1 Intensity_bs_2 Intensity_bs_3
#>    <dbl> <fct>            <dbl>          <dbl>          <dbl>          <dbl>
#>  1  30   Duhamel 2006     0.949         0.395           0.352          0.104
#>  2  60   Helge 1998       0.929         0               0              1    
#>  3 120   Van Zant 1997    0.935         0.0241          0.232          0.743
#>  4  60   Parcell 1999     0.89          0.0694          0.347          0.579
#>  5  70   Palmer 1999      0.911         0.0557          0.322          0.619
#>  6  15   Lane 2015        0.85          0.340           0.405          0.161
#>  7  74.7 Helge 1996       0.868         0.0241          0.232          0.743
#>  8  50   Arkinstall 2004b 0.85          0               0              0    
#>  9 140   Van Zant 1997    0.913         0.0241          0.232          0.743
#> 10  40   Tarnopolsky 1995 0.969         0.0241          0.232          0.743

Created on 2021-11-09 by the reprex package (v2.0.1)

The special formula used by lmer will not know how to connect those to the original Intensity:

library(tidymodels)
#> Registered S3 method overwritten by 'tune':
#>   method                   from   
#>   required_pkgs.model_spec parsnip
library(tidyverse)
library(multilevelmod)

testdf <- structure(list(RER = c(0.949, 0.929, 0.935, 0.89, 0.911, 0.85, 
                                 0.868, 0.85, 0.913, 0.969), 
                         Time = c(30, 60, 120, 60, 70, 15, 
                                  74.711, 50, 140, 40), 
                         Intensity = c(58, 72.6, 70, 68, 68.527, 
                                       60, 70, 45, 70, 70), 
                         Study = c("Duhamel 2006", "Helge 1998", 
                                   "Van Zant 1997", "Parcell 1999", "Palmer 1999", "Lane 2015", 
                                   "Helge 1996", "Arkinstall 2004b", "Van Zant 1997", "Tarnopolsky 1995"
                         )), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
                         )) %>% as.data.frame()

mixed_model_spec <- linear_reg() %>% set_engine("lmer")
mixed_rec  <- recipe(RER ~ ., data = testdf) %>% 
  step_bs(Intensity) 


mixed_model_wf <- workflow() %>%
  add_model(mixed_model_spec, formula = RER ~ Time + Intensity  + (1 | Study)) %>%
  add_recipe(mixed_rec)

fit(mixed_model_wf, testdf)
#> Error in eval(predvars, data, env): object 'Intensity' not found
#> Timing stopped at: 0.002 0 0.003

Created on 2021-11-09 by the reprex package (v2.0.1)

So you'll need to account for that. This is a limitation of using the formula argument in a workflow; there's really no way to send in any flexible selectors. From workflows:

In those cases, a formula specifying the model structure must be passed unchanged into the model call itself.

from multilevelmod.

topepo avatar topepo commented on August 28, 2024

I believe this can work:

library(splines)
mixed_model_wf <- workflow() %>%
  add_model(mixed_model_spec, formula = RER ~ Time + bs(Intensity)  + (1 | Study)) %>%
  add_recipe(mixed_rec)

from multilevelmod.

github-actions avatar github-actions commented on August 28, 2024

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.

from multilevelmod.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.