get_p_result: Run and get predicted results

Description Usage Arguments Value Examples

View source: R/get_p_result.R

Description

Run statistical model and get AUC results.

Usage

1
get_p_result(data_list, model = c("svm", "randomforest"), func = NULL)

Arguments

data_list

data list including train_x, train_y and test_x

model

base classification model.

  • logistic: Logistic regression. glm function with family = 'binomial'

  • penlog: Penalized logistic regression with LASSO penalty. glmnet in glmnet package

  • svm: Support Vector Machines. svm in e1071 package

  • randomforest: Random Forest. randomForest in randomForest package

  • lda: Linear Discriminant Analysis. lda in MASS package

  • slda: Sparse Linear Discriminant Analysis with LASSO penalty.

  • nb: Naive Bayes. naiveBayes in e1071 package

  • nnb: Nonparametric Naive Bayes. naive_bayes in naivebayes package

  • ada: Ada-Boost. ada in ada package

  • xgboost: XGBboost. xgboost in xgboost package

  • tree: Classificatin Tree. tree in tree package

  • self: You can use your self-defined function. You need to pass your self-defined function via the "func" parameter.

func

If you set "model" to "self", you have to pass your self-defined model function. This function should be able to take "x_train" and "y_train" as the first two inputs to train the model and then take "x_test" as the third input and return the predicted scores of x_test data. For example,

library(e1071)

predict_model <- function(x_train, y_train, x_test){
data_trainxy<-data.frame(x_train,y_train=as.factor(y_train))
fit_svm<-svm(y_train~.,data=data_trainxy,probability=TRUE)
pred_svm <- predict(fit_svm, x_test, probability=TRUE,decision.values = TRUE)
p_svm=as.data.frame(attr(pred_svm, "probabilities"))$"1"
return(p_svm)
}

result = get_p_result(data_list=data_list, model=c("self","randomforest"), func=predict_model)

Value

the scores predicted by models

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
library(mvtnorm)
library(MASS)

df = 10
rho = 0.5
d = 5
delta = rep(2, d)
H <- abs(outer(1:d, 1:d, "-"))
covxx = rho^H

n1_all <- n0_all <- 800
n1_p <- n0_p <- 15

x0_all = rmvt(n = n0_all, sigma = covxx, delta = rep(0, d), df = df)
x1_all = rmvt(n = n1_all, sigma = covxx, delta = delta, df = df)

x_data = rbind(x0_all, x1_all)
y_data = c(rep(0, n0_all), rep(1, n1_all))

id0 <- which(y_data == 0)
id1 <- which(y_data == 1)

id0_p <- sample(id0, n0_p)
id1_p <- sample(id1, n1_p)
id_p <- c(id0_p, id1_p)

x_pilot <- as.matrix(x_data[id_p, ])
y_pilot <- as.matrix(y_data[id_p])

n1_train <- n0_train <- n_train <- 60
n0_test <- n1_test <- 300

data_list = pilot_tfe_mvnorm_pca2(x_pilot, y_pilot, n0_train, n1_train, n0_test,
    n1_test)

result = get_p_result(data_list, model = c("svm", "randomforest"))

ShudongSun/Sample-Size-Determination documentation built on Feb. 10, 2022, 1:33 a.m.