Re: [R] "Help On optim"
Hi I cannot help you with optim as it is beyond my expertise, but here are some notes. Your mail is partially scrammbled due to HTML formating, please use plain text. You say that you do not get right results, how they differ from your expectation? Preferably provide some toy data as illustration. Cheers Petr > -Original Message- > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of niharika > singhal > Sent: Thursday, August 10, 2017 11:54 AM > To: r-help@r-project.org > Subject: [R] "Help On optim" > > Hello, > > > > I have some parameters from Mclust function. The parameters are in the form > > > > *parametersDf * > > > > * mu_1 mu_2 var_mc1var_mc2c1 > c2* > > *2 1.357283 2.962736 0.4661540.1320129 0.5258975 > 0.4741025 * > > *21 8.357283 9.962736 0.4661540.1320129 0.5258975 > 0.4741025 * > > > > Each row in the above data frame represent the segment and each segment > contain two-mixture component. (Latter I will increase the segment and the > mixture component) > > > > So for every segment with n mixture component I have to find the > > > > *max f(x) with –infinity < x < infinity* > > > > *f(x)=sum_i c_i N(O,mu_i,sigma_i). * > > > > *Since i need to calculate the derivate f’ and set f’(x)=0 I thought of using > Newton method * > > > > *In R the function I selected for my problem is optim.* > > > > *Below is my code * > > > > > > *l = 2 # represent the number of mixture component* > > *i=1# represent the segment 1 if i=2 represent the 2 segment* > > *parametersDf #which I have posted above* > > *xnorm= function(x)* > > *{ * > > > > * #observation= append(segment1, segment2)* > > * mu.vector = parametersDf[1:l] * > > * var.vector =parametersDf[(l+1):((2*l))]* > > * coeff.vector=parametersDf[(2*l+1):(3*l)]* > > * val_df=data.frame()* > > * sum_prob=0* > > * for (j in 1:l){* > > * > val=(coeff.vector[i,j]*(dnorm(x,mu.vector[i,j],sqrt(var.vector[i,j]* > > *sum_prob=sum_prob+val* > > * } * > > * return(sum_prob)* > > *}* > > *#segment1* > > > > *val1=optim(parametersDf$mean_mc_1[1],** fn=xnorm,* > > * method = "BFGS")* > > > > *I am not getting right result from optim function. Is there something I can > do?* > > > *Thanks in Advance for the help * > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům. Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému. Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat. Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu. V případě, že je tento e-mail součástí obchodního jednání: - vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu. - a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou. - trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech. - odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi či osobě jím zastoupené známá. This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. The sender of this e-mail shall not be liable for any possible damage caused by modifica
[R] "Help On optim"
Hello, I have some parameters from Mclust function. The parameters are in the form *parametersDf * * mu_1 mu_2 var_mc1var_mc2c1 c2* *2 1.357283 2.962736 0.4661540.1320129 0.5258975 0.4741025 * *21 8.357283 9.962736 0.4661540.1320129 0.5258975 0.4741025 * Each row in the above data frame represent the segment and each segment contain two-mixture component. (Latter I will increase the segment and the mixture component) So for every segment with n mixture component I have to find the *max f(x) with –infinity < x < infinity* *f(x)=sum_i c_i N(O,mu_i,sigma_i). * *Since i need to calculate the derivate f’ and set f’(x)=0 I thought of using Newton method * *In R the function I selected for my problem is optim.* *Below is my code * *l = 2 # represent the number of mixture component* *i=1# represent the segment 1 if i=2 represent the 2 segment* *parametersDf #which I have posted above* *xnorm= function(x)* *{ * * #observation= append(segment1, segment2)* * mu.vector = parametersDf[1:l] * * var.vector =parametersDf[(l+1):((2*l))]* * coeff.vector=parametersDf[(2*l+1):(3*l)]* * val_df=data.frame()* * sum_prob=0* * for (j in 1:l){* * val=(coeff.vector[i,j]*(dnorm(x,mu.vector[i,j],sqrt(var.vector[i,j]* *sum_prob=sum_prob+val* * } * * return(sum_prob)* *}* *#segment1* *val1=optim(parametersDf$mean_mc_1[1],** fn=xnorm,* * method = "BFGS")* *I am not getting right result from optim function. Is there something I can do?* *Thanks in Advance for the help * [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] Help with optim function in R, please?
I haven't tried your code, but I've seen far too many attempts like this where there is no simple call to the function with starting parameters to see if a sensible answer is returned. If you get NaN or Inf or ... that isn't sensible, then you know it is not a good idea to try further. Beyond this, you don't have gradients. I think I would try nmkb (an updated Nelder-Mead style code that allows for bounds) from dfoptim package. Note that you cannot start on the bounds with this code. And it won't be terribly efficient, but it will -- if you ask for intermediate results -- give you some idea about your function. If everything then working reasonably, you can try L-BFGS-B, possibly with "better" starting parameters. However, I'm fairly certain you will find something to fix before then. It really does pay to be very pedestrian in using optimization software. I've been at this game (and note I wrote 3 of the 5 routines in optim(), which now all have replacements!) and I am constantly reminded in my own work to ALWAYS run some simple tests with the objective function and, if I have them, the gradients. JN On 2017-08-06 11:11 AM, fadeh Alanazi wrote: Hi all, Many thank in advance for helping me. I tried to fit Expectation Maximization algorithm for mixture data. I must used one of numerical method to maximize my function. I built my code but I do not know how to make the optim function run over a different value of the parameters. That is, For E-step I need to get the value of mixture weights based on the current (initial) values of the parameter of the density. Then, multiple the weight by the logliklihood function and maximize it (M-step) Then, I would like to take the new values of the parameter (from M-step) and plug it in the weight, to get a new value of the weight. Then, iterate till converges. I tried the following code, but it does not work. library(copula) library(VineCopula) ## to generate the data set.seed(123) cp <- mixCopula(list(frankCopula(4),claytonCopula(2))) cop <- rCopula(100,cp) x <- pobs(cop) ## this is the data ## my function including optim function myfunc <- function(data,copula=list(frankCopula(4,dim=2), claytonCopula(0.5,dim=2)),maxit=200){ # copula[[1]]@parameters <- par[1] # copula[[2]]@parameters <- par[2] optim_1 <- function(par, data.=data, copula.=copula){ copula[[1]]@parameters <- par[1] copula[[2]]@parameters <- par[2] tau_1 <- par[3]*dCopula(data,copula[[1]],log = F) tau_2 <- par[4]*dCopula(data,copula[[2]],log=F) Tau_1 <- tau_1 / sum(tau_1 + tau_2) Tau_2 <- tau_2 / sum(tau_1 + tau_2) up_pi1 <- sum(Tau_1)/ 100 up_pi2 <- sum(Tau_2) / 100 # Tau <- c(Tau_1, Tau_2) ll <- (-sum(Tau_1*dCopula(data,copula[[1]],log=T))) #ll_1 <- -sum(Tau_2*dCopula(data,copula[[2]],log=T)) if (is.finite(ll)) { return(ll) } else { if (is.na(ll)) { message(par) } message(ll) return(-3^305) } } xy <- optim(par=c(2,0.5,0.2,0.2),fn=optim_1,method="L-BFGS-B",control = list(maxit= 200, trace=1),lower= c(copula[[1]]@param.lowbnd, copula[[2]]@param.lowbnd,0,0), upper = c(copula[[1]]@param.upbnd, copula[[2]]@param.upbnd,1,1)) return(xy$par) } Any help please? Regards, Fadhah [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
[R] Help with optim function in R, please?
Hi all, Many thank in advance for helping me. I tried to fit Expectation Maximization algorithm for mixture data. I must used one of numerical method to maximize my function. I built my code but I do not know how to make the optim function run over a different value of the parameters. That is, For E-step I need to get the value of mixture weights based on the current (initial) values of the parameter of the density. Then, multiple the weight by the logliklihood function and maximize it (M-step) Then, I would like to take the new values of the parameter (from M-step) and plug it in the weight, to get a new value of the weight. Then, iterate till converges. I tried the following code, but it does not work. library(copula) library(VineCopula) ## to generate the data set.seed(123) cp <- mixCopula(list(frankCopula(4),claytonCopula(2))) cop <- rCopula(100,cp) x <- pobs(cop) ## this is the data ## my function including optim function myfunc <- function(data,copula=list(frankCopula(4,dim=2), claytonCopula(0.5,dim=2)),maxit=200){ # copula[[1]]@parameters <- par[1] # copula[[2]]@parameters <- par[2] optim_1 <- function(par, data.=data, copula.=copula){ copula[[1]]@parameters <- par[1] copula[[2]]@parameters <- par[2] tau_1 <- par[3]*dCopula(data,copula[[1]],log = F) tau_2 <- par[4]*dCopula(data,copula[[2]],log=F) Tau_1 <- tau_1 / sum(tau_1 + tau_2) Tau_2 <- tau_2 / sum(tau_1 + tau_2) up_pi1 <- sum(Tau_1)/ 100 up_pi2 <- sum(Tau_2) / 100 # Tau <- c(Tau_1, Tau_2) ll <- (-sum(Tau_1*dCopula(data,copula[[1]],log=T))) #ll_1 <- -sum(Tau_2*dCopula(data,copula[[2]],log=T)) if (is.finite(ll)) { return(ll) } else { if (is.na(ll)) { message(par) } message(ll) return(-3^305) } } xy <- optim(par=c(2,0.5,0.2,0.2),fn=optim_1,method="L-BFGS-B",control = list(maxit= 200, trace=1),lower= c(copula[[1]]@param.lowbnd, copula[[2]]@param.lowbnd,0,0), upper = c(copula[[1]]@param.upbnd, copula[[2]]@param.upbnd,1,1)) return(xy$par) } Any help please? Regards, Fadhah [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] Help with optim() to maximize log-likelihood
1) It helps to include the require statements for those of us who work outside your particular box. lme4 and (as far as I can guess) fastGHQuad are needed. 2) Most nonlinear functions have domains where they cannot be evaluated. I'd be richer than Warren Buffett if I got $5 for each time someone said your optimizer doesn't work and I found f(start, ...) was NaN or Inf, as in this case, i.e., start - c(plogis(sum(Y/m)),log(sigma2H)) cat(starting params:) print(start) tryf0 - ll(start,Y,m) print(tryf0) It really is worthwhile actually computing your function at the initial parameters EVERY time. (Or turn on the trace etc.) JN On 15-03-10 07:00 AM, r-help-requ...@r-project.org wrote: Message: 12 Date: Mon, 9 Mar 2015 16:18:06 +0200 From: Sophia Kyriakou sophia.kyriako...@gmail.com To: r-help@r-project.org Subject: [R] Help with optim() to maximize log-likelihood Message-ID: cao4ga+qokumhozwvbu7ey3xabbo2lnqjrcwxqkzchm3u9oz...@mail.gmail.com Content-Type: text/plain; charset=UTF-8 hello, I am using the optim function to maximize the log likelihood of a generalized linear mixed model and I am trying to replicate glmer's estimated components. If I set both the sample and subject size to q=m=100 I replicate glmer's results for the random intercept model with parameters beta=-1 and sigma^2=1. But if I change beta to 2 glmer works and optim gives me the error message function cannot be evaluated at initial parameters. If anyone could please help? Thanks # likelihood function ll - function(x,Y,m){ beta - x[1] psi - x[2] q - length(Y) p - 20 rule20 - gaussHermiteData(p) wStar - exp(rule20$x * rule20$x + log(rule20$w)) # Integrate over(-Inf, +Inf) using adaptive Gauss-Hermite quadrature g - function(alpha, beta, psi, y, m) {-y+m*exp(alpha + beta)/(1 + exp(alpha + beta)) + alpha/exp(psi)} DDfLik - deriv(expression(-y+m*exp(alpha + beta)/(1 + exp(alpha + beta)) + alpha/exp(psi)), namevec = alpha, func = TRUE,function.arg = c(alpha, beta, psi, y, m)) int0 - rep(NA,q) piYc_ir - matrix(NA,q,p) for (i in 1:q){ muHat - uniroot(g, c(-10, 10),extendInt =yes, beta = beta, psi = psi, y = Y[i], m = m)$root jHat - attr(DDfLik(alpha = muHat, beta, psi, Y[i], m), gradient) sigmaHat - 1/sqrt(jHat) z - muHat + sqrt(2) * sigmaHat * rule20$x piYc_ir[i,] - choose(m,Y[i])*exp(Y[i]*(z+beta))*exp(-z^2/(2*exp(psi)))/((1+exp(z+beta))^m*sqrt(2*pi*exp(psi))) int0[i] - sqrt(2)*sigmaHat*sum(wStar*piYc_ir[i,]) } ll - -sum(log(int0)) ll } beta - 2 sigma2 - 1 m - 100 q - 100 cl - seq.int(q) tot - rep(m,q) set.seed(123) alpha - rnorm(q, 0, sqrt(sigma2)) Y - rbinom(q,m,plogis(alpha+beta)) dat - data.frame(y = Y, tot = tot, cl = cl) f1 - glmer(cbind(y, tot - y) ~ 1 + (1 | cl), data = dat,family = binomial(),nAGQ = 20) betaH - summary(f1)$coefficients[1] sigma2H - as.numeric(summary(f1)$varcor) thetaglmer - c(betaH,sigma2H) logL - function(x) ll(x,Y,m) thetaMLb - optim(c(plogis(sum(Y/m)),log(sigma2H)),fn=logL)$par Error in optim(c(plogis(sum(Y/m)), log(sigma2H)), fn = logL) : function cannot be evaluated at initial parameters thetaglmer [1] 2.1128529 0.8311484 (thetaML - c(thetaMLb[1],exp(thetaMLb[2]))) [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
[R] Help with optim() to maximize log-likelihood
hello, I am using the optim function to maximize the log likelihood of a generalized linear mixed model and I am trying to replicate glmer's estimated components. If I set both the sample and subject size to q=m=100 I replicate glmer's results for the random intercept model with parameters beta=-1 and sigma^2=1. But if I change beta to 2 glmer works and optim gives me the error message function cannot be evaluated at initial parameters. If anyone could please help? Thanks # likelihood function ll - function(x,Y,m){ beta - x[1] psi - x[2] q - length(Y) p - 20 rule20 - gaussHermiteData(p) wStar - exp(rule20$x * rule20$x + log(rule20$w)) # Integrate over(-Inf, +Inf) using adaptive Gauss-Hermite quadrature g - function(alpha, beta, psi, y, m) {-y+m*exp(alpha + beta)/(1 + exp(alpha + beta)) + alpha/exp(psi)} DDfLik - deriv(expression(-y+m*exp(alpha + beta)/(1 + exp(alpha + beta)) + alpha/exp(psi)), namevec = alpha, func = TRUE,function.arg = c(alpha, beta, psi, y, m)) int0 - rep(NA,q) piYc_ir - matrix(NA,q,p) for (i in 1:q){ muHat - uniroot(g, c(-10, 10),extendInt =yes, beta = beta, psi = psi, y = Y[i], m = m)$root jHat - attr(DDfLik(alpha = muHat, beta, psi, Y[i], m), gradient) sigmaHat - 1/sqrt(jHat) z - muHat + sqrt(2) * sigmaHat * rule20$x piYc_ir[i,] - choose(m,Y[i])*exp(Y[i]*(z+beta))*exp(-z^2/(2*exp(psi)))/((1+exp(z+beta))^m*sqrt(2*pi*exp(psi))) int0[i] - sqrt(2)*sigmaHat*sum(wStar*piYc_ir[i,]) } ll - -sum(log(int0)) ll } beta - 2 sigma2 - 1 m - 100 q - 100 cl - seq.int(q) tot - rep(m,q) set.seed(123) alpha - rnorm(q, 0, sqrt(sigma2)) Y - rbinom(q,m,plogis(alpha+beta)) dat - data.frame(y = Y, tot = tot, cl = cl) f1 - glmer(cbind(y, tot - y) ~ 1 + (1 | cl), data = dat,family = binomial(),nAGQ = 20) betaH - summary(f1)$coefficients[1] sigma2H - as.numeric(summary(f1)$varcor) thetaglmer - c(betaH,sigma2H) logL - function(x) ll(x,Y,m) thetaMLb - optim(c(plogis(sum(Y/m)),log(sigma2H)),fn=logL)$par Error in optim(c(plogis(sum(Y/m)), log(sigma2H)), fn = logL) : function cannot be evaluated at initial parameters thetaglmer [1] 2.1128529 0.8311484 (thetaML - c(thetaMLb[1],exp(thetaMLb[2]))) [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] Help with optim() to maximize log-likelihood
Sophia Kyriakou sophia.kyriakou17 at gmail.com writes: hello, I am using the optim function to maximize the log likelihood of a generalized linear mixed model and I am trying to replicate glmer's estimated components. If I set both the sample and subject size to q=m=100 I replicate glmer's results for the random intercept model with parameters beta=-1 and sigma^2=1. But if I change beta to 2 glmer works and optim gives me the error message function cannot be evaluated at initial parameters. If anyone could please help? Thanks snip to make gmane happy. It looks like you're getting floating-point under/overflow. If you do all the computations on the log scale first and then exponentiate, it seems to work, i.e.: piYc_ir[i,] - lchoose(m,Y[i]) + Y[i]*(z+beta) + (-z^2/(2*exp(psi))) - m*(log1p(exp(z+beta))) - 0.5*(log(2*pi)+psi) piYc_ir[i,] - exp(piYc_ir[i,]) follow-ups should probably go to r-sig-mixed-mod...@r-project.org instead ... Ben Bolker __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] Help with optim() to maximize log-likelihood
yes Ben, this works indeed! Thanks a million!! On Mon, Mar 9, 2015 at 7:17 PM, Ben Bolker bbol...@gmail.com wrote: Sophia Kyriakou sophia.kyriakou17 at gmail.com writes: hello, I am using the optim function to maximize the log likelihood of a generalized linear mixed model and I am trying to replicate glmer's estimated components. If I set both the sample and subject size to q=m=100 I replicate glmer's results for the random intercept model with parameters beta=-1 and sigma^2=1. But if I change beta to 2 glmer works and optim gives me the error message function cannot be evaluated at initial parameters. If anyone could please help? Thanks snip to make gmane happy. It looks like you're getting floating-point under/overflow. If you do all the computations on the log scale first and then exponentiate, it seems to work, i.e.: piYc_ir[i,] - lchoose(m,Y[i]) + Y[i]*(z+beta) + (-z^2/(2*exp(psi))) - m*(log1p(exp(z+beta))) - 0.5*(log(2*pi)+psi) piYc_ir[i,] - exp(piYc_ir[i,]) follow-ups should probably go to r-sig-mixed-mod...@r-project.org instead ... Ben Bolker __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] help using optim function
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 __ R-help@r-project.org 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.
[R] help using optim function
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. Many thanks, Tariq -- View this message in context: http://r.789695.n4.nabble.com/help-using-optim-function-tp4647231.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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.