扫帚::增强返回矩阵不能嵌套

时间:2019-12-04 11:57:36

标签: r broom

我正在尝试在lm(y〜poly(x,3),data = dat)的增强上运行扫帚。

使用该形式的扫帚:: augment返回嵌套列中的矩阵。当我尝试取消嵌套时,此操作失败,并显示类似于“无法投射多边形...

我找到了类似的问题,但没有答案 Trying to unnest broom::augment data, but R "can't cast"

library(rmarkdown)
library(tidyverse)
library(fs)

structure(list(a = c("2019-11-25", "2019-11-25", 
"2019-11-25", "2019-11-25", "2019-11-25"), 
b = c("laktat-felttest", "laktat-felttest", 
"laktat-felttest", "laktat-felttest", 
"laktat-felttest"), 
c = c("kai", "kai", "kai", "kai", "kai"), 
maaling = c(1, 2, 3, 4, 5), 
load = c(800, 850, 900, 1000, 1100), 
time_mm = c(5, 5, 4, 4, 4), 
time_ss = c(9, 0, 55, 35, 45), 
hr = c(125, 140, 140, 160, 172), 
rpe = c(2, 4, 4, 8, 9), 
laktat = c(2.7, 2.1, 2, 4.8, 10.2), 
time = c(309, 300, 295, 275, 285), 
x = c(2.58899676375405, 2.83333333333333, 
3.05084745762712, 3.63636363636364, 3.85964912280702), 
la_x = c(1.042875, 0.741176470588235, 
0.655555555555556, 1.32, 2.64272727272727)), 
class = c("tbl_df", "tbl", "data.frame"), 
row.names = c(NA, -5L)) ->
dat

dat %>% 
    group_by(a,b,c) %>% 
    nest() %>% 
    mutate(model = data %>% map( ., ~lm( data = ., laktat ~ poly(x, 3), na.action = "na.exclude")),
           tidied = model %>% map( ., broom::tidy ),
           glance = model %>% map(., broom::glance), 
           augment = map( model, broom::augment), 
           augment = map( augment, janitor::clean_names, "snake" )) ->
    model_tbl

##model_tbl %>% 
##    unnest(augment) 

在我看来,嵌套的列(其中的增量是)有一个矩阵。我不知道如何将其更改为列表列。

来自丹麦的问候 丹·奥莱森

1 个答案:

答案 0 :(得分:0)

问题poly()。它导致奇怪的列名。您可以将其替换为x + I(x^2) + I(x^3),以获得更好的列名。

dat %>% 
  group_by(a,b,c) %>% 
  nest() %>% 
  mutate(model = data %>% map( ., ~lm( data = ., laktat ~ x + I(x^2) + I(x^3), na.action = "na.exclude")),
         tidied = model %>% map( ., broom::tidy ),
         glance = model %>% map(., broom::glance), 
         augment = map( model, broom::augment),
         augment = map( augment, janitor::clean_names, "snake" )) ->
  model_tbl

更新:
我刚意识到一个问题。使用x + I(x^2) + I(x^3)poly(x, 3)并不完全相同。如果使用后者,则系数不相关,但是如果使用前者,则系数不相关。目前不知道该如何解决。