1

我在制定模型时遇到问题,其中至少一个变量要独立于类进行估计,因此所有类的系数相同。怎么可能做到这一点?我正在使用 R 包 gmnl。

install.packages("gmnl")
library(gmnl)
library(mlogit)
#browseURL("https://cran.r-project.org/web/packages/poLCA/index.html")
## Examples using the Fishing data set from the AER package

data("Electricity", package = "mlogit")
Electr <- mlogit.data(Electricity, id.var = "id", choice = "choice",
                      varying = 3:26, shape = "wide", sep = "")
Elec.lc <- gmnl(choice ~ pf + cl + loc + wk + tod + seas| 0 | 0 | 0 | 1,
                data = Electr,
                subset = 1:3000,
                model = 'lc',
                panel = TRUE,
                Q = 2)
summary(Elec.lc)

您将如何独立于类对变量 pf、cl、loc、wk、tod 或 seas 之一进行建模?谢谢!

4

1 回答 1

1

感谢 Mauricio Sarrias,我可以提出这个解决方法,它应该可以解决问题:

###################################
library("gmnl")
library("mlogit")

# Data
data("Electricity", package = "mlogit")
Electr <- mlogit.data(Electricity, id.var = "id", choice = "choice",
                    varying = 3:26, shape = "wide", sep = "")
# ASCs
Electr$asc2 <- as.numeric(Electr$alt == 2)
Electr$asc3 <- as.numeric(Electr$alt == 3)
Electr$asc4 <- as.numeric(Electr$alt == 4)

# We estimate a MNL for the initial values of LC-MNL
init_mnl <- gmnl(choice ~ asc2 + asc3 + asc4 + pf + cl| 0,
                  data = Electr)
summary(init_mnl)


# Work on initial values for LC-MNL
init <- coef(init_mnl)
Q <- 2 # Number of Classes

init.shift <- seq(-0.02, 0.02, length.out = Q)
lc.mean <- c()
for(i in 1:Q){
  lc.mean  <- c(lc.mean,  init + init.shift[i])
}
lc.names <- c()
lc.nalpha <- c()
for (i in 1:Q){
  lc.names <- c(lc.names, paste('class', i, names(init), sep = '.'))
}
names(lc.mean) <- lc.names

# Now we fix pf coefficient  = 0 in the second class
lc.mean[c("class.2.pf")] <- 0
start_lc <- c(lc.mean, # Var coefficients
                 0)     #Constant for second class


# Estimate LC with price coefficient held fixed at 0 in class 2
lc <- gmnl(choice ~ asc2 + asc3 + asc4 + pf + cl| 0 | 0 | 0 |1,
                  data = Electr,
                  model = "lc",
                  iterlim = 500,
                  start = start_lc,
                  fixed = c(rep(FALSE, 8), TRUE, rep(FALSE, 2)), # note that class.2.pf is fixed at 0
                  print.level = 3,
                  print.init = TRUE,
                  Q = 2)
summary(lc)
########################
于 2016-06-28T10:26:02.453 回答