-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't use lme4::lmer() inside a targets workflow #160
Comments
This is an instance of ropensci/drake#1012 and a permanent consequence of how library(targets)
tar_script({
library(targets)
options(crayon.enabled = FALSE)
tar_option_set(packages = c("targets", "ggplot2", "lme4"))
read_data <- function() {
DF_raw = ggplot2::mpg
return(DF_raw)
}
run_lmer <- function(data) {
envir <- environment()
envir$data <- data
f <- as.formula("Reaction ~ Days + (Days | Subject)", env = envir)
lme4::lmer(f, data = data)
}
tar_pipeline(
tar_target(df, read_data()),
tar_target(model, lme4::lmer(year ~ displ + (1|manufacturer), data = df))
)
})
tar_make()
#> ● run target df
#> ● run target model
#> boundary (singular) fit: see ?isSingular
tar_read(model)
#> Linear mixed model fit by REML ['lmerMod']
#> Formula: year ~ displ + (1 | manufacturer)
#> Data: df
#> REML criterion at convergence: 1364.562
#> Random effects:
#> Groups Name Std.Dev.
#> manufacturer (Intercept) 0.00
#> Residual 4.47
#> Number of obs: 234, groups: manufacturer, 15
#> Fixed Effects:
#> (Intercept) displ
#> 2001.7084 0.5161
#> convergence code 0; 0 optimizer warnings; 1 lme4 warnings Created on 2020-09-17 by the reprex package (v0.3.0) |
Thanks for the quick response @wlandau I was trying to implement the custom function solution, but apparently there is something else(more?) going on here. It seems the lmer() models will work when the data is called targets Example: Without custom function, data is called library(targets)
tar_script({
library(targets)
options(crayon.enabled = FALSE)
tar_option_set(packages = c("targets", "ggplot2", "lme4"))
read_data <- function() {
DF_raw = ggplot2::mpg
return(DF_raw)
}
# run_lmer <- function(data) {
# envir <- environment()
# envir$data <- data
# f <- as.formula("Reaction ~ Days + (Days | Subject)", env = envir)
# lme4::lmer(f, data = data)
# }
tar_pipeline(
tar_target(df, read_data()),
tar_target(model, lme4::lmer(year ~ displ + (1|manufacturer), data = df))
)
})
tar_make()
#> ● run target df
#> ● run target model
#> boundary (singular) fit: see ?isSingular
tar_read(model)
#> Linear mixed model fit by REML ['lmerMod']
#> Formula: year ~ displ + (1 | manufacturer)
#> Data: df
#> REML criterion at convergence: 1364.562
#> Random effects:
#> Groups Name Std.Dev.
#> manufacturer (Intercept) 0.00
#> Residual 4.47
#> Number of obs: 234, groups: manufacturer, 15
#> Fixed Effects:
#> (Intercept) displ
#> 2001.7084 0.5161
#> convergence code 0; 0 optimizer warnings; 1 lme4 warnings Created on 2020-09-18 by the reprex package (v0.3.0) Not sure if I should post this in the ropensci/drake#1012 web, but just in case... the same happens in drake. Everything is fine as long as the data is called drake Example (see mod2): library(drake)
suppressPackageStartupMessages(library(lme4))
fit_lmer <- function(dat) {
envir <- environment()
envir$dat <- dat
f <- as.formula("Reaction ~ Days + (Days | Subject)", env = envir)
lme4::lmer(f, data = dat)
}
plan <- drake_plan(
dat = sleepstudy,
df = sleepstudy,
mod = fit_lmer(dat),
mod2 = lme4::lmer("Reaction ~ Days + (Days | Subject)", df)
)
make(plan)
#> ▶ target df
#> ▶ target dat
#> ▶ target mod2
#> ▶ target mod Created on 2020-09-18 by the reprex package (v0.3.0) |
Interesting. I wonder what's so special about In any case, it may be worth contacting the maintainers of |
Prework
Description
Can't find a way to use lme4::lmer() inside a targets workflow when the data object is created in one of the previous steps.
Below you can see that when
data = ggplot2::mpg
it works, but whendata = DF_raw
, it gives an error.I assume I am doing something wrong, that is why I am filling a "Trouble".
Thanks for the help!
Reproducible example
_targets.R file:
Created on 2020-09-17 by the reprex package (v0.3.0)
Session info
When running
targets::tar_make()
I get the following output:Desired result
Would expect to run the model3 target.
Diagnostic information
packageDescription("targets")$GithubSHA1
[1] "37af27f3b30c620a0dde66ff7d0120f0af98630a"
The text was updated successfully, but these errors were encountered: