以R为单位的平均产量

以R为单位的平均产量,r,R,我将一个代码复制50次,然后我希望平均输出 正在复制的代码: output <- gbm.step(data=data.sample, gbm.x = 2:9, gbm.y = 1, family = "poisson", tree.complexity = 3,

我将一个代码复制50次,然后我希望平均输出

正在复制的代码:

output <- gbm.step(data=data.sample, 
                            gbm.x = 2:9,
                            gbm.y = 1,
                          family = "poisson",
                            tree.complexity = 3,
                            learning.rate = 0.0002,
                            bag.fraction = 0.6)
我想取50次迭代中估计的cv偏差分数的平均值


我对R很陌生,所以任何帮助都将不胜感激

您可以定义一个函数
f
,该函数将使用
replicate
运行50次。然后从每次运行中提取偏差,并取其平均值,如下所示:

f <- function(d) {
    output <- gbm.step(data=d, 
        gbm.x = 2:9,
        gbm.y = 1,
        family = "poisson",
        tree.complexity = 3,
        learning.rate = 0.0002,
        bag.fraction = 0.6)
    return(output)
}

# Use simplify = FALSE to get the result in a list, 
# rather than coerced to an array
v <- replicate(50, f(data.sample), simplify = FALSE) 
# Gather all deviance means in v in a vector
deviances <- sapply(v, function(x) x$cv.statistics$deviance.mean)
# Finally take the mean of the deviances
dev.mean <- mean(deviances)

f要多次调用函数,请使用
replicate
谢谢,效果非常好!-现在我只需要知道如何表示输出:)我不知道
gbm
对象到底是什么样的,这就是为什么我不能帮上忙的原因。如何获得偏差,是否类似于
output$deviance
?如果是的话,那么如果你有
v你的帮助是非常感谢konvas!我可以使用output$cv.statistics$deviance.mean,然后返回值。但如果我输入v$cv.statistics$deviance.mean,它将返回NULL。“[[”是什么意思?你基本上必须将它应用于
v
的每个条目(而不是
v
本身),因为
v
gbm
对象的列表。那么
偏差呢
f <- function(d) {
    output <- gbm.step(data=d, 
        gbm.x = 2:9,
        gbm.y = 1,
        family = "poisson",
        tree.complexity = 3,
        learning.rate = 0.0002,
        bag.fraction = 0.6)
    return(output)
}

# Use simplify = FALSE to get the result in a list, 
# rather than coerced to an array
v <- replicate(50, f(data.sample), simplify = FALSE) 
# Gather all deviance means in v in a vector
deviances <- sapply(v, function(x) x$cv.statistics$deviance.mean)
# Finally take the mean of the deviances
dev.mean <- mean(deviances)