Using nest and map in combination with mgcv gam and broom augment

Malte Willmes

I am trying to create a smooth spline for each sample in a grouped dataframe. For this I am using a nest and map approach and mgcv gam (following this example https://smu095.github.io/2019/02/16/2019-02-16-tidytuesday-fitting-multiple-time-series-models-using-purrr/).

After running the gam I would like to use broom::augment to extract the fitted data and calculate confidence intervals.

This code works using broom 0.5.6 but throws an error using the new broom 0.7 version. broom::tidy and broom:glance still work with this format but augment stops with "Error: Problem with mutate() input augment_spline. x object 'year' not found"

Example code below

library(tidyverse)
library(dslabs)

#Use the gapminder dataset that comes with dslabs as an example
glimpse(gapminder)

gapminder_nest <- gapminder %>% 
  group_by(country) %>% 
  nest()%>%
  mutate(splined =map(data, ~mgcv::gam(population ~ s(year, k=5, bs="tp"), data=.x))) %>%
  mutate(augment_spline= map(splined, broom::augment))%>%
  unnest(augment_spline)%>%
  dplyr::select(country, population,.fitted,.se.fit)

Same code runs if using broom 0.5.6

devtools::install_version("broom", version = "0.5.6", repos = "http://cran.us.r-project.org")

All online tutorials I could find present similar code that doesn't seem to work using broom 0.7

Ronak Shah

In the newer version I think it also needs data in newdata argument. You can pass the data as separate argument with map2.

library(tidyverse)
library(dslabs)

gapminder_nest <- gapminder %>% 
  group_by(country) %>% 
  nest()%>%
  mutate(splined = map(data, ~mgcv::gam(population ~ s(year, k=5, bs="tp"), data=.x))) %>%
  mutate(augment_spline = map2(splined, data, ~broom::augment(.x, newdata = .y))) %>%
  unnest(augment_spline)

Although this works but this doesn't return all the columns as the 0.5.6 version of broom does i.e se.fit,.resid,.hat, .sigma and .cooksd.This only returns .fitted column.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

mgcv gam 및 broom augment와 함께 nest 및 map 사용

분류에서Dev

mgcv access to estimate.gam function

분류에서Dev

R gam과 mgcv 사이의 패키지 충돌?

분류에서Dev

Estimated.gam 함수에 대한 mgcv 액세스

분류에서Dev

mgcv의 GAM 모델에 진행률 막대를 추가 할 수 있습니까?

분류에서Dev

AIC () 및 model $ aic은 mgcv :: gam ()에서 다른 결과를 제공합니다.

분류에서Dev

mgcv의 vis.gam에서 등고선 플롯의 색상 변경

분류에서Dev

mgcv :: gam을 사용하여 보간하고`예측`하는 방법?

분류에서Dev

기존 모델에 새 데이터를 공급하고 broom :: augment를 사용하여 예측 추가

분류에서Dev

felm은 broom :: augment / purrr에서는 작동하지 않지만 깔끔하게 작동합니다.

분류에서Dev

mgcv : gam 및 gamm 모델에서 정확한 매듭 값을 식별하는 방법은 무엇입니까?

분류에서Dev

gamm, gam 및 lme 모델에서 AIC 또는 BIC citerium을 가져옵니다. mgcv는 어떻습니까? 결과를 어떻게 믿을 수 있습니까?

분류에서Dev

How do I nest schemas using mongoose?

분류에서Dev

Using fgets in combination with strtok to transform input to tokens

분류에서Dev

Combination of security access constraints using Grails

분류에서Dev

How to kill X server using a key combination?

분류에서Dev

How can I delete a specific river using NEST?

분류에서Dev

Querying Elastic using NEST does not return any response

분류에서Dev

How to remove duplicate element based on combination of multiple attributes using xslt

분류에서Dev

Alternatives to combination to paste and parse functions using ggplot2

분류에서Dev

Using a map to return something specific

분류에서Dev

Problem using itertools and zip in combination to create dictionary from two lists of different lengths

분류에서Dev

How can I solve "unmatched double quote" error using dbus-monitor in combination with xargs?

분류에서Dev

Combination Generator

분류에서Dev

tidy() working but glance() and augment() not working from a regression model

분류에서Dev

Using Map type to start an execution of state machine

분류에서Dev

Searching using unordered map vs array

분류에서Dev

Map "fn" + "home" to screen brightness using AutoHotKey?

분류에서Dev

C++ Using std::set<> in std::map<>

Related 관련 기사

  1. 1

    mgcv gam 및 broom augment와 함께 nest 및 map 사용

  2. 2

    mgcv access to estimate.gam function

  3. 3

    R gam과 mgcv 사이의 패키지 충돌?

  4. 4

    Estimated.gam 함수에 대한 mgcv 액세스

  5. 5

    mgcv의 GAM 모델에 진행률 막대를 추가 할 수 있습니까?

  6. 6

    AIC () 및 model $ aic은 mgcv :: gam ()에서 다른 결과를 제공합니다.

  7. 7

    mgcv의 vis.gam에서 등고선 플롯의 색상 변경

  8. 8

    mgcv :: gam을 사용하여 보간하고`예측`하는 방법?

  9. 9

    기존 모델에 새 데이터를 공급하고 broom :: augment를 사용하여 예측 추가

  10. 10

    felm은 broom :: augment / purrr에서는 작동하지 않지만 깔끔하게 작동합니다.

  11. 11

    mgcv : gam 및 gamm 모델에서 정확한 매듭 값을 식별하는 방법은 무엇입니까?

  12. 12

    gamm, gam 및 lme 모델에서 AIC 또는 BIC citerium을 가져옵니다. mgcv는 어떻습니까? 결과를 어떻게 믿을 수 있습니까?

  13. 13

    How do I nest schemas using mongoose?

  14. 14

    Using fgets in combination with strtok to transform input to tokens

  15. 15

    Combination of security access constraints using Grails

  16. 16

    How to kill X server using a key combination?

  17. 17

    How can I delete a specific river using NEST?

  18. 18

    Querying Elastic using NEST does not return any response

  19. 19

    How to remove duplicate element based on combination of multiple attributes using xslt

  20. 20

    Alternatives to combination to paste and parse functions using ggplot2

  21. 21

    Using a map to return something specific

  22. 22

    Problem using itertools and zip in combination to create dictionary from two lists of different lengths

  23. 23

    How can I solve "unmatched double quote" error using dbus-monitor in combination with xargs?

  24. 24

    Combination Generator

  25. 25

    tidy() working but glance() and augment() not working from a regression model

  26. 26

    Using Map type to start an execution of state machine

  27. 27

    Searching using unordered map vs array

  28. 28

    Map "fn" + "home" to screen brightness using AutoHotKey?

  29. 29

    C++ Using std::set<> in std::map<>

뜨겁다태그

보관