Re: [问题] 写函数喂入data.frame疑问

楼主: celestialgod (天)   2016-03-13 15:44:50
※ 引述《gbd37 (有够瞎)》之铭言:
: [问题类型]:
: 程式咨询(我想用R 做某件事情,但是我不知道要怎么用R 写出来)
: [软件熟悉度]:
: 入门(写过其他程式,只是对语法不熟悉)
: [问题叙述]:
: 自已写函数,如下方连结
: 可这样喂入data很受限,只能喂入分类变量名字要是'y'
: 现在输入是cv.log(data,0.5)
: ________________________________________
: 我在想输入是cv.log(data$y,0.5)像这样,就方便很多
: 请问我函数内该如何修改,麻烦指导小弟
: 谢谢Orz
: [程式范例]: http://pastie.org/10757810
: [环境叙述]: win10 R:3.2.3
: [关键字]:function logistic data.frame
直接使用好用的formula吧!!
好读版: http://pastebin.com/U42Sq7Xx
library(magrittr) # %>%, %<>%
library(data.table) # data.table, setnames
library(dplyr) # filter, mutate
library(purrr) # map_dbl
# data generation
logit_f <- function(x, b) 1 / (1 + exp(x %*% b))
dat <- matrix(rnorm(1000), 100, 10) %>%
cbind(map_dbl(logit_f(cbind(1, .), runif(11, -5, 5)),
~rbinom(1, 1, .))) %>%
data.table %>% setnames(c(paste0("X", 1:10), "Y"))
# k-fold cv index
cv_index_f <- function(numSampleSize, numFold){
cvIndex <- rep(1:numFold, each = floor(numSampleSize / numFold))
m <- numSampleSize %% numFold
if (m > 0)
cvIndex %<>% c(1:m)
return(cvIndex %>% sample(length(.)))
}
# cv function
cv_f <- function(formula, data, threshold = 0.5, kfold = NULL){
y <- model.frame(formula, data) %>% model.response
if (is.null(kfold) || kfold == 0)
kfold <- nrow(data)
cvIndex <- cv_index_f(nrow(data), kfold)
predictRes <- vector('numeric', nrow(data))
for (i in 1:kfold)
{
model <- filter(data, cvIndex != i) %>%
glm(formula, data = ., binomial)
predictRes[cvIndex == i] <- filter(data, cvIndex == i) %>%
predict(model, ., type='response')
}
return(mean((predictRes > threshold) == y))
}
cv_f(Y ~ X1 + X2 + X3, dat)
cv_f(Y ~ ., dat)
dat %<>% setnames("Y", "sss")
cv_f(sss ~ ., dat)
作者: gbd37 (我想抓波波)   2016-03-13 17:10:00
受教了 谢谢Orz

Links booklink

Contact Us: admin [ a t ] ucptt.com