Hi! I have a difficulty in the use of function optimize(). Could you help me? I want to maximize this function: ##### Logaritmo da distribui??o condicional de alpha[i] lp_alphai <- function(alphai, i, beta, tau, N){ t1 <- (N[i+1] - N[i])*log(alphai) t2 <- - (N[i+1] - N[i])*alphai*log(beta[i]) t3 <- (alphai - 1)*sum(log(times[(N[i] + 1):N[i+1]])) t4 <- - exp(alphai*(log(tau[i+1]) - log(beta[i]))) t5 <- 0 if(i > 1) t5 <- exp(alphai*(log(tau[i]) - log(beta[i]))) v <- t1 + t2 + t3 + t4 + t5 return(v) } Then, I've done: optimize(lp_alphai, lower = 0, upper = 10, maximum = TRUE, tol 0.00000000001, i = 1, beta = 125, tau = c(0,1900), N = c(0,236))$maximum However, I face this problem: Error in i + 1 : 'i' missing I've been in this problem all this day, but, I can't find a solution. Thank you. -- View this message in context: http://r.789695.n4.nabble.com/optimize-tp4173920p4173920.html Sent from the R help mailing list archive at Nabble.com.
dnz.marcio <dnz.marcio <at> gmail.com> writes:>[snip]> ##### Logaritmo da distribui??o condicional de alpha[i] > > lp_alphai <- function(alphai, i, beta, tau, N){ > > t1 <- (N[i+1] - N[i])*log(alphai) > t2 <- - (N[i+1] - N[i])*alphai*log(beta[i]) > t3 <- (alphai - 1)*sum(log(times[(N[i] + 1):N[i+1]])) > t4 <- - exp(alphai*(log(tau[i+1]) - log(beta[i]))) > > t5 <- 0 > if(i > 1) > t5 <- exp(alphai*(log(tau[i]) - log(beta[i]))) > > v <- t1 + t2 + t3 + t4 + t5 > > return(v) > > } > > Then, I've done: > > optimize(lp_alphai, lower = 0, upper = 10, maximum = TRUE, tol > 0.00000000001, i = 1, beta = 125, tau = c(0,1900), N = c(0,236))$maximum > > However, I face this problem: > Error in i + 1 : 'i' missing >This is actually fairly subtle: you're running into a partial matching problem with the "interval" argument of optimize(). I added interval=NULL to the call and got a little farther, but then I ran into an error because I hadn't specified the "times" variable. Presumably you have defined a "times" variable? Ben Bolker