【问题标题】:The use of sapply in cross validationsapply 在交叉验证中的使用
【发布时间】:2013-07-23 23:39:53
【问题描述】:

我对@9​​87654323@in R 有疑问。在我的示例中,我将其用于留一法交叉验证

 ##' Calculates the LOO CV score for given data and regression prediction function
##'
##' @param reg.data: regression data; data.frame with columns 'x', 'y'
##' @param reg.fcn:  regr.prediction function; arguments:
##'                    reg.x: regression x-values
##'                    reg.y: regression y-values
##'                    x:     x-value(s) of evaluation point(s)
##'                  value: prediction at point(s) x
##' @return LOOCV score
loocv <- function(reg.data, reg.fcn)
{
  ## Help function to calculate leave-one-out regression values
  loo.reg.value <- function(i, reg.data, reg.fcn)
    return(reg.fcn(reg.data$x[-i],reg.data$y[-i], reg.data$x[i]))

  ## Calculate LOO regression values using the help function above
  n <- nrow(reg.data)
  loo.values <- sapply(seq(1,n), loo.reg.value, reg.data, reg.fcn)

  ## Calculate and return MSE
  return(???)
}

我对@9​​87654325@的问题如下:

  1. 我可以使用多个参数和函数,即sapply(X1,FUN1,X2,FUN2,..),其中X1 和X2 分别是函数FUN1 和FUN2 的函数参数。
  2. 在上面的代码中,我将1:n应用于函数loo.reg.value。但是,这个函数有多个参数,实际上是3个:整数i,回归数据reg.data和回归函数reg.fcn。如果 sapply 中的函数有多个参数,而我的 X 只包含一个参数,那么 sapply 是否将其用作“第一个参数”?所以它会和sapply(c(1:n,reg.data,reg.fcn),loo.reg.value, reg.data, reg.fcn)一样吗?

感谢您的帮助

【问题讨论】:

  • 查看此:stackoverflow.com/questions/3505701/… 以获得全面的概述,查看此:stackoverflow.com/questions/17490297/… 了解如何使用具有多个参数的多个函数。
  • @Ferdinand.kraft mapply 有一个特定的用途,这与您想要将函数应用于以函数作为参数的此处的用途不同。 mapply 和 sapply 都有用处,但我看不出 mapply 在这种情况下如何应用?
  • @GavinSimpson,回答 OP 的第一个问题,可以使用 mapply(function(f, x) f(x), list(FUN1, FUN2), list(X1, X2))。

标签: r cross-validation


【解决方案1】:

在回答第一个问题时,是的,您可以使用多个函数,但是第二个和后续函数需要传递给第一个函数,然后传递给下一个函数等。因此需要对函数进行编码以便采用附加参数和传递它们。

例如

foo <- function(x, f1, ...) f1(x, ...)
bar <- function(y, f2, ...) f2(y, ...)
foobar <- function(z, f3, ...) f3(z)

sapply(1:10, foo, f1 = bar, y = 2, f2 = foobar, z = 4, f3 = seq_len)

> sapply(1:10, foo, f1 = bar, y = 2, f2 = foobar, z = 4, f3 = seq_len)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    1    1    1    1    1    1    1    1     1
[2,]    2    2    2    2    2    2    2    2    2     2
[3,]    3    3    3    3    3    3    3    3    3     3
[4,]    4    4    4    4    4    4    4    4    4     4

这是一个愚蠢的例子,但它展示了如何将额外的参数传递给foo(),最初,作为sapply() 的... 参数的一部分。它还展示了如何让foo() 和后续函数接受额外的参数被传递,只需在函数定义中使用...和调用下一个函数,例如f2(y, ...)。请注意,我还避免了位置匹配问题,并将所有提供给 foo() 的附加参数命名。

关于问题 2,我认为您解释它的方式过于复杂。例如,您在 R 用 sapply() 迭代的内容中复制了 reg.data 和 reg.fcn 位,这是不正确的(这意味着您迭代向量 c(1:n,reg.data,reg.fcn) 中的 3 个事物,而不是 @ 987654333@).

sapply(1:n, fun, arg1, arg2) 等价于

fun(1, arg1, arg2)
fun(2, arg1, arg2)
....
fun(10, arg1, arg2)

而sapply(1:n, fun, arg1 = bar, arg2 = foobar) 相当于

fun(1, arg1 = bar, arg2 = foobar)
fun(2, arg1 = bar, arg2 = foobar)
....
fun(10, arg1 = bar, arg2 = foobar)

【讨论】:

  • 感谢您的回答。所以,我的第一印象是正确的:如果我有一个带有参数 x,y,z 的函数 FUN,那么 sapply(x,FUN,y,z) 只是意味着,我“迭代了 x,保持其他参数“不变”,对吧?
【解决方案2】:

您传递给sapply 的函数可以接受任意数量的参数(当然是在合理范围内),但它会为每个应用程序回收除第一个参数之外的所有参数。您是否尝试过运行此代码?它看起来会起作用。

【讨论】:

    猜你喜欢
    • 2015-09-06
    • 2023-03-10
    • 2018-03-09
    • 2018-06-13
    • 2012-01-13
    • 2014-08-19
    • 2015-05-13
    • 2015-05-27
    • 2011-07-09
    相关资源
    最近更新 更多