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