# [R] Maximum Likelihood Estimation and Optimisation

Spencer Graves spencer.graves at pdf.com
Thu Jul 10 17:01:52 CEST 2003

```It is not obvious to me what parameters in what model you want to fit.
Function "optim" does very well with many different kinds of problems.
If you just want to estimate parameters of a probability distribution,
function "fitdistr" in library(MASS) will do that.  A couple of days
ago, I needed to fit a "Pareto distribution of the first kind."  A
search of "www.r-project.org" -> search -> "R site search" uncovered
functions for a Pareto distribtion of a different kind.  So, I wrote the
following and used them to check "fitdistr" and then to actually fit the
distribution to data.

hope this helps.  spencer graves
#####################################################
dpareto <-
function(x, shape, x0, log=FALSE){
dp <- if(log) (log(shape-1)-shape*log(x/x0)-log(x0))
else ((shape-1)*((x0/x)^shape)/x0)
dp[x<x0] <- 0
dp
}

ppareto <-
function(q, shape, x0, lower.tail = TRUE, log.p=FALSE){
q <- pmax(x0, q)
if(log.p){
if(lower.tail){
return(log(ppareto(q, shape, x0)))
}
else return((shape-1)*log(x0/q))
}
else{
S.q <- (x0/q)^(shape-1)
if(lower.tail)return(1-S.q)
else return(S.q)
}
}

qpareto <-
function(p, shape, x0, lower.tail=TRUE){
if(lower.tail) p <- (1-p)
x0*exp(-log(p)/(shape-1))
}

rpareto <-
function(n, shape, x0)
qpareto(runif(n), shape, x0, lower.tail=FALSE)

fitdistr(rpareto(10000, 3, 1), dpareto, list(shape=2.5), x0=1)

########################################################
Harold Doran wrote:
> Well, lm() produces an OLS solution, which are also MLE
solutions for the fixed effects. I think this is an easy
way, although maybe not the best.
>
> BHHH is a numerical approximation that can be used when
a closed form solution is not available. It is less
sophisticated than Newton-Raphson.
>
>
>
> ------
> Harold C. Doran
> Director of Research and Evaluation
> New American Schools
> 675 N. Washington Street, Suite 220
> Alexandria, Virginia 22314
> 703.647.1628
>
> -----Original Message-----
> From: Fohr, Marc [AM] [mailto:marc.fohr at first-private.de]
> Sent: Thursday, July 10, 2003 10:17 AM
> To: R-help at stat.math.ethz.ch
> Subject: [R] Maximum Likelihood Estimation and Optimisation
>
>
> Hello,
>
> I want to calculate a maximum likelihood funktion in R in
order to solve for the parameters of an estimator. Is there
an easy way to do this in R? How do I get the parameters and
the value of the maximum likelihood funktion.
>
> More, I want to specify the algorithm of the optimisation
above: BHHH (Berndt Hall Hall Hausman). Is this possible?
>
> Thanks a lot for your help and best regards
>
> Marc
>
> -----------------------------------------------------------------------------
> Marc Fohr, CFA
> Equity Portfolio Manager
> First Private Investment Management
> Neue Mainzer Strasse 75
> D-60311 Frankfurt/Main
> Phone: ++49 - 69 - 2607 5424
> Fax: ++49 - 69 - 2607 5440
> Email: marc.fohr at first-private.de
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help

```