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

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日

悬赏问题

  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。