tmuman <mumantariq <at> gmail.com> writes:
> Hi, am very new to R and I've written an optim function, but can't
> get it to work
> least.squares.fitter<-function(start.params,gr,
> low.constraints,high.constraints,model.one.stepper,data,scale,ploton=F)
> {
> result<-optim(par=start.params,
method=c('Nelder-Mead'),
fn=least.squares.fit,
lower=low.constraints,
upper=high.constraints,data=data,scale=scale,ploton=ploton)
return(result)
}
>
least.squares.fitter(c(2,2),c(0,0),c(Inf,Inf),ricker.one.step,dat,'linear')
>
> where least.squares.fit is the function returning the difference between
> model data points and predicted points (residuals, residuals^2 and SSR),
> which i am trying to minimise
>
> and ricker.one.step is the function that generated the predictions
>
> I've been trying to figure out whats wrong for hours but can't.
>
You haven't given us much to work with here -- have you
read the posting guide (URL at the bottom of every message)?
It's not reproducible ( http://tinyurl.com/reproducible-000 ),
and you haven't told us what's going wrong. Are you getting
error messages? What are they? Warnings? No errors but
you have reason to believe the answer is wrong?
A few comments:
* 'scale' and 'ploton' are not arguments to optim(),
so they'd better be accepted by your least.squares.fit() function
* the Nelder-Mead algorithm doesn't accept constraints (lower/upper)
(see the nloptr package for a Nelder-Mead implementation that does)
* except for the constraints, you might be better off with ?nls
if all you want to do is least-squares fitting; nlsLM in the
minpack.lm package does least-squares fitting with constraints