gen_error.R 4.52 KB
Newer Older
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
1
2
3
4
5
6
7
8
9
10
11

#' gen_error

#' gen_error
#' @param dataset data.frame
#' @param target column name of the target
#' @param predictors column names of the predictors
#' @param FUN_fit function for the fitting
#' @param FUN_predict function for the prediciton
#' @param error_type chosen error type 'r','s','sr','or','sor','isor'
#' @param par parameters of the input, output, structure, and remnant errors
Jean-Marie Lepioufle's avatar
typo    
Jean-Marie Lepioufle committed
12
#' @param seed seed for reproducibility
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
13
14
15
16
17
18
19
20
#' @keywords aqoslogen
#' @export
#' @examples
#' \dontrun{
#' gen_error()
#' }
#
gen_error <- function(dataset,target,predictors,FUN_fit,FUN_predict,error_type=c("r","or","sor","isor"),par=list(
Jean-Marie Lepioufle's avatar
typo    
Jean-Marie Lepioufle committed
21
22
23
24
25
      phi_f1 = 4.35/100/1.96,
      phi_g1 = 4.35/100/1.96,
      phi_h1 = 0.1,
      beta_r  = 1,
      sig_r  = 1),
Jean-Marie Lepioufle's avatar
typo    
Jean-Marie Lepioufle committed
26
27
28
      seed=NULL) {

  set.seed(seed) # for reproducibility
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

  res <- switch(error_type,

  "r"    = gen_error_r(dataset,target,predictors,FUN_fit,FUN_predict,par[["beta_r"]],par[["sig_r"]]),
  "s"    = gen_error_r(dataset,target,predictors,FUN_fit,FUN_predict,par[["phi_h1"]]),
  "sr"   = gen_error_sr(dataset,target,predictors,FUN_fit,FUN_predict,par[["phi_h1"]],par[["beta_r"]],par[["sig_r"]]),
  "or"   = gen_error_or(dataset,target,predictors,FUN_fit,FUN_predict,par[["phi_g1"]],par[["beta_r"]],par[["sig_r"]]),
  "sor"  = gen_error_sor(dataset,target,predictors,FUN_fit,FUN_predict,par[["phi_h1"]],par[["phi_g1"]],par[["beta_r"]],par[["sig_r"]]),
  "isor" = gen_error_isor(dataset,target,predictors,FUN_fit,FUN_predict,par[["phi_f1"]],par[["phi_h1"]],par[["phi_g1"]],par[["beta_r"]],par[["sig_r"]]),
  stop("error_type not included yet"))

  return(res)

}


# x,y,m perfect, with remant
gen_error_r <- function(dataset,target,predictors,FUN_fit,FUN_predict,beta_r,sig_r){
  split_dataset <- partition(dataset)
  tmp_model <- FUN_fit(split_dataset$train,target=target,predictors=predictors)
  tmp_pred <- error_remnant(FUN_predict(tmp_model,split_dataset$test),beta_r,sig_r)
  dataset <- dplyr::bind_cols(split_dataset$test,prediction=tmp_pred)
  return(dataset)
}

# x,y, perfect, m non-perfect, without remnant
gen_error_s <- function(dataset,target,predictors,FUN_fit,FUN_predict,phi_h1){
  split_dataset <- partition(dataset)
  tmp_model <- FUN_fit(split_dataset$train,target=target,predictors=predictors)
  tmp_pred <- error_structural(FUN_predict(tmp_model,split_dataset$test),phi_h1)
  dataset <- dplyr::bind_cols(split_dataset$test,prediction=tmp_pred)
  return(dataset)
}

# x,y, perfect, m non-perfect, with remnant
gen_error_sr <- function(dataset,target,predictors,FUN_fit,FUN_predict,phi_h1,beta_r,sig_r){
  split_dataset <- partition(dataset)
  tmp_model <- FUN_fit(split_dataset$train,target=target,predictors=predictors)
  tmp_pred <- error_structural(error_remnant(FUN_predict(tmp_model,split_dataset$test),beta_r,sig_r),phi_h1)
  dataset <- dplyr::bind_cols(split_dataset$test,prediction=tmp_pred)
  return(dataset)
}

# x, m perfect, y non-perfect, with remnant
gen_error_or <- function(dataset,target,predictors,FUN_fit,FUN_predict,phi_g1,beta_r,sig_r){
  dataset[[target]] <- error_output(dataset[[target]],phi_g1)
  split_dataset <- partition(dataset)
  tmp_model <- FUN_fit(split_dataset$train,target=target,predictors=predictors)
  tmp_pred <- error_remnant(FUN_predict(tmp_model,split_dataset$test),beta_r,sig_r)
  dataset <- dplyr::bind_cols(split_dataset$test,prediction=tmp_pred)
  return(dataset)
}

# x,perfect, y and m non-perfect, with remnant
gen_error_sor <- function(dataset,target,predictors,FUN_fit,FUN_predict,phi_h1,phi_g1,beta_r,sig_r){

  dataset[[target]] <- error_output(dataset[[target]],phi_g1)
  split_dataset <- partition(dataset)
  tmp_model <- FUN_fit(split_dataset$train,target=target,predictors=predictors)
  tmp_pred <- error_structural(error_remnant(FUN_predict(tmp_model,split_dataset$test),beta_r,sig_r),phi_h1)
  dataset <- dplyr::bind_cols(split_dataset$test,prediction=tmp_pred)
  return(dataset)
}

# x,y, m non-perfect, with remnant
gen_error_isor <- function(dataset,target,predictors,FUN_fit,FUN_predict,phi_f1,phi_h1,phi_g1,beta_r,sig_r){

  lapply(predictors, function(x){
     dataset[[x]] <<- error_input(dataset[[x]],phi_f1)
  })
  dataset[[target]] <- error_output(dataset[[target]],phi_g1)
  split_dataset <- partition(dataset)
  tmp_model <- FUN_fit(split_dataset$train,target=target,predictors=predictors)
  tmp_pred <- error_structural(error_remnant(FUN_predict(tmp_model,split_dataset$test),beta_r,sig_r),phi_h1)
  dataset <- dplyr::bind_cols(split_dataset$test,prediction=tmp_pred)
  return(dataset)
}