使用gmnl()在Latent类模型中建模泛型变量

时间:2016-06-16 09:45:08

标签: r linear-regression mlogit

我在构建模型时遇到了问题,其中至少有一个变量是独立于类的,所以所有类都有一个相同的系数。怎么会这样做? 我正在使用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?谢谢!

1 个答案:

答案 0 :(得分:0)

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