bgdrgj 2022-07-10 20:23 采纳率: 66.7%
浏览 1230
已结题

R语言报错'x'必需是阵列,而且至少得有两个维度

########### loading of data file #################
Data <- read.csv("C:/Users/1/Desktop/R sample code/R sample code/MXL/Data_Clean_English.csv",header=T)
##count of low of data
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(8) 

##### random number generation #####

## the number of random number generation
R <- 100

## random number generation - random draw method
#rand1 <- matrix(rnorm(hh*R),hh,R)
#rand2 <- matrix(rnorm(hh*R),hh,R)

## random number generation - halton  progression function method
#### halton  progression function 
halton <- function(n,x) {
  no <- 1:n
  hal <- numeric(n)
  i <- 1
  while (n >= x^i) {
    i <- i + 1
  }
  for (j in i:1 ) {
    hal <- hal + ( no %/% (x^(j-1)) ) * (1/ x^j)
    no <- no %% x^(j-1)
  }
  hal
}
rand1 <- matrix(qnorm(halton(hh*R,2)),hh,R)
rand2 <- matrix(qnorm(halton(hh*R,3)),hh,R)

##### definition of Log-likelihood function in Logit model #####

fr <- function(x) {
  
  ### declare the parameters###
  ## Alternative specific constants
  b1 <- x[1]
  b2 <- x[2]
  b3 <- x[3]
  b4 <- x[4]
  
  ## time to destination
  dmu1 <- x[5] #average
  dsigma1 <- x[6] #standard deviation 
  
  ## price
  fmu1 <- x[7] #average
  fsigma1 <- x[8] #standard deviation   
  
  ## declare the variables for the simulation likelihood
  SimLL = 0
  
  ### destination is as follows in this estimation
  ## train
  ## bus
  ## car
  ## bike
  ## walk
  
  ## R times: the number of loop for simulation
  for (i in 1:R) {
    
    ## declare the log-likelihood variable, set value to 0
    LL = 0
    
    ##generate parameter from random number by using simulation
    d1 <- dmu1 + dsigma1 * rand1[,i]
    f1 <- fmu1 + fsigma1 * rand2[,i]
    
    ## calculate the utility function: :introduce the desired explanatory variables in the function
                                                    # ,drop=Ftime                    # Fare                    # constant
    train  <- Data$ModeAvailabletrain*exp(d1*Data$TotalTimetrain/100 +f1*Data$Faretrain/100 + b1*matrix(1,nrow =hh,ncol=1))
    bus    <- Data$ModeAvailablebus  *exp(d1*Data$TotalTimebus/100   +f1*Data$Farebus/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                                                            )
    
    ################# caluculate choice probability #################### 
    ## 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$ModeAvailablebus  *(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))
    
    SimLL <- SimLL + LL 
    
    ##loop end
  }
  
  ##print the caluculation process
  print(x)
  print(SimLL/R)
  
  ## divide by the number of the repetition
  SimLL <- SimLL / R
  
}

#############  maximize Log-likelihood function, fr ################

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

## Parmeter estimation丄Hessian matrix calculation 
b   <- res$par
hhh <- res$hessian

## Calculate the t-statistic
tval <- b/sqrt(-diag(solve(hhh)))

## L(0), Log-Likelihood when all parameters are 0
L0 <- fr(b0)
## LL, maximium likelihood
LL <- res$value

##### Output #####
print(res)
## L(0)
print(L0)
## LL
print(LL)
##rho-square
print((L0-LL)/L0)
## adjusted rho-square
print((L0-(LL-length(b)))/L0)
##estimated parameter values
print(b)
## t-statistic 
print(tval)
报错内容

Error in colSums(Ctrain * log(Ptrain) + Cbus * log(Pbus) + Ccar * log(Pcar) + :
'x'必需是阵列,而且至少得有两个维度
Called from: colSums(Ctrain * log(Ptrain) + Cbus * log(Pbus) + Ccar * log(Pcar) +
Cbike * log(Pbike) + Cwalk * log(Pwalk))

  • 写回答

1条回答 默认 最新

  • huiyiiiiii 2022-07-11 10:20
    关注

    请确保colSum内的内容的是二维或以上数组
    利用dim()函数确定colsum函数内的维度,如果不是2维,尝试将格式转化为二维数组。

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

报告相同问题?

问题事件

  • 系统已结题 7月20日
  • 已采纳回答 7月12日
  • 创建了问题 7月10日

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装