问题
I have the following tibble:
# A tibble: 18 × 6
id columnFilter modelName model train.X train.Y
<int> <chr> <chr> <list> <list> <list>
1 1 groupedColumns.donr boostModel <fun> <tibble [3,984 × 17]> <fctr [3,984]>
2 2 groupedSquaredColumns.donr boostModel <fun> <tibble [3,984 × 28]> <fctr [3,984]>
3 3 groupedTransformedColumns.donr boostModel <fun> <tibble [3,984 × 17]> <fctr [3,984]>
4 4 ungroupedColumns.donr boostModel <fun> <tibble [3,984 × 17]> <fctr [3,984]>
5 5 ungroupedSquaredColumns.donr boostModel <fun> <tibble [3,984 × 28]> <fctr [3,984]>
6 6 ungroupedTransformedColumns.donr boostModel <fun> <tibble [3,984 × 17]> <fctr [3,984]>
7 7 groupedColumns.donr ldaModel <fun> <tibble [3,984 × 17]> <fctr [3,984]>
8 8 groupedSquaredColumns.donr ldaModel <fun> <tibble [3,984 × 28]> <fctr [3,984]>
9 9 groupedTransformedColumns.donr ldaModel <fun> <tibble [3,984 × 17]> <fctr [3,984]>
10 10 ungroupedColumns.donr ldaModel <fun> <tibble [3,984 × 17]> <fctr [3,984]>
11 11 ungroupedSquaredColumns.donr ldaModel <fun> <tibble [3,984 × 28]> <fctr [3,984]>
12 12 ungroupedTransformedColumns.donr ldaModel <fun> <tibble [3,984 × 17]> <fctr [3,984]>
13 13 groupedColumns.donr logitModel <fun> <tibble [3,984 × 17]> <fctr [3,984]>
14 14 groupedSquaredColumns.donr logitModel <fun> <tibble [3,984 × 28]> <fctr [3,984]>
15 15 groupedTransformedColumns.donr logitModel <fun> <tibble [3,984 × 17]> <fctr [3,984]>
16 16 ungroupedColumns.donr logitModel <fun> <tibble [3,984 × 17]> <fctr [3,984]>
17 17 ungroupedSquaredColumns.donr logitModel <fun> <tibble [3,984 × 28]> <fctr [3,984]>
18 18 ungroupedTransformedColumns.donr logitModel <fun> <tibble [3,984 × 17]> <fctr [3,984]>
As you can see, modelName
is the name of the model, stored as a function in model
.
What I want to do is for each row, call the function stored in model
, pass it train.X
and train.Y
as parameters, and store the function's output into a new column.
Conceptually, something like:
df %>% mutate(result = pmap(train.X,train.Y,model)
I've been trying to use pmap()
, but to no success.
Need some guidance here.
回答1:
invoke_map
should work after you combine train.X
and train.Y
into a list. Here's a basic example in a similar situation that could be tested. tib
mimics your situation in that x
and y
are parameters you need to provide the function. In the example, I use the runif
function which takes the parameters plus n
. I use map2
to get x
and y
wrapped in a list column called "params". Then I use the invoke_map()
function to iteratively apply functions to the params.
library(tidyverse)
# Basic example
tib <- tribble(
~fun, ~x, ~y,
runif, -1, 1,
runif, -10, 10,
runif, -3,3
)
tib
#> # A tibble: 3 × 3
#> fun x y
#> <list> <dbl> <dbl>
#> 1 <fun> -1 1
#> 2 <fun> -10 10
#> 3 <fun> -3 3
tib %>%
mutate(params = map2(x, y, list)) %>%
mutate(result = invoke_map(fun, params, n = 5))
#> # A tibble: 3 × 5
#> fun x y params result
#> <list> <dbl> <dbl> <list> <list>
#> 1 <fun> -1 1 <list [2]> <dbl [5]>
#> 2 <fun> -10 10 <list [2]> <dbl [5]>
#> 3 <fun> -3 3 <list [2]> <dbl [5]>
Now we just need to apply the same procedure to your example. This should work.
df %>%
mutate(params = map2(train.X, train.Y, list)) %>%
mutate(result = invoke_map(model, params))
来源:https://stackoverflow.com/questions/42604498/use-functions-and-parameters-stored-in-list-cols-purrr