########### 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))