I'm trying to maximize the likelihood of logistic distribution with a regression structure. The code is:
cov1 <- rep(1,115)
cov2 <- rnorm(115,0,1)
e <- rlogis(115, 0,1)
yy <- 1*cov1 + 0*cov2 + 2*e
n <- 115
logL <- function(par, x1, x2, y, n){
b1 <- par[1]
b2 <- par[2]
l <- -(sum(y) - n*(b1*x1+b2*x2) - 2* (sum(log(1+exp(y-(b1*x1+b2*x2))))))
return(l)
}
optim(c(1,0), logL, method="BFGS", x1=cov1, x2=cov2, y=yy, n=115)$par
Returns the following error:
Error in optim(start, logL, method = "BFGS", x1 = cov1, x2 = cov2, y = yy, :
função alvo em optim retorna um objeto de comprimento 115 ao invés de 1
I can not find what's wrong.