www
www

Reputation: 39154

Why I cannot apply a function to a list of lm fit using map?

I am fitting linear regression model to different groups using nested data frame and map function. After that, I want to conduct segmented regression to each fit of lm.

In this example, diamonds2 contains a column Fit_lm with lm fit of each group. After that, I thought I can use map again to apply the segmented function, but the code did not work (see diamonds3).

Nevertheless, if I combined lm and segmented in the same function, the code worked (see diamonds4).

If possible, I would like to learn the reason why map and segmented cannot work on the lm object? Does this has something to do with how the map function work? Of course, I can do the same thing like diamonds4, but since I have already fit lm once, this strategy is not efficient.

library(tidyverse)
library(segmented)

data("diamonds")

diamonds2 <- diamonds %>%
  group_by(cut) %>%
  nest() %>%
  mutate(Fit_lm = map(data, ~lm(price ~ carat, data = .x))) 

diamonds3 <- diamonds2 %>%
  # segmented regression
  mutate(Fit_seg = map(Fit_lm, ~segmented(.x, seg.Z = ~carat)))
# Error: Problem with `mutate()` input `Fit_seg`.
# x cannot coerce class ‘"lm"’ to a data.frame
# i Input `Fit_seg` is `map(Fit_lm, ~segmented(.x, seg.Z = ~carat))`.
# i The error occured in group 1: cut = "Fair".
# Run `rlang::last_error()` to see where the error occurred.

diamonds4 <- diamonds2 %>%
  #  lm and segmented regression
  mutate(Fit_seg = map(data, function(x){
    fit <- lm(price ~ carat, data = x)
    fit2 <- segmented(fit, seg.Z = ~carat)
    return(fit2)
  }))

Upvotes: 0

Views: 896

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 389155

You need data in the function when you run segmented. Also data should have the same name as it was when you ran lm.

So this works :

library(dplyr)
library(purrr)
library(segmented)

diamonds2 %>%
  mutate(Fit_seg = map2(data, Fit_lm, ~segmented(.y, seg.Z = ~carat)))

#  cut       data                  Fit_lm Fit_seg   
#  <ord>     <list>                <list> <list>    
#1 Ideal     <tibble [21,551 × 9]> <lm>   <segmentd>
#2 Premium   <tibble [13,791 × 9]> <lm>   <segmentd>
#3 Good      <tibble [4,906 × 9]>  <lm>   <segmentd>
#4 Very Good <tibble [12,082 × 9]> <lm>   <segmentd>
#5 Fair      <tibble [1,610 × 9]>  <lm>   <segmentd>

but this will not :

diamonds2 %>%
  mutate(Fit_seg = map2(Fit_lm, data, ~segmented(.x, seg.Z = ~carat)))

This is because your data was named .x when you ran lm.

So it will fail even if you run with an anonymous function because .x is different from x.

diamonds2 %>% mutate(Fit_seg = map2(data, Fit_lm, 
            function(x, y) segmented(y, seg.Z = ~carat)))

So I guess the best solution is to use segmented along with lm in the same function so you have both data and lm object together.

Upvotes: 3

Bruno
Bruno

Reputation: 4150

You can do with the new dplyr 1.0.0

library(tidyverse)
library(segmented)

data("diamonds")

diamonds2 <- diamonds %>%
  nest_by(cut) %>% 
  mutate(fit_lm = list(lm(price ~ carat,data = data)),
         seg = list(segmented(fit_lm,seg.Z = ~carat)))

diamonds2
#> # A tibble: 5 x 4
#> # Rowwise:  cut
#>   cut                     data fit_lm seg       
#>   <ord>     <list<tbl_df[,9]>> <list> <list>    
#> 1 Fair             [1,610 x 9] <lm>   <segmentd>
#> 2 Good             [4,906 x 9] <lm>   <segmentd>
#> 3 Very Good       [12,082 x 9] <lm>   <segmentd>
#> 4 Premium         [13,791 x 9] <lm>   <segmentd>
#> 5 Ideal           [21,551 x 9] <lm>   <segmentd>

Created on 2020-06-05 by the reprex package (v0.3.0)

devtools::session_info()
#> - Session info ---------------------------------------------------------------
#>  setting  value                       
#>  version  R version 4.0.0 (2020-04-24)
#>  os       Windows 10 x64              
#>  system   x86_64, mingw32             
#>  ui       RTerm                       
#>  language (EN)                        
#>  collate  English_United Kingdom.1252 
#>  ctype    English_United Kingdom.1252 
#>  tz       America/Sao_Paulo           
#>  date     2020-06-05                  
#> 
#> - Packages -------------------------------------------------------------------
#>  package     * version    date       lib source                              
#>  assertthat    0.2.1      2019-03-21 [1] CRAN (R 4.0.0)                      
#>  backports     1.1.7      2020-05-13 [1] CRAN (R 4.0.0)                      
#>  blob          1.2.1      2020-01-20 [1] CRAN (R 4.0.0)                      
#>  broom         0.5.6      2020-04-20 [1] CRAN (R 4.0.0)                      
#>  callr         3.4.3      2020-03-28 [1] CRAN (R 4.0.0)                      
#>  cellranger    1.1.0      2016-07-27 [1] CRAN (R 4.0.0)                      
#>  cli           2.0.2      2020-02-28 [1] CRAN (R 4.0.0)                      
#>  colorspace    1.4-1      2019-03-18 [1] CRAN (R 4.0.0)                      
#>  crayon        1.3.4      2017-09-16 [1] CRAN (R 4.0.0)                      
#>  DBI           1.1.0      2019-12-15 [1] CRAN (R 4.0.0)                      
#>  dbplyr        1.4.4      2020-05-27 [1] CRAN (R 4.0.0)                      
#>  desc          1.2.0      2018-05-01 [1] CRAN (R 4.0.0)                      
#>  devtools      2.3.0      2020-04-10 [1] CRAN (R 4.0.0)                      
#>  digest        0.6.25     2020-02-23 [1] CRAN (R 4.0.0)                      
#>  dplyr       * 1.0.0      2020-05-29 [1] CRAN (R 4.0.0)                      
#>  ellipsis      0.3.1      2020-05-15 [1] CRAN (R 4.0.0)                      
#>  evaluate      0.14       2019-05-28 [1] CRAN (R 4.0.0)                      
#>  fansi         0.4.1      2020-01-08 [1] CRAN (R 4.0.0)                      
#>  forcats     * 0.5.0      2020-03-01 [1] CRAN (R 4.0.0)                      
#>  fs            1.4.1      2020-04-04 [1] CRAN (R 4.0.0)                      
#>  generics      0.0.2      2018-11-29 [1] CRAN (R 4.0.0)                      
#>  ggplot2     * 3.3.1      2020-05-28 [1] CRAN (R 4.0.0)                      
#>  glue          1.4.1      2020-05-13 [1] CRAN (R 4.0.0)                      
#>  gtable        0.3.0      2019-03-25 [1] CRAN (R 4.0.0)                      
#>  haven         2.3.0      2020-05-24 [1] CRAN (R 4.0.0)                      
#>  highr         0.8        2019-03-20 [1] CRAN (R 4.0.0)                      
#>  hms           0.5.3      2020-01-08 [1] CRAN (R 4.0.0)                      
#>  htmltools     0.4.0      2019-10-04 [1] CRAN (R 4.0.0)                      
#>  httr          1.4.1      2019-08-05 [1] CRAN (R 4.0.0)                      
#>  jsonlite      1.6.1      2020-02-02 [1] CRAN (R 4.0.0)                      
#>  knitr         1.28       2020-02-06 [1] CRAN (R 4.0.0)                      
#>  lattice       0.20-41    2020-04-02 [2] CRAN (R 4.0.0)                      
#>  lifecycle     0.2.0      2020-03-06 [1] CRAN (R 4.0.0)                      
#>  lubridate     1.7.8      2020-04-06 [1] CRAN (R 4.0.0)                      
#>  magrittr      1.5        2014-11-22 [1] CRAN (R 4.0.0)                      
#>  memoise       1.1.0      2017-04-21 [1] CRAN (R 4.0.0)                      
#>  modelr        0.1.8      2020-05-19 [1] CRAN (R 4.0.0)                      
#>  munsell       0.5.0      2018-06-12 [1] CRAN (R 4.0.0)                      
#>  nlme          3.1-147    2020-04-13 [2] CRAN (R 4.0.0)                      
#>  pillar        1.4.4      2020-05-05 [1] CRAN (R 4.0.0)                      
#>  pkgbuild      1.0.8      2020-05-07 [1] CRAN (R 4.0.0)                      
#>  pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 4.0.0)                      
#>  pkgload       1.1.0      2020-05-29 [1] CRAN (R 4.0.0)                      
#>  prettyunits   1.1.1      2020-01-24 [1] CRAN (R 4.0.0)                      
#>  processx      3.4.2      2020-02-09 [1] CRAN (R 4.0.0)                      
#>  ps            1.3.3      2020-05-08 [1] CRAN (R 4.0.0)                      
#>  purrr       * 0.3.4      2020-04-17 [1] CRAN (R 4.0.0)                      
#>  R6            2.4.1      2019-11-12 [1] CRAN (R 4.0.0)                      
#>  Rcpp          1.0.4.6    2020-04-09 [1] CRAN (R 4.0.0)                      
#>  readr       * 1.3.1      2018-12-21 [1] CRAN (R 4.0.0)                      
#>  readxl        1.3.1      2019-03-13 [1] CRAN (R 4.0.0)                      
#>  remotes       2.1.1      2020-02-15 [1] CRAN (R 4.0.0)                      
#>  reprex        0.3.0      2019-05-16 [1] CRAN (R 4.0.0)                      
#>  rlang         0.4.6      2020-05-02 [1] CRAN (R 4.0.0)                      
#>  rmarkdown     2.2        2020-05-31 [1] CRAN (R 4.0.0)                      
#>  rprojroot     1.3-2      2018-01-03 [1] CRAN (R 4.0.0)                      
#>  rvest         0.3.5      2019-11-08 [1] CRAN (R 4.0.0)                      
#>  scales        1.1.1      2020-05-11 [1] CRAN (R 4.0.0)                      
#>  segmented   * 1.1-0      2019-12-10 [1] CRAN (R 4.0.0)                      
#>  sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 4.0.0)                      
#>  stringi       1.4.6      2020-02-17 [1] CRAN (R 4.0.0)                      
#>  stringr     * 1.4.0      2019-02-10 [1] CRAN (R 4.0.0)                      
#>  testthat      2.3.2      2020-03-02 [1] CRAN (R 4.0.0)                      
#>  tibble      * 3.0.1      2020-04-20 [1] CRAN (R 4.0.0)                      
#>  tidyr       * 1.1.0      2020-05-20 [1] CRAN (R 4.0.0)                      
#>  tidyselect    1.1.0      2020-05-11 [1] CRAN (R 4.0.0)                      
#>  tidyverse   * 1.3.0.9000 2020-05-16 [1] Github (tidyverse/tidyverse@1d7f9b7)
#>  usethis       1.6.1      2020-04-29 [1] CRAN (R 4.0.0)                      
#>  utf8          1.1.4      2018-05-24 [1] CRAN (R 4.0.0)                      
#>  vctrs         0.3.0      2020-05-11 [1] CRAN (R 4.0.0)                      
#>  withr         2.2.0      2020-04-20 [1] CRAN (R 4.0.0)                      
#>  xfun          0.14       2020-05-20 [1] CRAN (R 4.0.0)                      
#>  xml2          1.3.2      2020-04-23 [1] CRAN (R 4.0.0)                      
#>  yaml          2.2.1      2020-02-01 [1] CRAN (R 4.0.0)                      
#> 
#> [1] C:/Users/brunotc1/Documents/R/win-library/4.0
#> [2] C:/Program Files/R/R-4.0.0/library

Upvotes: 2

Related Questions