将函数参数简化为向量

时间:2019-03-20 00:11:15

标签: r function sum reduce

你好,我正在模拟一个由十二个参数定义的函数:

library(tidyverse)

rmixexp <- function(n, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, m1, m2, 
m3, m4, m5, m6, m7, m8, m9, m10, m11) {
  w1 * rexp(n, 1 / m1) +
    w2 * rexp(n, 1 / m2) +
    w3 * rexp(n, 1 / m2) +
    w4 * rexp(n, 1 / m5) +
    w5 * rexp(n, 1 / m5) +
    w6 * rexp(n, 1 / m6) +
    w7 * rexp(n, 1 / m7) +
    w8 * rexp(n, 1 / m8) +
    w9 * rexp(n, 1 / m9) +
    w10 * rexp(n, 1 / m10) +
    w11 * rexp(n, 1 / m11)
  }

datatibble <- tibble(
  w1 = c(1/11, 1/11),
  w2 = c(1/11, 1/11),
  w3 = c(1/11, 1/11),
  w4 = c(1/11, 1/11),
  w5 = c(1/11, 1/11),
  w6 = c(1/11, 1/11),
  w7 = c(1/11, 1/11),
  w8 = c(1/11, 1/11),
  w9 = c(1/11, 1/11),
  w10 = c(1/11, 1/11),
  w11 = c(1/11, 1/11),
  m1 = c(1/11, 1/11),
  m2 = c(1/11, 1/11),
  m3 = c(1/11, 1/11),
  m4 = c(1/11, 1/11),
  m5 = c(1/11, 1/11),
  m6 = c(1/11, 1/11),
  m7 = c(1/11, 1/11),
  m8 = c(1/11, 1/11),
  m9 = c(1/11, 1/11),
  m10 = c(1/11, 1/11),
  m11 = c(1/11, 1/11)
  )

这将导致如下繁琐的功能:

从函数中模拟...

n <- 10

loss.test <- datatibble %>% mutate(severity = 
                                       pmap(
                                         list(n, w1, w2, w3, w4, w5, 
                                              w6, w7, w8, w9, w10, w11, 
                                              m1, m2, m3, m4, m5, 
                                              m6, m7, m8, m9, m10, m11),
                                            function(n, w1, w2, w3, w4, w5, 
                                                     w6, w7, w8, w9, w10, w11, 
                                                     m1, m2, m3, m4, m5, 
                                                     m6, m7, m8, m9, m10, m11) 
                                              rmixexp(n, w1, w2, w3, w4, w5, 
                                                      w6, w7, w8, w9, w10, w11, 
                                                      m1, m2, m3, m4, m5, 
                                                      m6, m7, m8, m9, m10, m11))
                                    ) %>%
  mutate(severity = map(severity, ~ data.frame(severity = .x, 
                                                      sim = seq_along(.x))))     %>% 
unnest() %>% select(sim, severity)

有没有一种方法可以修改函数以接受固定的一系列参数,而不是分别定义每个变量?函数本身也可以定义为权重和rexp函数之和吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

如果wm的长度相同,一种解决方案是将它们提供为列表,并通过遍历每个元素进行求和:

rmixexp <- function(n, w, m) {
    ## Results placeholder
    result <- 0

    ## Loop through w and m
    for(element in 1:length(w)) {
        result <- result + w[[element]] * rexp(n, 1 / m[[element]])
    }

    return(result)
}

如果wm是向量,这也将起作用(然后您可以遍历w[element],等等。

或者,您可以使用mapply函数将相同的函数直接应用于两个列表:

rmixexp <- function(n, w, m) {
    ## Using mapply to go through both lists (same length)
    mapply.fun <- function(w, m) {w * rexp(n, 1 / m)}

    ## Summing up the result of each function
    result <- sum(unlist(mapply(mapply.fun, w, m))))

    return(result)
}

我希望这能回答您的问题。