\[ λe^{-λ*x} \] ### Se aplica la pitatoria para obtener la función de verosimilitud \[ \prod^{n}_{i=1} λe^{-λ*x} = \sum^n{i=1}log(λe^{-λx})=\sum^n{i=1}log(λ)+\sum^n{i=1}log(e^{-λx}) = log(λ)n +\sum^n_{i=1}(-λx) = log(λ)*n -λ\sum^n_{i=1}(x) \] ## Paso 1: Generar los datos
library(pacman)
p_load(data.table, fixest, lattice, magrittr, ggplot2, kableExtra,dplyr)
## Installing package into 'C:/Users/Samoth/Documents/R/win-library/4.1'
## (as 'lib' is unspecified)
## also installing the dependency 'vctrs'
## Warning: unable to access index for repository http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/4.1:
## no fue posible abrir la URL 'http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/4.1/PACKAGES'
## package 'vctrs' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'vctrs'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problema al copiar C:
## \Users\Samoth\Documents\R\win-library\4.1\00LOCK\vctrs\libs\x64\vctrs.dll a C:
## \Users\Samoth\Documents\R\win-library\4.1\vctrs\libs\x64\vctrs.dll: Permission
## denied
## Warning: restored 'vctrs'
## package 'dplyr' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\Samoth\AppData\Local\Temp\RtmpwFAteC\downloaded_packages
##
## dplyr installed
## Warning in p_load(data.table, fixest, lattice, magrittr, ggplot2, kableExtra, : Failed to install/load:
## dplyr
x = 300 # Número de experimentos
lambda = 5 # Lambda modificable
dis = rexp(x, lambda) # distribución
data = data.frame(dis) # data frame
maximoDer = function(x, n, lambda){
return(-(n*log(lambda)-lambda*sum(x)))
}
ValorLambda = seq(0,60)
plot(seq(0,60), maximoDer(x=dis, n=300, lambda=ValorLambda))
MLE_estimates <- optim(fn=maximoDer, # Función de verosimilitud
par=c(1), # Estimación inicial
lower = c(-Inf, -Inf), # Límite inferior de los parámetros
upper = c(Inf, Inf), # Límite superior de los parámetros
hessian=TRUE, # Devuelve el Hessiano
method = "L-BFGS-B",
# Entradas personalizadas
n = 300,
x = dis)
# Examinar estimaciones
MLE_par <- MLE_estimates$par
MLE_SE <- sqrt(diag(solve(MLE_estimates$hessian)))
MLE <- data.table(param = c("lambda"),
estimates = MLE_par,
sd = MLE_SE)
kable(MLE)
| param | estimates | sd |
|---|---|---|
| lambda | 4.947167 | 0.2856248 |
log_like_graph = function(x = dis, n = 300){
lambda = MLE_par
loglik = log(lambda)*n - lambda*sum(x)
return (loglik)
}
# Se vectoriza el grafico
log_like_graph = Vectorize(log_like_graph)
# Se grafica
ggplot(data = data.frame(lambda = 0), mapping = aes(lambda = lambda)) + stat_function(fun = log_like_graph) + xlim(0,60) + theme_bw() +xlab("lambda") + ylab("log lik")