I'm writing a code to have a travel demand forecasting using the Multinomial Logit Model without using packages. However, I'm having an error in the BFGS optim part. I'd appreciate if you take a look at my code.
Part of the code is shown below.
OutPut <-data.frame(PCar,PBus,PTaxi,PSharedtaxi,PWalk,SCar,SBus,STaxi,SSharedtaxi,SWalk)
write.table(OutPut,"Output.csv",quote=F,col.names=T,append=T,sep=",")
LL <-colSums(SCarlog(PCar)+SBuslog(PBus)+STaxilog(PTaxi)+SSharedtaxilog(PSharedtaxi)+SWalk*log(PWalk))
}
b0 <-numeric(12)
res_BFGS <-optim(b0,fr, method="BFGS", hessian=TRUE, control=list(fnscale=-1))
show <-function(res,b0){
b <-res$par
hhh <-res$hessian
tval <-b/sqrt(-diag(solve(hhh)))
L0 <-hh*log(1/5)
LL <-res$value
print(b)
print(tval)
print(hhh)
print(L0)
print(LL)
print((L0-LL)/L0)
print((L0-(LL-length(b)))/L0)
}
show(res_BFGS,b0)
Error:
Error in colSums(SCar * log(PCar) + SBus * log(PBus) + STaxi * log(PTaxi) + :
'x' must be an array of at least two dimensions
In addition: Warning message:
In write.table(OutPut, "Output.csv", quote = F, col.names = T, append = T, :
appending column names to file
Called from: colSums(SCar * log(PCar) + SBus * log(PBus) + STaxi * log(PTaxi) +
SSharedtaxi * log(PSharedtaxi) + SWalk * log(PWalk))