1

I am trying to create a nested tibble with columns containing models. I succeeded in creating a column with the nlsList models. I am failing at creating a column with the corresponding nlme's.

Even when I extract one nlsList element and try to use it without map() it fails. I am stumped.

library(nlme)
fm1 = nlsList(uptake ~ SSasympOff(conc, Asym, lrc, c0),
              data = CO2, start = c(Asym = 30, lrc = -4.5, c0 = 52))
# str(CO2)

gCO2 <- CO2 %>%
  dplyr::group_by(Treatment) %>%
  nest() %>%
  mutate(gdat = map(data, ~ groupedData(uptake ~ conc | Plant, data = .x)))

gCO2

# # A tibble: 2 x 3
# # Groups:   Treatment [2]
# Treatment  data              gdat             
# <fct>      <list>            <list>           
#   1 nonchilled <tibble [42 × 4]> <tibble [42 × 4]>
#   2 chilled    <tibble [42 × 4]> <tibble [42 × 4]>


map(gCO2$gdat, ~class(.)) #OK?

gCO2 <- gCO2 %>%
  mutate(nlsLst1 =
           map(gdat,
               ~ nlsList(uptake ~ SSasympOff(conc, Asym, lrc, c0),
                         data = .x, start = c(Asym = 30,
                                              lrc = -4.5,
                                              c0 = 52))))

map(gCO2$nlsLst1, ~class(.)) #OK?


gCO2 <- gCO2 %>%
  mutate(nlme1 = map(nlsLst1, ~nlme(model = .x,
                                    random = Asym ~ 1)))

# Error: Problem with `mutate()` input `nlme1`.
# x object '.x' not found
# ℹ Input `nlme1` is `map(nlsLst1, ~nlme(model = ., random = Asym ~ 1))`.
# ℹ The error occurred in group 1: Treatment = "nonchilled".
# Run `rlang::last_error()` to see where the error occurred.


  • 1
    The problem is that the call to `nlsList` remembers that the data.frame you used was called `.x`. Then when you go to call `nlme`, it can't find that `.x` variable anymore. It doesn't know the data is stored in your `gdat` column. Seems like this function really doesn't like operating in a tibble. – MrFlick Oct 26 '20 at 20:01
  • @MrFlick thanks! Indeed, the nlme, nlsList, and others keep the .x as the name of the data. I was not able to make it work with tidyverse. Maybe others have better luck, but for now I do not know how to make these models play with tidymodels. – Emilio A. Laca Nov 12 '20 at 18:46

0 Answers0