Hi all, I'm trying to define and log-likelihood function to work with MLE. There will be parameters like mu_i, sigma_i, tau_i, ro_i, for i between 1 to 24. Instead of listing all the parameters, one by one in the function definition, is there a neat way to do it in R ? The example is as follows: ll<- function(mu1=-0.5,b=1.2,tau_1=0.5,sigma_1=0.5,ro_1=0.7) { if (tau1>0 && ro<1 && ro>-1) -sum(dmnorm(cbind(x,y),c(mu1,b*mu1),matrix(c(tau_1^2,ro_1*tau_1*sigma_1, ro_1*tau_1*sigma_1,sigma_1^2),nrow=2),log=T)) else NA } but now I need to have the sum of 24 of these negative log-likelihood. Thanks. Yue Notice: This e-mail message, together with any attachme...{{dropped:11}}
use 24 length vectors as parameters instead of numbers e.g. mu=rep(0.5,24) On Mon, Aug 2, 2010 at 11:00 AM, Shentu, Yue <yue_shentu@merck.com> wrote:> Hi all, > I'm trying to define and log-likelihood function to work with MLE. > There will be parameters like mu_i, sigma_i, tau_i, ro_i, for i between > 1 to 24. Instead of listing all the parameters, one by one in the > function definition, is there a neat way to do it in R ? The example is > as follows: > > ll<- function(mu1=-0.5,b=1.2,tau_1=0.5,sigma_1=0.5,ro_1=0.7) > { if (tau1>0 && ro<1 && ro>-1) > > -sum(dmnorm(cbind(x,y),c(mu1,b*mu1),matrix(c(tau_1^2,ro_1*tau_1*sigma_1, > ro_1*tau_1*sigma_1,sigma_1^2),nrow=2),log=T)) > else NA > } > > but now I need to have the sum of 24 of these negative log-likelihood. > > Thanks. > > Yue > Notice: This e-mail message, together with any attachme...{{dropped:11}} > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
Hi, Putting all parameters into a data frame would help. Code like: parameters <- data.frame(mu=1:24,b=(1:24)*2,tau=(1:24)/2,sigma=(1:24)^2,ro=sqrt(1:24)) ll<- function(parameters){ results <- numeric(24) for (i in 1:24){ mu <- parameters$mu[i] b <- parameters$b[i] ..... results[i] <- sum(mu*b) } results } sum(results) ----- A R learner. -- View this message in context: http://r.789695.n4.nabble.com/Dealing-with-a-lot-of-parameters-in-a-function-tp2310384p2310468.html Sent from the R help mailing list archive at Nabble.com.