ajout de rGAL et testparGAL

This commit is contained in:
François Pelletier 2014-01-04 13:02:45 -05:00
parent d25ec6ae1d
commit bdf752111e
8 changed files with 160 additions and 0 deletions

View file

@ -15,6 +15,7 @@
#' @author Francois Pelletier
cfGAL <- function(u,param,type="mu",log=FALSE)
{
testparGAL(param,type,log)
if(log)
{
if(type=="mu")

View file

@ -15,6 +15,7 @@
#' @author Francois Pelletier
changetypeGAL <- function(param,type="mu",target="kappa",log=FALSE)
{
testparGAL(param,type,log)
if(log)
{
if(type=="mu" && target=="kappa")

View file

@ -8,6 +8,7 @@
#' @author Francois Pelletier
dGAL <- function(x,param,type="mu",log=FALSE)
{
testparGAL(param,type,log)
if(log)
{
if(type=="mu")

View file

@ -15,6 +15,7 @@
#' @author Francois Pelletier
mgfGAL <- function(u,param,type="mu",log=FALSE)
{
testparGAL(param,type,log)
if(log)
{
if(type=="mu")

52
R/rGAL.R Normal file
View file

@ -0,0 +1,52 @@
# Random number generator for the GAL distribution
#
# Author: Francois Pelletier
#
# LGPL 3.0
###############################################################################
#' Random number generator for the GAL distribution
#' @param n number of observations
#' @param param Parameter vector
#' @param type Choose between "mu" or "kappa" parametrization
#' @param log Logical for log-parameters
#' @return A vector of random numbers
#'
#' @author Francois Pelletier
rGAL <- function(n,param,type="mu",log=FALSE)
{
testparGAL(param,type,log)
if(log)
{
if(type=="mu")
{
rGAL(n,changetypeGAL(param,type="mu",target="kappa"),type="kappa",log=log)
}
if(type=="kappa")
{
# simulation de deux variables gamma
rgamma1 <- rgamma(n, shape = exp(param[4]), scale = 1/exp(param[3]))
rgamma2 <- rgamma(n, shape = exp(param[4]), scale = exp(param[3]))
# simulation de la variable GAL
exp(param[1]) + exp(param[2])/sqrt(2)*(rgamma1 - rgamma2)
}
}
else
{
if(type=="mu")
{
rGAL(n,changetypeGAL(param,type="mu",target="kappa"),type="kappa",log=log)
}
if(type=="kappa")
{
# simulation de deux variables gamma
rgamma1 <- rgamma(n, shape = param[4], scale = 1/param[3])
rgamma2 <- rgamma(n, shape = param[4], scale = param[3])
# simulation de la variable GAL
param[1] + param[2]/sqrt(2)*(rgamma1 - rgamma2)
}
}
}

53
R/testparGAL.R Normal file
View file

@ -0,0 +1,53 @@
# Check for the validity of a parameter vector. Stop at error.
#
# Author: Francois Pelletier
#
# LGPL 3.0
###############################################################################
#' Check for the validity of a parameter vector. Stop at error.
#' @param param Parameter vector
#' @param type Choose between "mu" or "kappa" parametrization
#' @param log Logical for log-parameters
#' @return logical
#'
#' @author Francois Pelletier
testparGAL <- function(param,type="mu",log=FALSE)
{
if(log)
{
if(type=="mu")
{
if(exp(param[2])<=0)
stop("param 2 must be positive")
if(exp(param[4])<=0)
stop("param 4 must be positive")
}
if(type=="kappa")
{
if(exp(param[2])<=0)
stop("param 2 must be positive")
if(exp(param[4])<=0)
stop("param 4 must be positive")
}
}
else
{
if(type=="mu")
{
if(param[2]<=0)
stop("param 2 must be positive")
if(param[4]<=0)
stop("param 4 must be positive")
}
if(type=="kappa")
{
if(param[2]<=0)
stop("param 2 must be positive")
if(param[4]<=0)
stop("param 4 must be positive")
}
}
TRUE
}