Completar el código adjunto de tal manera que la función de curvas de riesgo incluya también rutinas para las ditribuciones: gompertz, lognormal, log_logistica y gamma generalizada.
Importamos las siguientes librerias
library(eha) #Gompertz
library(actuar) #Log_logistica
##
## Attaching package: 'actuar'
## The following objects are masked from 'package:eha':
##
## dllogis, pllogis, qllogis, rllogis
## The following objects are masked from 'package:stats':
##
## sd, var
## The following object is masked from 'package:grDevices':
##
## cm
library(ggamma) #Gamma_generalizada
La funcion tiene por defecto 100 simulaciones
riesgo_distr <- function(distr, pe=NULL, pg = NULL, sh = NULL, sc = NULL, mnln=NULL, sdln=NULL,pll=NULL,shll=NULL,scll=NULL,pgg=NULL,shgg=NULL,scgg=NULL,pgo=NULL,shgo=NULL, scgo=NULL ){
if(distr == "exp"){ #exponencial
rate <- pe
r <- rexp(100, rate = rate)
fd <- dexp(r, rate = rate)
Fd <- pexp(r, rate = rate)
}
if(distr == "gamma"){ #gamma
r <- rgamma(100, pg)
fd <- dgamma(r, pg)
Fd <- pgamma(r, pg)
}
if(distr == "weibull"){ # weibull
r <- rweibull(100, shape = sh, scale = sc)
fd <- dweibull(r, shape = sh, scale = sc)
Fd <- pweibull(r, shape = sh, scale = sc)
}
if(distr == "lnorm"){ #lognormal
r<-rlnorm(100, meanlog = mnln, sdlog = sdln)
fd<-dlnorm(r, meanlog = mnln, sdlog = sdln)
Fd <- plnorm(r, meanlog = mnln, sdlog = sdln)
}
if(distr == "llogis"){ #log_logistica
r <- rllogis(100, shape=shll, scale = scll)
fd <- dllogis(r, shape=shll, scale = scll)
Fd <- pllogis(r, shape=shll, scale = scll)
}
if(distr == "ggamma"){ #gamma_generalizada
r <- rggamma(100, a=1/pgg, b=shgg, k=scgg)
fd <- dggamma(r, a=1/pgg, b=shgg, k=scgg)
Fd <- pggamma(r, a=1/pgg, b=shgg, k=scgg)
}
if(distr == "gompertz"){ #gompertz
r <- rgompertz(100, shape = shgo, scale = scgo, pgo)
fd <- dgompertz(r, shape = shgo, scale = scgo, pgo)
Fd <- pgompertz(r, shape = shgo, scale = scgo, pgo)
}
S <- 1 - Fd
hz <- fd/S
plot(sort(hz))
}
riesgo_distr("exp", pe= 2)
## Warning in plot.window(...): relative range of values ( 56 * EPS) is small (axis
## 2)
riesgo_distr("gamma",pg= 5)
riesgo_distr("weibull",sc=2, sh=3)
riesgo_distr("lnorm", mnln = 0, sdln = 0.1)
riesgo_distr("llogis", shll = 3, scll = 2)
riesgo_distr("ggamma",pgg=3,shgg=2,scgg=1)
riesgo_distr("gompertz",pgo=10,shgo=2,scgo=3)
Munero de simulaciones a elegir
riesgo_distr2 <- function(distr, nsims, ...){#distribucion, simulaciones
if(distr == "exp"){# Exponencial
r <- rexp(nsims, ...)
fd <- dexp(r, ...)
Fd <- pexp(r, ...)
}
if(distr == "gamma"){# Gamma
r <- rgamma(nsims, ...)
fd <- dgamma(r, ...)
Fd <- pgamma(r, ...)
}
if(distr == "weibull"){# Weibull
r <- rweibull(nsims, ...)
fd <- dweibull(r, ...)
Fd <- pweibull(r, ...)
}
if(distr == "lnorm"){# Lognormal
library(actuar)
r<-rlnorm(nsims, ...)
fd<-dlnorm(r, ...)
Fd <- plnorm(r, ...)
}
if(distr == "llogis"){# Log_logistica
library(actuar)
r <- rllogis(nsims, ...)
fd <- dllogis(r, ...)
Fd <- pllogis(r, ...)
}
if(distr == "ggamma"){# Gamma_generalizada
library(ggamma)
r <- rggamma(nsims, ...)
fd <- dggamma(r, ...)
Fd <- pggamma(r, ...)
}
if(distr == "gompertz"){# Gompertz
library(eha)
r <- rgompertz(nsims, ...)
fd <- dgompertz(r, ...)
Fd <- pgompertz(r, ...)
}
S <- 1 - Fd
hz <- fd/S
plot(sort(hz))
}
riesgo_distr2("exp",1000,2)
riesgo_distr2("gamma",1000,5)
riesgo_distr2("weibull",1000,2,3)
riesgo_distr2("lnorm", nsims = 1000, 0, 0.1)
riesgo_distr2("llogis", nsims = 1000,3,2)
riesgo_distr2("ggamma", nsims = 1000, 3,2,1)
riesgo_distr2("gompertz", nsims = 1000,10,2,3)