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

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 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加