16. R的bnlearn包中的score函数的探究

  • 这里从代码的角度探索一下bnlearn的很常用的score函数。
  • 代码来自R的bnlearn包
  1. bnlearn的score是经常用到的一个函数,它是对网络整体打分:
example.score[i] = score(network.temp,x,type = “bic”) # 默认是bic类型
  1. 注意打分有离散和连续(以及混合)之分,以bic为例,离散是“bic”,连续是“bic-g”。但是由于score默认是bic类的打分,所以如下代码:
example.score[i] = score(network.temp,x) # 默认是bic类型

并没有指定的情况下,score函数会自行判断离散还是连续与否:frontend-score.R中有score的代码

# frontend-score.R中有score的代码
  if (type %in% discrete.data.types)
    score(object, data = data, type = "bic", ...)
  else if (type == "continuous")
    score(object, data = data, type = "bic-g", ...)
  else if (type == "mixed-cg")
    score(object, data = data, type = "bic-cg", ...)
  1. 对比之下,per.node.score就是对单独节点打分
reference.score = per.node.score(network = chowliu.network, score = "bic",
                                   targets = nodes, extra.args = extra.args, data = x)

per.node.score和score的关系可以在frontend-score.R的score函数的具体实现中看到:

# compute the node contributions to the network score.
  local = per.node.score(network = x, data = data, score = type,
            targets = names(x$nodes), extra.args = extra.args, debug = debug)

  # 这里注意by.node=true的话直接返回对每个节点打分的分值,否则返回它们的和
  if (by.node)
    return(local)
  else
    return(sum(local))

这里注意by.node=true的话直接返回对每个节点打分的分值,否则返回它们(local)的作为score函数的返回值,注意local是每个节点打分的结果。这个by.node参数的值默认是false,也就是默认为对网络整体进行打分

score = function(x, data, type = NULL, ..., by.node = FALSE, debug = FALSE)

综上可以看到score就是单个节点打分之和
4. 讲到这里,我们如果想对单个节点进行打分实际上有2种方法可以选择,一是调用score,并且设置by.node=TRUE;二是直接调用per.node.score(),这里推荐有官方接口并且需要设置较少参数的前者。
5. 进一步的,score函数的本质既然是单个节点打分之和,那么单个节点打分又是怎么打的呢?且看另一篇单独分析的博客《R的bnlearn包中的per.node.score函数的学习探究》
https://blog.csdn.net/qq_42147816/article/details/105000794

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值