R在函数内嵌入函数

时间:2014-05-22 21:18:53

标签: r function glm

我正在尝试编写一个函数,该函数将使用不同的阈值对逻辑回归模型执行k折交叉验证。通过阈值,我的意思是模型输出被转换为1或0的预测的概率。例如,使用.4的阈值,.42的概率将被编码为1的预测。

要使用逻辑回归运行交叉验证,我需要创建自己的成本函数(默认计算MSE)并将其提供给cv.glm()函数。如果我使用静态阈值,下面的函数将起作用,但我希望在每个循环中更改阈值,因此我将我的成本函数嵌入到循环中。我收到错误'我找不到的对象'。有没有办法可以使用嵌入函数中未指定的参数在函数内创建新函数?

logit.CV<-function(data, model, K, firstThreshold, lastThreshold) {
    error<-NULL

for(i in seq_along(firstThreshold:lastThreshold) {   

    costFunction<-function(y, pred) {
        pred<-ifelse(pred > (i+firstThreshold-1)/10, 1, 0)
        mean(abs(y-pred) > .5) 
    }

error[i]<-cv.glm(amData, logit.mod, cost=costFunction, K=10)$delta[1]

}

print(error)

}

1 个答案:

答案 0 :(得分:1)

这样做看起来并不存在任何内在错误。这个例子似乎有用

runner<-function(f, n) f(n)

for(i in 1:10) {
   pepper<-function(n) {
       rep(n,i)
   }
   print(runner(pepper, letters[i]))
}

因此,cv.glm调用函数的方式必须具体。

怎么样?
logit.CV<-function(data, model, K, firstThreshold, lastThreshold) {
    error<-NULL

    getCostFunction<-function(i) {
        function(y, pred) {
            pred<-ifelse(pred > (i+firstThreshold-1)/10, 1, 0)
            mean(abs(y-pred) > .5) 
        }
    }

    for(i in seq_along(firstThreshold:lastThreshold) {   
        error[i] <- cv.glm(amData, logit.mod, cost=getCostFunction(i), K=10)$delta[1]
    }

    print(error)
}

如果仍然不起作用,也许你可以使用包中的测试数据制作一个可重复的例子,这样其他人就可以实际运行它并尝试一下。

相关问题