老师,R语言中有一个KNN补值函数叫impute.knn,该函数的源代码中有一个内部函数叫knnimp函数,与该函数中KNN的补值计算流程紧密相关,但是我却找不到这个内部函数的源代码,请问老师,这个内部函数knnimp函数的源代码是否可以找到?还是这个函数的代码是不公开的呢,所以才找不到呢?我对这部分的学习非常渴望,希望可以得到您的帮助,先谢谢您的回答。
impute.knn函数基本函数代码如下,倒数第二行是我找不到的源代码函数。
impute.knn <- function(data, k = 10, rowmax = 0.5, colmax = 0.8, maxp = 1500,
rng.seed = 362436069)
{
rng.state <- NULL
if (exists(".Random.seed")) {
rng.state <- .Random.seed
}
set.seed(rng.seed)
x <- data
p <- nrow(x)
col.nas <- drop(rep(1, p) %*% is.na(x)) #matrix multiplication #count NA number in each sample
if (any(col.nas > colmax * p)) {
stop(paste("a column has more than", format(round(colmax * 100)), "% missing values!"))
}
x <- knnimp(x, k, maxmiss = rowmax, maxp = maxp) #???????why this function cannot be found
return(list(data = x, rng.seed = rng.seed, rng.state = rng.state))
}
> library('impute')
> getAnywhere ('knnimp')
A single object matching ‘knnimp’ was found
It was found in the following places
namespace:impute
with value
function (x, k = 10, maxmiss = 0.5, maxp = 1500)
{
pn <- dim(x)
dn <- dimnames(x)
p <- as.integer(pn[1])
n <- as.integer(pn[2])
imiss <- is.na(x)
x[imiss] <- 0
irmiss <- drop(imiss %*% rep(1, n))
imax <- trunc(maxmiss * n)
imax <- irmiss > imax
simax <- sum(imax)
if (simax > 0) {
warning(paste(simax, "rows with more than", format(round(maxmiss *
100, 1)), "% entries missing;\n", "mean imputation used for these rows"))
irmiss <- irmiss[!imax]
imiss.omit <- imiss[imax, , drop = FALSE]
imiss <- imiss[!imax, ]
xomit <- x[imax, , drop = FALSE]
x <- x[!imax, ]
discards <- seq(imax)[imax]
p <- as.integer(p - simax)
}
storage.mode(imiss) <- "integer"
storage.mode(irmiss) <- "integer"
storage.mode(x) <- "double"
if (p <= maxp)
ximp <- knnimp.internal(x, k, imiss, irmiss, p, n, maxp = maxp)
else ximp <- knnimp.split(x, k, imiss, irmiss, p, n, maxp = maxp)
imiss.new <- is.na(ximp)
newmiss <- any(imiss.new)
if ((simax > 0) | newmiss) {
xbar <- mean.miss(x, imiss = imiss)
if (newmiss)
ximp <- meanimp(ximp, imiss.new, xbar)
if (simax > 0) {
xomit <- meanimp(xomit, imiss.omit, xbar)
xout <- array(0, dim = pn)
xout[!imax, ] <- ximp
xout[imax, ] <- xomit
ximp <- xout
}
}
dimnames(ximp) <- dn
ximp
}
<bytecode: 0x65c8d18>
<environment: namespace:impute>
很好,一般的直接这样找就行
如果getanywhere找不到,说明他没有直接暴露在包的用户接口中,它可能是一个非导出的函数或者一个C层面的函数