<div dir="ltr"><div class="gmail_default" style="font-family:trebuchet ms,sans-serif">Ana Paula,<br><br>A optim() otimiza qualquer função. Nem precisa ser uma função de verossimilhança. Ela pode ser usada para definir delineamentos ótimos, máximo de funções como curvas de lactação, enfim, serve à propósitos gerais. Sendo assim, depende do usuário escrever a função objetivo e passar. Abaixo seguem exemplos da gama com a optim() e da weibull com optim(), nlm(), survival::survreg() e bbmle::mle2().<br>
</div>​<br><span style="font-family:courier new,monospace">##-----------------------------------------------------------------------------<br>## Gama.<br><br>n <- 100;<br>shape <- 5; scale <- 3<br>curve(dgamma(x, shape, scale), 0, 10)<br>
y <- rgamma(n, shape=shape, scale=scale)<br><br>ll <- function(theta, y){<br>    sum(dgamma(y, shape=theta[1], scale=theta[2], log=TRUE))<br>}<br><br>start <- c(5,3)<br>op <- optim(start, fn=ll, y=y, hessian=TRUE,<br>
            method="BFGS", control=list(fnscale=-1))<br>op$par<br>op$value<br>op$hessian<br><br>##-----------------------------------------------------------------------------<br>## Weibull.<br><br>n <- 100;<br>
shape <- 5; scale <- 3<br>curve(dweibull(x, shape, scale), 0, 10)<br>y <- rweibull(n, shape=shape, scale=scale)<br><br>ll <- function(theta, y){<br>    sum(dweibull(y, shape=theta[1], scale=theta[2], log=TRUE))<br>
}<br><br>start <- c(5,3)<br>op <- optim(start, fn=ll, y=y, hessian=TRUE,<br>            method="BFGS", control=list(fnscale=-1))<br>op$par<br>op$value<br>op$hessian<br><br>##-----------------------------------------------------------------------------<br>
## Usando a nlm (só minimiza).<br><br>ll <- function(theta, y){<br>    -sum(dweibull(y, shape=theta[1], scale=theta[2], log=TRUE))<br>}<br><br>start <- c(5,3)<br>op <- nlm(p=start, f=ll, y=y, hessian=TRUE)<br>op<br>
<br>##-----------------------------------------------------------------------------<br>## Usando a survreg (para a gama seria a glm(..., family=gamma)).<br><br>require(survival)<br><br>m0 <- survreg(Surv(time=y)~1, dist="weibull")<br>
summary(m0)<br><br>exp(coef(m0)) ## = scale.<br>1/m0$scale    ## = shape.<br><br>##-----------------------------------------------------------------------------<br>## Usando a bblme.<br><br>require(bbmle)<br><br>## Forma de especificar I.<br>
ml0 <- mle2(y~dweibull(shape=sh, scale=sc),<br>             start=list(sh=5, sc=3), data=list(y=y))<br>class(ml0)<br><br>## Vantagem é a disponibilidade de métodos.<br>summary(ml0)<br>vcov(ml0)<br>plot(profile(ml0))<br>
confint(ml0)<br>confint(ml0, method="quad")<br><br>## Forma de específicar II.<br>ll <- function(shape, scale, y){<br>    -sum(dweibull(y, shape=shape, scale=scale, log=TRUE))<br>}<br><br>start <- list("shape"=5, "scale"=3)<br>
ml1 <- mle2(minuslogl=ll, start=start, method="BFGS", data=list(y=y))<br><br>summary(ml1)<br>vcov(ml1)<br>plot(profile(ml1))<br>confint(ml1)<br>confint(ml1, method="quad")<br><br>##-----------------------------------------------------------------------------</span><br>
<br><br>À<div class="gmail_default" style="font-family:trebuchet ms,sans-serif;display:inline">​ disposição<br>Walmes.​</div><br><br></div>