I am trying to compute the following "approximate Maximum Likelihood Estimate" in R. I am a little lost as to how to do this though:

any hints would be appreciated.

**Contents**hide

#### Best Answer

You want to use the function `nlm()`

to calculate the argmax, which requires a function to be optimized as the first argument and the an initial guess at the parameters as the second argument, like so:

`# define g(x;gamma) g <- function(x,gamma) { (1/pi) * ( gamma / (x^2 + gamma^2) ) } # define y here so it can be used inside fun() via lexical scoping y <- blah blah blah # the function to be optimized is optimized over the first argument. fun <- function(x){ # separate the first argument into the variables needed # for opimization phi = x[1:2] psi = x[3:4] gamma = x[5] # PERFORM THE BIG SUM out <- 0 for(t in seq(r + 1,T-s)){ # calculate A A <- (y[t] * (psi[1]*phi[1] + 1) + (psi[1]*phi[2] + phi[1]) * y[t-1] - phi[2]* y[t-2] - psi[1] * y[t+1] ) # increment the sum out <- out + log(g(A,gamma)) } #nlm performs the argmin, so we'll return -out -out } # do the optimization out <- nlm(fun, # initial guess at the parameters, c(0,0,# phi 0,0,# psi 0))# gamma phi_hat <- out$estimate[1:2] psi_hat <- out$estimate[3:4] gamma_hat <- out$estimate[5] `

Note that `nlm()`

does not make restrictions on the arguments so, for example, if you need the arguments to be positive, you need to make a transformation like this:

`phi = exp(x[1:2]) `

in `fun()`

and also here:

`phi_hat <- exp(out$estimate[1:2]) `

### Similar Posts:

- Solved – R times series — correct use of forecast() and accuracy() in forecast package
- Solved – R times series — correct use of forecast() and accuracy() in forecast package
- Solved – How to define initial probabilities for HMM
- Solved – SVM parameter dependence on number of samples
- Solved – maximization of a function with nlminb in R