[R] Help MLE

2008-10-09 Thread LFRC

Dear, 

I'm starting on R language. I would like some help to implement a MLE
function.

I wish to obtain the variables values (alpha12, w_g12, w_u12) that maximize
the function LL = Y*ln(alpha12 + g*w_g12 + u*w_u12).

Following the code:

 rm(list=ls())   
 ls()
 library(stats4)

 Model = function(alpha12,w_g12,w_u12)
 {
 Y = 1
 u = 0.5
 g = -1
 Y*log(alpha12 + g*w_g12 + u*w_u12)
 }

 res = mle(minuslog=Model,start=list(alpha12=0.1,w_u12=0.1,w_g12=0.1),
method = "BFGS")
 
 res

Error message:

 > res = mle(minuslog=Model,start=list(alpha12=0.1, w_u12=0.1, w_g12=0.1),
method = "BFGS")
 Error in optim(start, f, method = method, hessian = TRUE, ...) : 
  non-finite finite-difference value [1]

Thanks,
LF
-- 
View this message in context: 
http://www.nabble.com/Help-MLE-tp19904724p19904724.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.


Re: [R] Help MLE

2008-10-10 Thread LFRC

Dear Ben Bolker,

Thanks a lot for your help.

I have two more questions:

1) My goal is maximize the function (> r = Y*log(comb)) but the
parameters found, minimized the function (r = Y*log(comb)).

2) What this function do?
  > model2 <- function(p) {
  >   do.call("Model",as.list(p))
  > }

Best regards,
LFRC



Ben Bolker wrote:
> 
> LFRC  yahoo.com.br> writes:
> 
>> 
>> 
>> Dear, 
>> 
>> I'm starting on R language. I would like some help to implement a MLE
>> function.
>> 
>> I wish to obtain the variables values (alpha12, w_g12, w_u12) that
>> maximize
>> the function LL = Y*ln(alpha12 + g*w_g12 + u*w_u12).
>> 
> 
> 
>   You're running into a problem because your linear combination
> goes negative in the course of the optimization, which makes
> the logarithm NaN, which crashes the optimizer.  The quick
> hack would be to calculate the linear combination and then
> set it to max(value,some_small_positive_number) -- this will
> (probably) work if the solution is not on the boundary.
> Alternatively you can use constrOptim (see below),
> because you need to impose a linear inequality constraint.
> 
>  rm(list=ls())   
>  ls()
>  library(stats4)
> 
> Model = function(alpha12,w_g12,w_u12)
>   {
> Y = 1
> u = 0.5
> g = -1
> comb = alpha12 + g*w_g12 + u*w_u12
> r = Y*log(comb)
> cat(alpha12,w_g12,w_u12,comb,log(comb),r,"\n") ## debug
> r
>   }
> 
> res = mle(minuslog=Model,start=list(alpha12=0.1,w_u12=0.1,w_g12=0.1),
>   method = "BFGS")
> 
> model2 <- function(p) {
>   do.call("Model",as.list(p))
> }
> 
> constrOptim(theta=c(0.1,0.1,0.1),f=model2,
> grad=NULL,
> ui = c(1,-1,0.5),
> ci = rep(0,3))
> 
> 
>   Ben Bolker
> 
> __
> 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.
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Help-MLE-tp19904724p19917390.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.


Re: [R] Help MLE

2008-10-10 Thread LFRC

Thanks again.

Now I want to complicate the model. Supose that Y is a vector with 30 data.

Y = c(0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0,
1, 0, 0, 0, 0, 1, 0)
Then the r = sum(Y*log(comb))

Is it possible? I want to discover the variables values
(alpha12,w_g12,w_u12) for each Y that optimize the function r.

The new code is:

Model = function(alpha12,w_g12,w_u12) 
  { 
Y = c(0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
0, 1, 0, 0, 0, 0, 1, 0) 
u = 0.5 
g = -1 
comb = alpha12 + g*w_g12 + u*w_u12 
r = sum(Y*log(comb))
cat(alpha12,w_g12,w_u12,comb,log(comb),r,"\n") ## debug 
r 
  } 

res = mle(minuslog=Model,start=list(alpha12=0.1,w_u12=0.1,w_g12=0.1), 
  method = "BFGS") 

model2 <- function(p) { 
  do.call("Model",as.list(p)) 
} 

constrOptim(theta=c(0.1,0.1,0.1),f=model2, 
grad=NULL, 
ui = c(1,-1,0.5), 
ci = rep(0,3)) 

ERROR:

Error in optim(start, f, method = method, hessian = TRUE, ...) : 
  objective function in optim evaluates to length 30 not 1

Best regards,
LFRC


Ben Bolker wrote:
> 
> LFRC  yahoo.com.br> writes:
> 
>> 
>> 
>> Dear Ben Bolker,
>> 
>> Thanks a lot for your help.
>> 
>> I have two more questions:
>> 
>> 1) My goal is maximize the function (> r = Y*log(comb)) but the
>> parameters found, minimized the function (r = Y*log(comb)).
>> 
> 
>   Oh.  Oops.  Just change the sign ( r = -Y*log(comb)) or
> set fnscale=-1 (see ?optim) 
> 
>> 2) What this function do?
>>   > model2 <- function(p) {
>>   >   do.call("Model",as.list(p))
>>   > }
>> 
>  
>   I got a little carried away, this is R Magic.
> mle() expects the function to be written out with
> a list of parameters ( func(param1,param2,param3) ),
> while optim and constrOptim want the function to
> have a single vector argument ( func(paramvec) ).
> model2() is a wrapper that converts a vector p
> into a list and passes it to the Model function.
> 
>   Ben Bolker
> 
> __
> 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.
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Help-MLE-tp19904724p19927405.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.


[R] MLE Constraints

2008-10-15 Thread LFRC

Dears,

I'm trying to find the parameters (a,b, ... l) that optimize the function
(Model) 
described below.

1) How can I set some constraints with MLE2 function? I want to set p1>0,
p2>0, 
p3>0, p1>p3. 

2) The code is giving the following warning. 
Warning: optimization did not converge (code 1)
How can I solve this problem?

Can someone help me?

M <- 14
Y = c(0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1)
x1 = c(0.50, 0.50, 0.50, 0.50, 0.50, 0.50, 0.50, 0.50, 0.50, 0.50, 0.25, 
0.25, 0.25, 0.25)
x2 = c(-1, -1, -1, -1, -1,  1,  1,  1,  1,  1,  1,  1,  1,  1)
x3 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
states = c(1, 1, 2, 3, 1, 2, 3, 1, 1, 2, 2, 3, 1, 1)
prob_fn = rep(0,M)

Model=function(a, b, c, d, e, f, g, h, i, j, k, l)
{
p1 = exp(-(a   g*x1   d*x2   j*x3))
p2 = exp(-(b   h*x1   e*x2   k*x3))
p3 = exp(-(c   i*x1   f*x2   l*x3))

### Set P
t5 = 0
while(t5http://www.nabble.com/MLE-Constraints-tp19994553p19994553.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.


Re: [R] MLE Constraints

2008-10-16 Thread LFRC

Dears,

Any help?

Thanks,
LFRC



LFRC wrote:
> 
> Dears,
> 
> I'm trying to find the parameters (a,b, ... l) that optimize the function
> (Model) 
> described below.
> 
> 1) How can I set some constraints with MLE2 function? I want to set p1>0,
> p2>0, 
> p3>0, p1>p3. 
> 
> 2) The code is giving the following warning. 
> Warning: optimization did not converge (code 1)
> How can I solve this problem?
> 
> Can someone help me?
> 
> M <- 14
> Y = c(0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1)
> x1 = c(0.50, 0.50, 0.50, 0.50, 0.50, 0.50, 0.50, 0.50, 0.50, 0.50, 0.25, 
> 0.25, 0.25, 0.25)
> x2 = c(-1, -1, -1, -1, -1,  1,  1,  1,  1,  1,  1,  1,  1,  1)
> x3 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
> states = c(1, 1, 2, 3, 1, 2, 3, 1, 1, 2, 2, 3, 1, 1)
> prob_fn = rep(0,M)
> 
> Model=function(a, b, c, d, e, f, g, h, i, j, k, l)
> {
> p1 = exp(-(a   g*x1   d*x2   j*x3))
> p2 = exp(-(b   h*x1   e*x2   k*x3))
> p3 = exp(-(c   i*x1   f*x2   l*x3))
> 
> ### Set P
> t5 = 0
> while(t5 {
> t5 = t5 1
> 
> if(states[t5]==1)  {prob_ok = p1[1]}
> if(states[t5]==2)  {prob_ok = p2[1]}
> if(states[t5]==3)  {prob_ok = p3[1]}
> prob_fn[t5] = c(prob_ok)
> }
> 
> prob_fn[prob_fn==0] = 0.1
> 
> ### LL
> ll_calc = -(sum(Y*log(prob_fn)))
> return(ll_calc)
> }
> 
> res = mle2(Model, start=list(a=1, b=1, c=1, d=0.15, e=0.15,
> f=0.15, g=0.9, h=0.9, i=0.9, j=0.1, k=0.1, l=0.1), method = "Nelder-
> Mead")
> res
> 
> Best regards,
> LFRC
> 
> 

-- 
View this message in context: 
http://www.nabble.com/MLE-Constraints-tp19994553p20016631.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.