I have already looked a lot in various forums and many pages on parameter estimation via the optim
function of R and found nothing on how to add the gradient function in a script, so that the derivatives are calculated via some function of the R is, _deriv()_
or _deriv3()_
or via package _NumDeriv_
or package _Deriv_
. I need to calculate the gradient in order to try to improve my estimation process and it is impossible to make the derivatives at hand since my likelihood log function is very extensive.
I can make the derivatives in _Maple 16_
, but I'd like to know how to get the gradient in _R_
more automated. The code below is a Birnbaum-Saunders parameter estimation for univariate parameters with two parameters, if I can implement the gradient in this script I could implement in my code more complicated!
When compiling the code below I have sometimes got negative parameters, which is absurd, since _alpha_
, _beta_
and _t_
are positive. I imagine with the gradient this might stop happening.
library(VGAM)
alpha<-2
beta <-1
truevalue <- c(alpha,beta)
n=1000
N=300
m=matrix(ncol=2,nrow=N)
for (i in 1:N){
x <- rnorm(n,mean = 0,sd=sqrt((alpha^2)/4))
t <- beta*(1+2*x^2+2*x*sqrt(1+x^2)) #t possui distribuição birnbaum-Saunders com parâmetros alpha e beta
#t <- rbisa(n, alpha, beta)
#sum(1*(t<0))
#Função Densidade da distribuição birnbaum-saunders
f <-function(theta){
alpha <- abs(theta[1])
beta <- abs(theta[2])
d <- (1/(2*alpha*beta*sqrt(2*pi)))*(sqrt(beta/t)+(beta/t)^(3/2))*exp(- (1/(2*alpha^2))*((t/beta)+(beta/t)-2))
return(d)
}
#Forma 1 da log verossimilhança
# log.ver <- function(theta){
# alpha <- abs(theta[1])
# beta <- abs(theta[2])
# l <- sum(log(f(theta)))
# return(l)
# }
#Forma 2 da log verossimilhança
log.ver <- function(theta){
alpha <- abs(theta[1])
beta <- abs(theta[2])
l <- sum(log((1/(2*alpha*beta*sqrt(2*pi)))*(sqrt(beta/t)+ (beta/t)^(3/2))*exp(-(1/(2*alpha^2))*((t/beta)+(beta/t)-2))))
return(l)
}
alpha_0 <- 3
beta_0 <- 4
start <- c(alpha_0,beta_0)
opt <- optim(start,log.ver,method="BFGS",hessian = F,control=list(fnscale=-1))
m[i,]=opt$par
}
#Calculating the average of each column of the array of parameters m
mest=colMeans(m)
#calculating the standard deviation of each column of the array of parameters m
dest=apply(m,2,sd)
#root mean square error in the calculation of each column of the array of parameters m in relation to the true value of the parameter
eqm=function(x,opt){
N=length(x)
sqrt(sum(((x-opt)^2))/N)}
#Estimated mean squared error of each parameter
eqmest=c(eqm(x=m[,1],opt=alpha),
eqm(x=m[,2],opt=beta))
# Table with the true values of the parameters and the average
# Standard deviation and mean square error of the estimated parameters
tab=data.frame(truevalue,mean=mest,sd=dest,eqm=eqmest)
tab
Thank you very much for the help!