将lm函数映射到嵌套tibble

时间:2018-03-15 14:39:33

标签: r purrr

我正在努力了解r。我在嵌套的地图函数中遇到了问题。

我想在嵌套的tibble上运行线性模型。但有几个x和y变量。

我有以下代码:

    > head(dat)
# A tibble: 6 x 28
  subject_id event_id aktivitet_id time   trin trin_ss time_to_end_trin time_hms time_ss  load velo_ms    hr resp_freq    ve
  <fct>      <fct>    <fct>        <chr> <int>   <dbl> <time>           <time>     <dbl> <dbl>   <dbl> <dbl>     <dbl> <dbl>
1 1          1        1            0:10      1     10. 03'50"           00'10"       10.   12.    3.33    NA        NA    NA
2 1          1        1            0:20      1     20. 03'40"           00'20"       20.   12.    3.33    NA        NA    NA
3 1          1        1            0:30      1     30. 03'30"           00'30"       30.   12.    3.33    NA        NA    NA
4 1          1        1            0:40      1     40. 03'20"           00'40"       40.   12.    3.33    NA        NA    NA
5 1          1        1            0:50      1     50. 03'10"           00'50"       50.   12.    3.33    NA        NA    NA
6 1          1        1            1:00      1     60. 03'00"           01'00"       60.   12.    3.33    NA        NA    NA
# ... with 14 more variables: vco2 <dbl>, vo2 <dbl>, rer <dbl>, vo2_kg <dbl>, vt <dbl>, fo2et <dbl>, fco2et <dbl>, la <dbl>,
#   rpe <dbl>, t20 <dbl>, step_freq <dbl>, step_len <dbl>, ve_vo2 <dbl>, ve_vco2 <dbl>

编辑:这是我的数据dput()

    > dput(head(dat))
structure(list(time = c("0:10", "0:20", "0:30", "0:40", "0:50", 
"1:00"), load = c(12, 12, 12, 12, 12, 12), hr = c(NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_), resp_freq = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), ve = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), vco2 = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), vo2 = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), rer = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), vo2_kg = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), vt = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), fo2et = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), fco2et = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), time_ss = c(10, 
20, 30, 40, 50, 60), time_hms = structure(c(10, 20, 30, 40, 50, 
60), class = c("hms", "difftime"), units = "secs"), trin = c(1, 
1, 1, 1, 1, 1), trin_ss = c(10, 20, 30, 40, 50, 60), time_to_end_trin = structure(c(230, 
220, 210, 200, 190, 180), class = c("hms", "difftime"), units = "secs"), 
    la = c(0.8, 0.8, 0.8, 0.8, 0.8, 0.8), rpe = c(3, 3, 3, 3, 
    3, 3), t20 = c(13.6, 13.6, 13.6, 13.6, 13.6, 13.6), velo_ms = c(3.33333333333333, 
    3.33333333333333, 3.33333333333333, 3.33333333333333, 3.33333333333333, 
    3.33333333333333), step_freq = c(176.470588235294, 176.470588235294, 
    176.470588235294, 176.470588235294, 176.470588235294, 176.470588235294
    ), step_len = c(1.13333333333333, 1.13333333333333, 1.13333333333333, 
    1.13333333333333, 1.13333333333333, 1.13333333333333), ve_vo2 = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), ve_vco2 = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_)), .Names = c("time", 
"load", "hr", "resp_freq", "ve", "vco2", "vo2", "rer", "vo2_kg", 
"vt", "fo2et", "fco2et", "time_ss", "time_hms", "trin", "trin_ss", 
"time_to_end_trin", "la", "rpe", "t20", "velo_ms", "step_freq", 
"step_len", "ve_vo2", "ve_vco2"), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))
> 

这是我的出发点。

dat_nest <- dat %>% group_by(subject_id, event_id, aktivitet_id) %>% 
nest()

    View(dat_nest)
> dat_nest
# A tibble: 1 x 4
  subject_id event_id aktivitet_id data               
  <fct>      <fct>    <fct>        <list>             
1 1          1        1            <tibble [175 x 25]>


vars_x <- c("time_ss", "vo2") # the variables I would like to put in lm function
vars_y <- c("hr", "vo2")

mod_lm <- function(d) { # this works as expected
lm(vo2 ~ time_ss, data = d)
}

dat_mod <- dat_nest %>%
mutate( dim = data %>% map(dim), 
      mod_lm = map(data, mod_lm)

但我无法弄清楚如何将我的变量组合到函数中。

我尝试过这种解决方案的变体

vars <- list(hr ~ time_ss, la ~ time_ss, vo2 ~ time_ss) %>% structure(., names=.)

mod_lm2 <- function(d) { 
  as.formula( paste0( "~lm(", vars, "~", "time_ss", ", data = .)"))

mod_lm3 <- function(d) {
    lm(data = d, formula = vars)

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

0 个答案:

没有答案
相关问题