dan*_*lay 8 r frequency cross-validation
我有一个data.frameR列表因子级别的数据频率表以及成功和失败的计数.我想将它从频率表转换为事件列表 - 即"表"命令的反面.具体来说,我想转此:
factor.A factor.B success.count fail.count
-------- -------- ------------- ----------
0 1 0 2
1 1 2 1
Run Code Online (Sandbox Code Playgroud)
进入这个:
factor.A factor.B result
-------- -------- -------
0 1 0
0 1 0
1 1 1
1 1 1
1 1 0
Run Code Online (Sandbox Code Playgroud)
在我看来,reshape应该这样做,甚至是一些我没有听说过的模糊的基础功能,但我没有运气.即使重复a的各行也data.frame很棘手 - 你如何传递可变数量的参数rbind?
提示?
背景:为什么?因为它比汇总的二项式数据更容易交叉验证这种数据集的逻辑拟合.
我正在用一个广义线性模型分析我作为R中的二项式回归,并希望交叉验证以控制我数据的正则化,因为我的目的是预测性的.
但是,据我所知,R中的默认交叉验证例程对于二项式数据来说并不是很好,只是跳过频率表的整行,而不是单独进行试验.这意味着轻度和重度采样因子组合在我的成本函数中具有相同的权重,这对我的数据是不合适的.
你可以试试这个:
# create 'result' vector
# repeat 1s and 0s the number of times given in the respective 'count' column
result <- rep(rep(c(1, 0), nrow(df)), unlist(df[ , c("success.count", "fail.count")]))
# repeat each row in df the number of times given by the sum of 'count' columns
data.frame(df[rep(1:nrow(df), rowSums(df[ , c("success.count", "fail.count")]) ), c("factor.A", "factor.B")], result)
# factor.A factor.B result
# 1 0 1 0
# 1.1 0 1 0
# 2 1 1 1
# 2.1 1 1 1
# 2.2 1 1 0
Run Code Online (Sandbox Code Playgroud)