I am working with big and complex function. I am using optim to estimate the model parameters. I see from the iteration values of optim, it does not converge even if the current and last values are very close.
For example,
iteration 10 400.0091
iteration 20 400.0092
iteration 30 400.0093
:
:
keep going, say for iteration 1200.
So how can I change the convergence round of the optim that is, if the current iteration is very close to previous iteration then converge.
You are looking for abstol or reltol which are components control argument.
See ?optim for more details. I can't recommend one without an example/context for your question but your call will look something like:
optim(par, fn, [other vars?], control = list(reltol = 1e-5))
Related
I want to program the maximum likelihood of a gamma distribution in R; until now I have done the following:
library(stats4)
x<-scan("http://www.cmc.edu/pages/faculty/MONeill/Math152/Handouts/gamma-arrivals.txt")
loglike2<-function(LL){
alpha<-LL$a
beta<-LL$b
(alpha-1)*sum(log(x))-n*alpha*log(beta)-n*lgamma(alpha)}
mle(loglike2,start=list(a=0.5,b=0.5))
but when I want to run it, the following message appear:
Error in mle(loglike2, start = list(a = 0.5, b = 0.5)) :
some named arguments in 'start' are not arguments to the supplied log-likelihood
What am I doing wrong?
From the error message it sounds like mle needs to be able to see the variable names listed in start= in the function call itself.
loglike2<-function(a, b){
alpha<-a
beta<-b
(alpha-1)*sum(log(x))-n*alpha*log(beta)-n*lgamma(alpha)
}
mle(loglike2,start=list(a=0.5,b=0.5))
If that doesn't work you should post a reproducible example with all variables defined and also explicitly indicate which package the mle function is coming from.
The error message is unfortunately criptic because it indicates mising
values owing to the fact that alpha and gamma have to be positive and mle optimizes over the real numbers. Hence, you need to transfomt the vector over which the function is being optimized, like so:
library(stats4)
x<-scan("http://www.cmc.edu/pages/faculty/MONeill/Math152/Handouts/gamma-arrivals.txt")
loglike<-function(alpha,beta){
(alpha-1)*sum(log(x))-n*alpha*log(beta)-n*lgamma(alpha)
}
fit <- mle(function(alpha,beta)
# transfrom the parameters so they are positive
loglike(exp(alpha),exp(beta)),
start=list(alpha=log(.5),beta=log(.5)))
# of course you would have to exponentiate the estimates too.
exp(coef(fit1))
note that the error now is that you are using n in loglike()
which you have not defined. If you define n, then you get an error stating
Lapack routine dgesv: system is exactly singular: U[1,1] = 0. which is
caused either by a not very good guess for the start value of alpha and
beta or (more likely) that loglike() does not have a minima (I think your
deleted post from last night had a slightly different formula which I was
able to get working, but not able to respond to b/c the post was deleted...)
FYI, if you want to inspect the alpha and beta parameters that cause the
errors, you can use scoping assignment to post the most recently called
parameters to the environment in which loglike() is defined as in:
loglike<-function(alpha,beta){
g <<- c(alpha,beta)
(alpha-1)*sum(log(x))-n*alpha*log(beta)-n*lgamma(alpha)
}
I am writing a numerical model in R, for an ecological system, and solving it using "lsoda" from package deSolve.
My model has 14 state variables.
I define the model, set it up fine, and give time duration according to this:
nyears<-60
ndays<-nyears*365+1
times<-seq(0,nyears*365,by=1)
Rates of change of state variables (e.g. the rate of change of variable "A1" is "dA1")are calculated according to existing values for state variables (at time=t) and a set of parameters.
Simplified example:
dA1<-Tf*A1*(ImaxA*p_sub)
Where Tf, ImaxA and p_sub are parameters, and A1 is my state variable at time=t.
When I solve the model, I use the lsoda solver like this:
out<-as.data.frame(lsoda(start,times,model,parms))
Sometimes (depending on my parameter combinations), the model run completes over the entire duration I have specified, however sometimes it stops short of the mark (still giving me output up until the solver "crashes"). When it "crashes", this message is displayed:
DLSODA- At current T (=R1), MXSTEP (=I1) steps
taken on this call before reaching TOUT
In above message, I1 = 5000
In above message, R1 = 11535.5
Warning messages:
1: In lsoda(start, times, model, parms) :
an excessive amount of work (> maxsteps ) was done, but integration was not successful - increase maxsteps
2: In lsoda(start, times, model, parms) :
Returning early. Results are accurate, as far as they go
It commonly appears when one of the state variables is getting exponentially bigger, or is tending very near to zero, however sometimes it crashes when seemingly not much change is happening. I may be wrong, but is it due to the rate of change of state-variables becoming too large? If so, why might it also "crash" when there is not a fast rate of change?
Is there a way that I can make the solver complete its task with the specified parameter values, maybe with a more relaxed tolerance for error?
Thank you all for your contributions. I looked at some of the rates, and at the point of crashing, the model was switching between two metabolic states - and the fast rate of this binary switch caused the solver to stop - rejecting the solution because the rate of change was too large. I have fixed my model by introducing a gradual switch between states (with a logistic curve) instead of this binary switch. I aknowledge that I didn;t give enough info in the original question, so thanks for the help you offered!
I am running clustering using the mclust function. In need to get the number of iterations the algorithm used to get the answer. I can't seem to find it anywhere. I do not mind using other function that will perform "gaussian mixture mode" using EM if it will provide me the number of iteration as part of it's output.
There seems to be no clear way to extract this, as far as I can tell. Here's a pretty hacky and approximate way to get at it though.
You can set the maximum number of iterations for EM using the control parameter,
x<-c(rnorm(100),rnorm(100,10,1))
mod<-Mclust(x,control = emControl(itmax=100))
These are set to Inf by default and so the EM will terminate only when the log likelihood changes in increments smaller than the tolerance. If you set itmax, the EM will terminate, but will throw a warning that the algorithm stopped before reaching the tolerance bounds.
So you could adjust itmax a few times to get a sense of how many iterations are required before the EM is naturally terminating. For example,
mod<-Mclust(x,control = emControl(itmax=102))
throws a warning but
mod<-Mclust(x,control = emControl(itmax=103))
does not.
So it seems that 103 iterations are required to reach the exit conditions (with the default tolerance parameters).
I would like to solve two ODE first order on microcontroller. It has to be evaluated every 100ms
x'=-k_{1}\cdot (x-x_{ref})\cdot e^{-b\cdot ((x-x_{obs})^{2}+(y-y_{obs})^{2})}
y'=-k_{1}\cdot (y-y_{ref})\cdot e^{-b\cdot ((x-x_{obs})^{2}+(y-y_{obs})^{2})}
Basically i thought of using euler integration (Runge-Kute I)
y(k+1)=y(k)+f(k,y(k))*dT
I expect error to be < 0.001. How do i determine how many iterations i should run until i hit that error rate ?
I guess that x and y, as well as x_{ref}, y_{ref}, x_{obs}, y_{obs} are time dependent. This limits the number of ODE solver you can use. So it can be only the Euler method and a Runge-Kutta method of 2 order (I forgot the name), which evaluate the r.h.s of you ODE only at the time points x(t), x(t+dT)ยด,x(t+2dT)`,...
You can use classical step size control with these two methods. That is you make one with step with the Euler method and one step with the RK-II method. The difference between these two steps is an indicator for the error and can be used for classical step size control. Have a look at the Numerical Recipes for more details.
I would like to estimate the 4-parameters that minimises the log-likelihood function LogL(\theta). The first two parameters (a and sigma) are positive; the third one is unconstrained and the last one should be leas than the minimum value in the data. So, I tried to use nlminb function and write it as:
nlminb(start=c(a=0.13,mu=1,sigma=31,xi=0.01),f,lower=c(0.0001,0,0.123,0.01210),
upper=c(2,18.21,50,0.95),control=list(eval.max=100, iter.max=100))
I got a good result, but there is still problem that the estimator of mu take the same value of the upper limit for all the values I try to use and for xi, it take the same lower value even If I change the starting values.
The log likelihood function take form:
-loglik=-n*log(a)+n*log(1-exp(-1))+n*log(sigma) -(a-1)*sum(log(x-mu))+(1/xi+1)*sum(log(1+xi*((x-mu)**a)/sigma))+ sum((1+xi*((x-mu)**a)/sigma)*(-1/xi))
I have another condition that x-mu should be positive and 1+xi*((x-mu)*a)/sigma as well
Any suggestion will be appreciated