Maintenance is scheduled between 16:00 and 23:59 CEST (14:00 and 21:59 UTC) on Thursday 2021-09-30. The system may be unavailable at any time during this timeframe. Please plan accordingly.

Commit 9ec2242f authored by Jean-Marie Lepioufle's avatar Jean-Marie Lepioufle
Browse files

first commit

parents
Package: errorgen
Type: Package
Version: 0.0.1
Authors@R: c(person("Jean-Marie", "Lepioufle", , "jml@nilu.no", role=c("aut","cre")))
Title: aqgen
Description: Generation of error in a dataset.
Depends:
R (>= 3.4.1)
Imports:
stats,
dplyr,
utils
Suggests:
knitr,
VignetteBuilder: knitr
License: GPL-3
Encoding: UTF-8
LazyData: false
RoxygenNote: 6.1.1
# Generated by roxygen2: do not edit by hand
export(error_input)
export(error_output)
export(error_remnant)
export(error_structural)
export(gen_error)
export(partition)
#' error_input
#' error_input
#' @param x true input
#' @param phi_f1 phi_f1
#' @keywords aqoslogen
#' @export
#' @examples
#' \dontrun{
#' error_input()
#' }
#
error_input <- function(x,phi_f1=3/100){
res <- x
n <- length(res)
res <- stats::rnorm(n,x,sd=abs(phi_f1)*x)
return(res)
}
#' error_output
#' error_output
#' @param y true output
#' @param phi_g1 phi_g1
#' @keywords aqoslogen
#' @export
#' @examples
#' \dontrun{
#' error_output()
#' }
#
error_output <- function(y,phi_g1=3/100){
res <- y
n <- length(res)
res <- stats::rnorm(n,y,sd=abs(phi_g1)*y)
return(res)
}
#' error_structural
#' error_structural
#' @param m true model output
#' @param phi_h1 phi_h1
#' @keywords aqoslogen
#' @export
#' @examples
#' \dontrun{
#' error_structural()
#' }
#
error_structural <- function(m,phi_h1=8/100){
n <- length(m)
res <- stats::rnorm(n,m,sd=abs(phi_h1)*m)
return(res)
}
#' error_remnant
#' error_remnant
#' @param y_hat true prediction
#' @param beta_r beta_r
#' @param sigma_r sigma_r
#' @keywords aqoslogen
#' @export
#' @examples
#' \dontrun{
#' error_remnant()
#' }
#
error_remnant <- function(y_hat,beta_r=0.8,sigma_r=0.5){
n <- length(y_hat)
res <- 1/beta_r*stats::rnorm(n,y_hat,sigma_r)
return(res)
}
#' 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
#' @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(
phi_f1 = list(mean=5.64/1.96,sd=10),
phi_g1 = list(mean=5.64/1.96,sd=10),
phi_h1 = list(mean=0.1,sd=0.1),
beta_r = list(mean=1,sd=2),
sig_r = list(df=1,scale=0.2))) {
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)
}
#' partition
#' partition
#' @param dataset list or tibble
#' @param train ratio for training
#' @param test ratio for testing
#' @param seed seed
#' @keywords errorgen
#' @export
#' @examples
#' \dontrun{
#' partition()
#' }
#
partition <- function(dataset,train=0.8,test=0.2,seed=NULL){
if (!is.null(seed)) set.seed(seed)
fact <- sample(c("train","test"),dim(dataset)[1],prob=c(train,test),replace=TRUE)
res <- split(dataset,fact)
return(res)
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/error.R
\name{error_input}
\alias{error_input}
\title{error_input
error_input}
\usage{
error_input(x, phi_f1 = 3/100)
}
\arguments{
\item{x}{true input}
\item{phi_f1}{phi_f1}
}
\description{
error_input
error_input
}
\examples{
\dontrun{
error_input()
}
}
\keyword{aqoslogen}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/error.R
\name{error_output}
\alias{error_output}
\title{error_output
error_output}
\usage{
error_output(y, phi_g1 = 3/100)
}
\arguments{
\item{y}{true output}
\item{phi_g1}{phi_g1}
}
\description{
error_output
error_output
}
\examples{
\dontrun{
error_output()
}
}
\keyword{aqoslogen}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/error.R
\name{error_remnant}
\alias{error_remnant}
\title{error_remnant
error_remnant}
\usage{
error_remnant(y_hat, beta_r = 0.8, sigma_r = 0.5)
}
\arguments{
\item{y_hat}{true prediction}
\item{beta_r}{beta_r}
\item{sigma_r}{sigma_r}
}
\description{
error_remnant
error_remnant
}
\examples{
\dontrun{
error_remnant()
}
}
\keyword{aqoslogen}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/error.R
\name{error_structural}
\alias{error_structural}
\title{error_structural
error_structural}
\usage{
error_structural(m, phi_h1 = 8/100)
}
\arguments{
\item{m}{true model output}
\item{phi_h1}{phi_h1}
}
\description{
error_structural
error_structural
}
\examples{
\dontrun{
error_structural()
}
}
\keyword{aqoslogen}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/gen_error.R
\name{gen_error}
\alias{gen_error}
\title{gen_error
gen_error}
\usage{
gen_error(dataset, target, predictors, FUN_fit, FUN_predict,
error_type = c("r", "or", "sor", "isor"), par = list(phi_f1 =
list(mean = 5.64/1.96, sd = 10), phi_g1 = list(mean = 5.64/1.96, sd =
10), phi_h1 = list(mean = 0.1, sd = 0.1), beta_r = list(mean = 1, sd =
2), sig_r = list(df = 1, scale = 0.2)))
}
\arguments{
\item{dataset}{data.frame}
\item{target}{column name of the target}
\item{predictors}{column names of the predictors}
\item{FUN_fit}{function for the fitting}
\item{FUN_predict}{function for the prediciton}
\item{error_type}{chosen error type 'r','s','sr','or','sor','isor'}
\item{par}{parameters of the input, output, structure, and remnant errors}
}
\description{
gen_error
gen_error
}
\examples{
\dontrun{
gen_error()
}
}
\keyword{aqoslogen}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/utils.R
\name{partition}
\alias{partition}
\title{partition
partition}
\usage{
partition(dataset, train = 0.8, test = 0.2, seed = NULL)
}
\arguments{
\item{dataset}{list or tibble}
\item{train}{ratio for training}
\item{test}{ratio for testing}
\item{seed}{seed}
}
\description{
partition
partition
}
\examples{
\dontrun{
partition()
}
}
\keyword{errorgen}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment