2D optimize() in R - r

I would like to use optimize() to find parameters of 2 dimensional mixed normal distribution but I don't know how to use the function.
I have the density function:
mixdnorm2<-function(x,y,p,mu11,mu12,s11,s12,rho1,mu21,mu22,s21,s22,rho2){
dnorm2<-function(x,y,m1,m2,s1,s2,r){
U<-c(x-m1,y-m2)
S<-matrix(c(s1^2,s1*s2*r,s1*s2*r,s2^2),2,2,byrow = T)
f<-1/(2*pi*sqrt(det(S)))*exp(-0.5%*%t(U)%*%solve(S)%*%U)
return(f)
}
f<-p*dnorm2(x,y,m11,m12,s11,s12,rho1)+(1-p)*dnorm2(x,y,m21,m22,s21,s22,rho2)
return(f)
}
but I don't know what to do with it.
optimize(mixdnorm2...)
Please do you know, how to use the function? I couldn't find anything about the problem so I'll glad for any advice :)

The optimize function is only for 1 dimension. The optim function is the one to use for 2 or more dimensions. So look up the help page for optim.
For some reason the help page for optimize does not mention optim, though the help page for optim does mention optimize.
There are also some packages that provide additional optimization functions (search on CRAN).

Related

Looking for algebraic primitive function solver code for math

I want to solve primitive functions algebraic for math class. I have been looking around and have found a lot of websites that have this function, but not any open source code!
The basic functionality is to have a function, and the be able to calculate it's primitive function and get the result back as an algebraic expression.
eg.
input: f(x) = cos(x)
output: F(x) = sin(x)
Are there code out there to solve this problem? I just need a template for my own code. And so the language isn't really of any concern.
Thanks for any help!
I have looked around on github and googled but haven't found anything!

How can I define a function in the form of f(a;b) in R?

This has been a recurring problem for me, so I'll have to address it here.
I'm trying to build a non-linear optimization problem using optim or fsolve, and I need certain variables as fixed parameters.
So by f(a;b), I mean a function that defines b as given, and immovable for the optimization.
Evidently matlab has a way to define b as just a parameter set & only allow a to be moved in the optimization; in R this doesn't seem to apply in the same way as matlab. Both optim & fsolve require that I specify the initial values of both a & b, and they calculate the results shifting b as well as a.
If anyone could let me know of a good way out of this that would be much appreciated.
Thank you!
You can pass b via the ... argument of optim.

Why can optim() be used recursively in R

I am hoping someone can clear something up for me please. I am using optim to optimise a set of parameters to fit a model to a timeseries. The code works fine and optimises fine.
However, I recently read on the optim help page that you can use optim() recursively. So I implemented this and it improves the fit over multiple recursions.
My question is this: If optim is already optimising over the parameter space and spitting out an optimal answer for that starting condition, why is the fit improved by using it recursively, repeatedly? This seems unintuitive.
Many thanks!

Find a primitive function for the antiderivative?

I've been searching on R help for a primitive function, because in my case I can't do it with integrate function, is there any way to find a primitive function (antiderivative)?
If it is a one-off, you can use a computer-algebra system (Maxima, Maple, Wolfram Alpha, etc.).
If you want to do it from R, you can use the Ryacas package.
For instance, yacas(expression(integrate(sin))) returns -Cos(x).
There is no analytical method to generate F where F'=f and f is known but you can always approximate a value for a specific bounds when the bounds is known using the trapezoid approximation for instance.

Minimisation using the nlminb R-function

I would like to find the 4-dimensional vector that minimises some function f which depends on 4 variables. The first three variables take on strictly positive values; the fourth one is unconstrained.
To do this, I would like to use R. I have tried to apply the nlminb function with lower=c(0.001, 0.001, 0.001, -Inf) as one of its optional argument. The procedure does converge but it turns out that the proposed solution does not satisfy the constraint !
I have an alternative solution that consists of using an exponential transformation. However, I would appreciate to figure out why R returns a solution that does not meet my requirements.
Any comment will be appreciated,
Thanks,
Marco
It would be very difficult for me to provide that function here. The reason is that it depends on a number of pre-defined stuff.
Anyway, I am not sure to understand why this occurs but I have realized that my function sometimes returns NaN due to very very large numbers. Actually, I have some doubt about convergence.
On the other hand, I have made some modifications and the alternative solution seems to work well.
As a conclusion, I think that the problem came from my function, not from nlminb.
Best,
Marco

Resources