problem with MLE estimation using Kalman filter

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

problem with MLE estimation using Kalman filter

curtburk
Hello,

I've recently run into a problem when trying to do some MLE parameter
estimation in R using a non-linear Kalman filter.  I've written some of the
functions myself and everything seems to be working fine until the end when
I try to use the mle2 function written for R.  The error message states
that the argument "minuslog1" is missing with no default, but I've
specified this argument using my written function. Any help with this would
much appreciated.

##script that codes for a non-linear Kalman filter

#simulate starting conditions and data
N.obs=seq(from=5,to=200,by=5)
y.procobs2=Nobs

#write the function called "nlkfpred" to predict the mean and variance of
#each observed value; Nobs is the data set

nlkfpred=function(r,K,procvar,obsvar,M.n.start,Var.n.start,Nobs){
nt = length(Nobs)
M.nobs = numeric(nt)
Var.nobs = numeric(nt)
M.n = M.n.start
Var.n = Var.n.start
M.nobs[1] = M.n.start
Var.nobs[1] = Var.n.start+obsvar
for (t in 2:nt) {
M.ni = M.n+r*M.n(1-M.n/K)
b = 1+r-2*r*M.n/K
Var.ni = b^2*Var.n+procvar
M.nobs[t] = M.ni
Var.nobs[t] = Var.ni+obsvar
M.n = M.ni+(Var.ni/Var.nobs[t])*(Nobs[t]-M.nobs[t])
Var.n = Var.ni*(1-(Var.ni/Var.nobs[t]))
}
list(mean = M.nobs,var = Var.nobs)
}

##write the function called "nlkflik" to make a normal likelihood comparison
##using log values of the parameters so that we don't have to worry about
##negative parameter values; "obs.data" is an observed data set

nlkflik =
function(logr,logK,logprocvar,logobsvar,logM.n.start,logVar.n.start,
obs.data){
pred = nlkfpred(r = exp(logr),K = exp(logK),procvar = exp(logprocvar),
obsvar = exp(logobsvar),M.n.start = exp(logM.n.start),Var.n.start =
exp(logVar.n.start),
N.obs=y.procobs2)
-sum(dnorm(obs.data,mean = pred$mean,sd = sqrt(pred$var),log = TRUE))
}

#designate starting parameter values

startvec=list(logr=log(0.25),logK=log(10),logprocvar=log(0.5),
logobsvar=log(0.5),logM.n.start=log(3),logVar.n.start=-2)

#load two required packages
require(bbmle)
require(optimx)

#obtain MLE estimates of paramter values
m4 = mle2(minuslog1 = nlkflik,start = startvec,data = list(obs.data=Nobs),
method = "Nelder-Mead", control = list(maxit=2000))

Thanks

--
Curtis

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: problem with MLE estimation using Kalman filter

Ingmar Visser
On Wed, Oct 16, 2013 at 8:30 PM, Curtis Burkhalter
<[hidden email]> wrote:
> I try to use the mle2 function written for R.  The error message states
> that the argument "minuslog1" is missing with no default, but I've


The argument is minuslogl

Note: l instead of 1

hth, Ingmar

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: problem with MLE estimation using Kalman filter

curtburk
Thanks, that always gets me for some reason.  Now when I run it though I
get an error message at the very end that states "could not find function
"M.n". I don't understand why I'm getting this message b/c there is no
where that calls a function named "M.n" and I don't define a function with
that name either.

Thanks



On Wed, Oct 16, 2013 at 2:38 PM, Ingmar Visser <[hidden email]> wrote:

> On Wed, Oct 16, 2013 at 8:30 PM, Curtis Burkhalter
> <[hidden email]> wrote:
> > I try to use the mle2 function written for R.  The error message states
> > that the argument "minuslog1" is missing with no default, but I've
>
>
> The argument is minuslogl
>
> Note: l instead of 1
>
> hth, Ingmar
>



--
Curtis Burkhalter

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: problem with MLE estimation using Kalman filter

Duncan Murdoch-2
On 16/10/2013 2:57 PM, Curtis Burkhalter wrote:
> Thanks, that always gets me for some reason.  Now when I run it though I
> get an error message at the very end that states "could not find function
> "M.n". I don't understand why I'm getting this message b/c there is no
> where that calls a function named "M.n" and I don't define a function with
> that name either.

You have this in your code:

M.n(1-M.n/K)

I imagine you want a * in there:

M.n*(1 - M.n/K)

Duncan Murdoch

>
> Thanks
>
>
>
> On Wed, Oct 16, 2013 at 2:38 PM, Ingmar Visser <[hidden email]> wrote:
>
> > On Wed, Oct 16, 2013 at 8:30 PM, Curtis Burkhalter
> > <[hidden email]> wrote:
> > > I try to use the mle2 function written for R.  The error message states
> > > that the argument "minuslog1" is missing with no default, but I've
> >
> >
> > The argument is minuslogl
> >
> > Note: l instead of 1
> >
> > hth, Ingmar
> >
>
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: problem with MLE estimation using Kalman filter

curtburk
Thanks Duncan and Ingmar, everything is all good now.

Best


On Wed, Oct 16, 2013 at 3:12 PM, Duncan Murdoch <[hidden email]>wrote:

> On 16/10/2013 2:57 PM, Curtis Burkhalter wrote:
>
>> Thanks, that always gets me for some reason.  Now when I run it though I
>> get an error message at the very end that states "could not find function
>> "M.n". I don't understand why I'm getting this message b/c there is no
>> where that calls a function named "M.n" and I don't define a function with
>> that name either.
>>
>
> You have this in your code:
>
> M.n(1-M.n/K)
>
> I imagine you want a * in there:
>
> M.n*(1 - M.n/K)
>
> Duncan Murdoch
>
>
>
>> Thanks
>>
>>
>>
>> On Wed, Oct 16, 2013 at 2:38 PM, Ingmar Visser <[hidden email]> wrote:
>>
>> > On Wed, Oct 16, 2013 at 8:30 PM, Curtis Burkhalter
>> > <[hidden email]> wrote:
>> > > I try to use the mle2 function written for R.  The error message
>> states
>> > > that the argument "minuslog1" is missing with no default, but I've
>> >
>> >
>> > The argument is minuslogl
>> >
>> > Note: l instead of 1
>> >
>> > hth, Ingmar
>> >
>>
>>
>>
>>
>


--
Curtis Burkhalter

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.