本文介绍了使用 dplyr 将预测值列添加到数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一列模型的数据框,我正在尝试向其中添加一列预测值.一个最小的例子是:

exampleTable <- data.frame(x = c(1:5, 1:5),y = c((1:5) + norm(5), 2*(5:1)),组 = rep(LETTERS[1:2], each = 5))模型 <-exampleTable %>% group_by(groups) %>% do(model = lm(y ~ x, data = .))exampleTable <- left_join(tbl_df(exampleTable), 模型)估计 <-exampleTable %>% rowwise() %>% do(Est = predict(.$model, newdata = .[x"]))

如何将一列数字预测添加到 exampleTable?我尝试使用 mutate 将列直接添加到表中,但没有成功.

exampleTable <-exampleTable %>% rowwise() %>% mutate(data.frame(Pred = predict(.$model, newdata = .[x"])))

错误:预测"没有适用的方法应用于列表"类的对象

现在我使用 bind_colsestimates 添加到 exampleTable 但我正在寻找更好的解决方案.

estimates % rowwise() %>% do(data.frame(Pred = predict(.$model, newdata = .[x"])))exampleTable <- bind_cols(exampleTable, 估计)

如何一步完成?

解决方案

使用 modelr,有一个使用 tidyverse 的优雅解决方案.

输入

库(dplyr)图书馆(咕噜咕噜)图书馆(整理)# 生成问题中的输入example_table <- data.frame(x = c(1:5, 1:5),y = c((1:5) + norm(5), 2*(5:1)),组 = rep(LETTERS[1:2], each = 5))模型 <-example_table %>%group_by(groups)%>%做(模型 = lm(y ~ x,数据 = .))%>%取消分组()example_table <- left_join(tbl_df(example_table), 模型, by = "groups")

解决办法

# 生成额外的列示例_表 %>%group_by(groups)%>%做(modelr::add_predictions(., first(.$model)))

说明

add_predictions 使用给定模型向数据框添加新列.不幸的是,它只需要一个模型作为参数.认识do.使用 do,我们可以对每个组单独运行 add_prediction.

. 代表分组数据框,.$model 模型列,first() 取每组的第一个模型.

简化

只有一个模型,add_predictions 效果很好.

#取其中一个模型模型 <- example_table$model[[6]]# 生成额外的列示例_表 %>%建模师::add_predictions(模型)

食谱

如今,tidyverse 正在从 modelr 包转移到 recipes,因此这可能是该包成熟后的新方法.

I have a data frame with a column of models and I am trying to add a column of predicted values to it. A minimal example is :

exampleTable <- data.frame(x = c(1:5, 1:5),
                           y = c((1:5) + rnorm(5), 2*(5:1)),
                           groups = rep(LETTERS[1:2], each = 5))

models <- exampleTable %>% group_by(groups) %>% do(model = lm(y ~ x, data = .))
exampleTable <- left_join(tbl_df(exampleTable), models)

estimates <- exampleTable %>% rowwise() %>% do(Est = predict(.$model, newdata = .["x"]))

How can I add a column of numeric predictions to exampleTable? I tried using mutate to directly add the column to the table without success.

exampleTable <- exampleTable %>% rowwise() %>% mutate(data.frame(Pred = predict(.$model, newdata = .["x"])))

Now I use bind_cols to add the estimates to exampleTable but I am looking for a better solution.

estimates <- exampleTable %>% rowwise() %>% do(data.frame(Pred = predict(.$model, newdata = .["x"])))
exampleTable <- bind_cols(exampleTable, estimates)

How can it be done in a single step?

解决方案

Using modelr, there is an elegant solution using the tidyverse.

The inputs

library(dplyr)
library(purrr)
library(tidyr)

# generate the inputs like in the question
example_table <- data.frame(x = c(1:5, 1:5),
                            y = c((1:5) + rnorm(5), 2*(5:1)),
                            groups = rep(LETTERS[1:2], each = 5))

models <- example_table %>%
  group_by(groups) %>%
  do(model = lm(y ~ x, data = .)) %>%
  ungroup()
example_table <- left_join(tbl_df(example_table ), models, by = "groups")

The solution

# generate the extra column
example_table %>%
  group_by(groups) %>%
  do(modelr::add_predictions(., first(.$model)))

The explanation

add_predictions adds a new column to a data frame using a given model. Unfortunately it only takes one model as an argument. Meet do. Using do, we can run add_prediction individually over each group.

. represents the grouped data frame, .$model the model column and first() takes the first model of each group.

Simplified

With only one model, add_predictions works very well.

# take one of the models
model <- example_table$model[[6]]

# generate the extra column
example_table %>%
  modelr::add_predictions(model)

Recipes

Nowadays, the tidyverse is shifting from the modelr package to recipes so that might be the new way to go once this package matures.

这篇关于使用 dplyr 将预测值列添加到数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 01:00
查看更多