Error in optimum function

1

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.

    
asked by anonymous 17.09.2016 / 21:25

1 answer

3

In the logistic regression the function to be maximized is the following:

Ifyou'regoingtoimplementitinR,itwouldlooklikethis(optimizingminimizes,so-upfront).

logL<-function(par,x1,x2,y,n){b1<-par[1]b2<-par[2]l<--sum(y*(x1*b1+x2*b2)-n*log(1+exp(x1*b1+x2*b2)))return(l)}

However,yourexampleseemstohavesomethingstrange...Logisticregressionisfordiscretedataandnotforcontinuousnumbers.(Itsyyvariableiscontinuous).Inaddition,%w/w%isnotthesamplesize,but%w/oofthebinomialyouareestimatingforeachindividual.

Iwouldmakeanexampleasfollows:

>set.seed(1)>cov1<-runif(100)>cov2<-runif(100)>>yy<-rbinom(n=100,prob=(cov1+cov2)/2,size=1)>>glm(yy~0+cov1+cov2,family="binomial")

Call:  glm(formula = yy ~ 0 + cov1 + cov2, family = "binomial")

Coefficients:
  cov1    cov2  
0.2138  0.1638  

Degrees of Freedom: 100 Total (i.e. Null);  98 Residual
Null Deviance:      138.6 
Residual Deviance: 137.6    AIC: 141.6
> optim(par = c(1,1), fn = logL, x1 = cov1, x2 = cov2, y = yy, n = 1)
$par
[1] 0.2132318 0.1643093

$value
[1] 68.77538

$counts
function gradient 
      55       NA 

$convergence
[1] 0

$message
NULL

Note that in this case, the calculation using the function n of R, is equal to the version using n .

    
19.09.2016 / 15:56