R:调用lapply w / nearest neighbors library会产生错误

时间:2013-11-15 22:32:31

标签: r

设置:

library(kknn)

event <- c('first', 'second', 'third', 'second', 'second', 'third', 'third', 'first')
speed <- c(1.25, 2.22, 3.02, 4.22, 1.56, 2.77, 3.21, 1.55)
time <- c(667, 444, 335, 204, 393, 949, 203, 365)

model <- event ~ speed + time

df <- data.frame(event, speed, time)

这很好用:

data <- df[1,]
fit <- kknn(model, df, data, k=2, kernel="gaussian")
fit$prob

但是构建一个在迭代中使用的函数不会:

k_fit <- function(data) {
    fit <- kknn(model, df, data, k=2, kernel="gaussian")
    out <- as.data.frame(fit$prob)
}

lapply(df, k_fit)

收率:

> lapply(df, k_fit)
Error in eval(predvars, data, env) : 
  numeric 'envir' arg not of length one
Calls: lapply ... model.matrix.default -> model.frame -> model.frame.default -> eval
Execution halted

这里的迭代方法出了什么问题?谢谢。

0 个答案:

没有答案