bgdrgj 2022-07-08 10:32 采纳率: 66.7%
浏览 298
已结题

R语言报错optim无法在初始值计算函数

### Multinomial Logit model estimation

### Create the data array from the existing dataset
Data <- read.csv("C:/Users/1/Desktop/R sample code/R sample code/MNL/Data_Clean_English.csv",header=TRUE)
## Count the number of data rows
hh <- nrow(Data)

## Set the initial values of the parameters (the number in parenthesis corresponds to the number of parameters to be estimated)
b0 <- numeric(5)

## Recall that we estimate parameters by maximizing the log-likelihood function, hence we first need to define that function

##### Define the log-likelihood function of the logit model#####

fr <- function(x) {
  ### declare the parameters###
  ## Alternative specific constants
  b1 <- x[1]
  b2 <- x[2]
  b3 <- x[3]
  b4 <- x[4]
  
  ## Travel time to destination
  d1 <- x[5]
  
  ## declare the log-likelihood variable, set value to 0
  LL = 0
  
  ### For this choice problem, we consider the following 5 modes???

  ## calculate the utility function: :introduce the desired explanatory variables in the function
  train  <- Data$ModeAvailableTrain*exp(d1*Data$TotalTimeTrain/100  +b1*matrix(1,nrow =hh,ncol=1))
  bus    <- Data$ModeAvailableBus  *exp(d1*Data$TotalTimeBus/100    +b2*matrix(1,nrow =hh,ncol=1))
  car    <- Data$ModeAvailableCar  *exp(d1*Data$TimeCar/100         +b3*matrix(1,nrow =hh,ncol=1))
  bike   <- Data$ModeAvailableBike *exp(d1*Data$TimeBike/100        +b4*matrix(1,nrow =hh,ncol=1))
  walk   <- Data$ModeAvailableWalk *exp(d1*Data$TimeWalk/100                                                     )
  
  ### Calculate the choice probabilities
  ## calculate the Inclusive Value (the denominator of the choice probabilities equation)
  deno <- (car + train + bus + bike + walk)
  
  ## Calculate indiviudal choice probabilities
  Ptrain <- Data$ModeAvailableTrain*(train / deno)
  Pbus   <- Data$ModeAvailableTrain  *(bus   / deno)
  Pcar   <- Data$ModeAvailableCar  *(car   / deno)
  Pbike  <- Data$ModeAvailableBike *(bike  / deno)
  Pwalk  <- Data$ModeAvailableWalk *(walk  / deno)
  
  ## Avoid problems stemming from choice probabilities becoming zero.
  Ptrain <- (Ptrain!=0)*Ptrain + (Ptrain==0)
  Pbus   <- (Pbus!=0)*Pbus     + (Pbus==0)
  Pcar   <- (Pcar  !=0)*Pcar   + (Pcar  ==0)
  Pbike  <- (Pbike  !=0)*Pbike + (Pbike  ==0)
  Pwalk  <- (Pwalk!=0)*Pwalk   + (Pwalk  ==0)
  
  
  
  ## Choice results
  Ctrain   <- Data$MainModeENG =="Rail"
  Cbus     <- Data$MainModeENG =="Bus"
  Ccar     <- Data$MainModeENG =="Car"
  Cbike    <- Data$MainModeENG =="Bicycle"
  Cwalk    <- Data$MainModeENG =="Walk"
  
  ## Calculate the Log-likelihood function
  LL <- colSums(Ctrain*log(Ptrain) + Cbus*log(Pbus) +
                  Ccar  *log(Pcar)   + Cbike  *log(Pbike) +Cwalk *log(Pwalk))
}
##### Maximize the Log-likelihood function#####

##Parameter optimization 
res<-optim(b0,fr,method="Nelder-Mead",hessian=TRUE,control=list(fnscale=-1))

## Parmeter estimation銆丠essian matrix calculation 
b   <- res$par
hhh <- res$hessian


运行Parameter optimization那里报错

res<-optim(b0,fr,method="Nelder-Mead",hessian=TRUE,control=list(fnscale=-1))
Error in optim(b0, fr, method = "Nelder-Mead", hessian = TRUE, control = list(fnscale = -1)) :
function cannot be evaluated at initial parameters

  • 写回答

1条回答 默认 最新

  • Kappuccinno 2022-07-08 21:38
    关注

    这样,在初始设置参数的时候的b0,不要都设成0,变成这样c(0,0,0,0,1)试一下

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 7月18日
  • 已采纳回答 7月10日
  • 赞助了问题酬金10元 7月8日
  • 赞助了问题酬金10元 7月8日
  • 展开全部

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。