[R] Function within functions and MLE

dpender d.pender.1 at research.gla.ac.uk
Mon Feb 21 14:32:11 CET 2011


Hi,

I am trying to determine the MLE of the following function:

http://r.789695.n4.nabble.com/file/n3317341/untitled.bmp 

I have defined both parts of the equation as separate functions and looped
over the t and G values to get summations of each part.  

The lamda function has 3 unknowns which I am trying to determine using MLE
bub tin order to try and get the overall function working these have been
assigned values at the moment.

So what I am trying to do is write a function l(t|0) that is the sum of two
functions X and Y which are represented by the summations in the above
equation.

My R code is as follows


te <-
c(11,161,337,684,1075,1639,2021,2325,2715,3052,3711,3928,4417,4808,5527,6489,6832,7641,8074)
G <-
c(106,164,314,350,523,317,281,378,327,578,201,427,338,682,952,337,803,424,0)

Poisson.lik <- function(theta0, theta1, theta2, w, t1, t2) {

X <- array(0, dim=c(1,19))

for (i in 1:19) {

X[i] <- function(theta0, theta1, theta2, w, t1, t2)
(((theta0*w*t2)-(theta1*cos(w*t2))+(theta2*sin(w*t2)))/w) -
(((theta0*w*t1)-(theta1*cos(w*t1))+(theta2*sin(w*t1)))/w)


}

XX <- -sum(X)

Y <- array(0, dim=c(1,19))

for (i in 1:19) {

Y[i] <- function(theta0, theta1, theta2, w, t2) theta0 + theta1*sin(w*t2) +
theta2*cos(w*t2)

}

YY <- sum(log(Y))

LL <- XX+YY

return (-LL)

}


optim(c(0,100), Poisson.lik, w=6.28, t1=te[i], t2=te[i]+G[i])

The code isn't working but I am also not sure if this is the best way to go
about it as I am not sure about how to define functions within functions. 

What I want out is the MLE for theta, theta1 and theta1.

Can anyone help? Apologies if the code is confusing!

Thanks,

Doug

-- 
View this message in context: http://r.789695.n4.nabble.com/Function-within-functions-and-MLE-tp3317341p3317341.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list