【问题标题】:apply linear model to nested data将线性模型应用于嵌套数据
【发布时间】:2018-07-20 09:06:27
【问题描述】:

我想将线性模型应用于 R 中的嵌套数据集。我的代码遇到错误,我无法弄清楚。

country_model <- function(df) {
  lm(lifeExp ~ year, data = df)
}

apminder.model  <- gapminder.nested %>% 
 mutate(model = map(data, country_model), 
        coef = map(model, broom::tidy))

mutate_impl(.data, dots) 中的错误: 评估错误:无法将类型“闭包”强制转换为“字符”类型的向量。

【问题讨论】:

  • 试试model = map(data, ~ country_model)。您能否提供一个可重现的示例?
  • gapminder.model % + mutate(model = map(data,~country_model), + coef = map(model, broom::tidy)) mutate_impl(.数据,点):评估错误:没有可识别的区域名称。SRY 弹出一个新错误
  • 这就像一个魅力country_model &lt;- function(df) { lm(Sepal.Length ~ Sepal.Width, data = df) };iris %&gt;% as.tibble() %&gt;% nest(-Species) %&gt;% mutate(model=map(data, country_model), coef = map(model, broom::tidy)) 所以你的gapminder.nested 可能是问题

标签: r


【解决方案1】:

@ANG 的评论有正确的解决方案,函数前面缺少波浪号:

model = map(data, ~ country_model)

你遇到的新错误

## Evaluation error: no recognized region names

是因为你在purrr 之后加载了maps 包,而map 函数被屏蔽了。简单的解决方案是使用purrr::map 而不仅仅是map

【讨论】:

    猜你喜欢
    • 2020-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-16
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多