I'm trying to use the nested dataframe (https://r4ds.had.co.nz/many-models.html) approach to fit multiple latent class growth curves using lcmm::lcmm()
and purrr::pmap()
.
This process requires fitting a model with one class (k = 1) using lcmm() and then using this model as an input to lcmm::gridsearch()
, which takes the starting values from this k = 1 model to feed into a k = 2+ class model. gridsearch()
also requires the model call for the k = 2+ model (plus two other arguments), which passed as a call to lcmm()
within the call to gridsearch()
. My usual approach would be to use pmap()
to pass a list of arguments to gridsearch()
, but list()
immediately evaluates the model call to lcmm()
and tries to fit the model instead of passing the model call to gridsearch()
(see confusing behavior of purrr::pmap with rlang; "to quote" or not to quote argument that is the Q).
NB Using RStudio's function viewer (F2), it seems that lcmm::gridsearch()
uses match.call()
to adjust the k = 2+ model call with a user-defined number of random starting values, and then iterate through these to find the preferred k = 2+ solution.
I've included a reprex below. When wrapping the call to gridsearch in pmap the command fails with "Error in mutate_impl(.data, dots) : Evaluation error: argument is of length zero." - I think this is because R is trying to evaluate the call to lcmm()
for the k = 2+ model, but I could be wrong.
How can I delay the evaluation of lcmm()
when passed as an argument to pmap()
?
Reprex below:
library(lcmm)
#> Warning: package 'lcmm' was built under R version 3.5.2
#> Loading required package: survival
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(tidyr)
library(purrr)
# load lcmm example data
data("data_lcmm")
# take sample
set.seed(123)
data_lcmm <-
data_lcmm %>%
sample_frac(0.1)
# NB grouping variable is needed to reproduce desired data structure
data_lcmm <-
data_lcmm %>%
mutate(group_var = sample(c(0, 1),
size = nrow(data_lcmm),
replace = TRUE
))
data_lcmm_nest <-
data_lcmm %>%
group_by(group_var) %>%
nest() %>%
mutate(data= map(data, as.data.frame))
# lcmm call from ?lcmm
lcmm_k1 <- function(df) {
lcmm(Ydep2 ~ Time + I(Time^2),
random = ~Time, subject = "ID", ng = 1,
data = data_lcmm_nest$data[[1]], link = "linear"
)
}
# fit k = 1 models
data_lcmm_nest <-
data_lcmm_nest %>%
mutate(lcgm = map(data, lcmm_k1))
#> Be patient, lcmm is running ...
#> The program took 0.18 seconds
#> Be patient, lcmm is running ...
#> The program took 0.19 seconds
# this works for a single row
desired_result <-
gridsearch(
m = lcmm(Ydep2 ~ Time + I(Time^2),
mixture = ~Time,
random = ~Time, subject = "ID", ng = 2,
data = data_lcmm_nest$data[[1]], link = "linear"
),
rep = 5,
maxiter = 2,
minit = data_lcmm_nest$lcgm[[1]]
)
#> Be patient, lcmm is running ...
#> The program took 0.45 seconds
#> Be patient, lcmm is running ...
#> The program took 0.45 seconds
#> Be patient, lcmm is running ...
#> The program took 0.45 seconds
#> Be patient, lcmm is running ...
#> The program took 0.45 seconds
#> Be patient, lcmm is running ...
#> The program took 0.47 seconds
#> Be patient, lcmm is running ...
#> The program took 0.61 seconds
# this fails with Error in mutate_impl(.data, dots) :
# Evaluation error: argument is of length zero.
data_lcmm_nest %>%
mutate(lcgm_2 = pmap(
list(
m = lcmm(Ydep2 ~ Time + I(Time^2),
mixture = ~Time,
random = ~Time, subject = "ID", ng = 2,
data = data, link = "linear"
),
rep = 5,
maxiter = 2,
minit = lcgm
), gridsearch
))
#> Error in mutate_impl(.data, dots): Evaluation error: argument is of length zero.
# wrapping gridsearch in helper also fails
grid_search_helper <- function(g_rep, g_maxiter, g_minit, g_m) {
gridsearch(
m = lcmm(Ydep2 ~ Time + I(Time^2),
mixture = ~Time,
random = ~Time, subject = "ID", ng = 2,
data = g_m, link = "linear"
),
rep = g_rep,
maxiter = g_maxiter,
minit = g_minit
)
}
data_lcmm_nest %>%
mutate(lcgm_2 = pmap(
list(
5,
2,
lcgm,
data
), grid_search_helper
))
#> Error in mutate_impl(.data, dots): Evaluation error: object 'g_m' not found.
Created on 2019-01-24 by the reprex package (v0.2.1)