怎么查看php函数源码,R如何查看各函数的源代码

作为一个开源软件,R的一个非常大的优点就是我们可以随意查看所有算法的源代码,在对这些源代码进行分析的过程中不仅可以加深对算法的认识,而且可以大步提高对R语言的掌握程度。所以接下来我重点写点关于各统计方法的R语言源代码的解释。今天先对如何查看源代码做点介绍。

最直接的方法当然是直接键入函数,大部分函数源代码就可以直接显现出来

> fivenum

function (x, na.rm = TRUE)

{

xna

if (na.rm)

x

else if (any(xna))

return(rep.int(NA, 5))

x

n

if (n == 0)

rep.int(NA, 5)

else {

n4

d

0.5 * (x[floor(d)] + x[ceiling(d)])

}

}

还有些函数直接键入出不来源代码,主要是因为R是面向对象设计的程序语言,不同的对象,计算方式也不同,所以要通过methods()来进一步定义具体的查看对象

> mean

function (x, ...)

UseMethod("mean")

不行,用methods查看

> methods(mean)

[1] mean.data.frame mean.Date       mean.default    mean.difftime

[5] mean.POSIXct    mean.POSIXlt

任意选择一个进行查看

> mean.data.frame

function (x, ...)

{

msg ) is deprecated.\n Use colMeans() or sapply(*, mean) instead."

warning(paste(msg, collapse = ""), call. = FALSE, domain = NA)

sapply(X = x, FUN = mean, ...)

}

查看包里的函数过程一样,例如查看神经网络的源代码过程如下

> library(nnet)

> nnet

function (x, ...)

UseMethod("nnet")

> methods(nnet)

[1] nnet.default nnet.formula

> nnet.default

function (x, y, weights, size, Wts, mask = rep(TRUE, length(wts)),

linout = FALSE, entropy = FALSE, softmax = FALSE, censored = FALSE,

skip = FALSE, rang = 0.7, decay = 0, maxit = 100, Hess = FALSE,

trace = TRUE, MaxNWts = 1000, abstol = 1e-04, reltol = 1e-08,

...)

{

net

x

y

if (any(is.na(x)))

stop("missing values in 'x'")

if (any(is.na(y)))

stop("missing values in 'y'")

if (dim(x)[1L] != dim(y)[1L])

stop("nrows of 'x' and 'y' must match")

if (linout && entropy)

stop("entropy fit only for logistic units")

if (softmax) {

linout

entropy

}

if (censored) {

linout

entropy

softmax

}

net$n

net$nunits

net$nconn

net$conn

net

if (skip)

net

net$n[2L], net$nunits - 1L))

if ((nwts

stop("no weights to fit")

if (nwts > MaxNWts)

stop(gettextf("too many (%d) weights", nwts), domain = NA)

nsunits

if (linout)

nsunits

net$nsunits

net$decay

net$entropy

if (softmax && NCOL(y) < 2L)

stop("'softmax = TRUE' requires at least two response categories")

net$softmax

net$censored

if (missing(Wts))

if (rang > 0)

wts

else wts

else wts

if (length(wts) != nwts)

stop("weights vector of incorrect length")

if (length(mask) != length(wts))

stop("incorrect length of 'mask'")

if (trace) {

cat("# weights: ", length(wts))

nw

if (nw < length(wts))

cat(" (", nw, " variable)\n", sep = "")

else cat("\n")

flush.console()

}

if (length(decay) == 1L)

decay

.C(VR_set_net, as.integer(net$n), as.integer(net$nconn),

as.integer(net$conn), as.double(decay), as.integer(nsunits),

as.integer(entropy), as.integer(softmax), as.integer(censored))

ntr

nout

if (missing(weights))

weights

if (length(weights) != ntr || any(weights < 0))

stop("invalid weights vector")

Z

storage.mode(weights)

tmp

wts = as.double(wts), val = double(1), as.integer(maxit),

as.logical(trace), as.integer(mask), as.double(abstol),

as.double(reltol), ifail = integer(1L))

net$value

net$wts

net$convergence

tmp

nout), as.double(net$wts))$tclass, ntr, nout)

dimnames(tmp)

net$fitted.values

tmp

dimnames(tmp)

net$residuals

.C(VR_unset_net)

if (entropy)

net$lev

if (softmax)

net$lev

net$call

if (Hess)

net$Hessian

class(net)

net

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值