代码之家  ›  专栏  ›  技术社区  ›  Bart Spoon

在带有偏移项的glm上使用broom(增强)和modelr(crossv_kfold)时出错

  •  2
  • Bart Spoon  · 技术社区  · 8 年前

    library(tidyverse)
    library(modelr)
    non_breaks = rpois(dim(warpbreaks)[1],20)
    warp = warpbreaks %>%
        mutate(total = breaks + non_breaks)
    

    library(broom)
    warp_no_offset = crossv_kfold(warp, k = 10) %>%
        mutate(model = map(train, ~ glm(breaks~ wool*tension, ., family=poisson))) %>%
        mutate(predicted = map2(model, test, ~ augment(.x, newdata = .y, predict.type= "response")))
    

    warp_offset = crossv_kfold(warp, k = 10) %>%
        mutate(model = map(train, ~ glm(breaks~ offset(log(total)) + wool*tension, ., family=poisson))) %>%
        mutate(predicted = map2(model, test, ~ augment(.x, newdata = .y, predict.type= "response")))
    

    它抛出了一个错误:

    Error in mutate_impl(.data, dots) : 
        Evaluation error: arguments imply differing number of rows: 5, 49.
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Hack-R    8 年前

    offset() 没有被评估你认为是如何和何时。我知道这很棘手,但解决办法很简单。

    你只需要记住使用 I()

    例如:

    warp_offset = crossv_kfold(warp, k = 10) %>%
      mutate(model = map(train, ~ glm(breaks~ I(offset(log(total))) + wool*tension, ., family=poisson))) %>%
      mutate(predicted = map2(model, test, ~ augment(.x, newdata = .y, predict.type= "response")))
    

    推荐文章