【问题标题】:How to apply the gmnl function in R for a latent class analysis of a discrete choice experiement?如何将 R 中的 gmnl 函数应用于离散选择实验的潜在类分析?
【发布时间】:2019-10-29 15:50:11
【问题描述】:

我尝试对来自离散选择实验的数据执行潜在类别分析。受访者需要在具有 as 属性的 2 个选项之间进行选择:他们喜欢的孩子数量,以及他们喜欢孩子的教育水平(表示为孩子数量的混合)。我的数据的第一行如下所示:

Respondent Block Choice card Chosen FNoPrimary  FPrimary FSecondary FTertiary MNoPrimary
 1          1           1      1    0.0000000  0.0000000   0.00     0.0000000  0.0000000
 1          1           1      0    0.3333333  0.6666667   0.00     0.0000000  0.0000000
 1          2          12      0    0.3333333  0.3333333   0.00     0.0000000  0.0000000
 1          2          12      1    0.1666667  0.0000000   0.00     0.3333333  0.1666667
 1          3           2      0    0.0000000  0.0000000   1.00     0.0000000  0.0000000
 1          3           2      1    0.0000000  0.0000000   0.25     0.0000000  0.0000000
  MPrimary MSecondary MTertiary NChildren Age District   Religion Indigenous Ethnic group    Sex
1        0       1.00 0.0000000         1  18        0 Protestant          0      Wolaita Female
2        0       0.00 0.0000000         3  18        0 Protestant          0      Wolaita Female
3        0       0.00 0.3333333         9  18        0 Protestant          0      Wolaita Female
4        0       0.00 0.3333333        12  18        0 Protestant          0      Wolaita Female
5        0       0.00 0.0000000         1  18        0 Protestant          0      Wolaita Female
6        0       0.25 0.5000000         4  18        0 Protestant          0      Wolaita Female
       Educational level Studentornot Farmerornot Marital status Having children Ever used contraception
1 High school - grade 10            1           0              0               0                       0
2 High school - grade 10            1           0              0               0                       0
3 High school - grade 10            1           0              0               0                       0
4 High school - grade 10            1           0              0               0                       0
5 High school - grade 10            1           0              0               0                       0
6 High school - grade 10            1           0              0               0                       0
  Alternative
1           1
2           2
3           1
4           2
5           1
6           2

我查看了 R 中所有可用的包,我认为只有 gmnl 包可以处理我的数据类型并且能够添加协变量。但是,如果我比较一个简单线性模型的潜在类分析的输出与只有 2 个协变量(年龄和地区)(如下所述),我将成为参数估计和对数似然的完全不同的输出(Stata 中的 -2598.6 vs R 中的 -2495.5)然后当我使用 Stata 执行相同的分析时(参见下面的代码)。

在R中:

defining_data <- mlogit.data(final_data_alternativeadded, id.var = "Respondent", choice = "Chosen", alt.var = "Alternative", chid.var="Choice.card", group.var = "Block", varying = 7:15, shape = "long")
mnl <- gmnl(Chosen ~ 1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + z | 0 | 0 | 0 | Age + District, data = defining_data, model = 'lc', Q = 3) 
summary(mnl)

在Stata中:

ssc install lclogit
ssc install fmlogit
lclogit chosen fprimary fsecondary ftertiary mnoprimary mprimary msecondary mtertiary block nchildren, group(choicecard) id(respondent) nclasses(3) membership(age district)

我尝试将所有变量设为数字,将混合比例乘以孩子的数量以获得彼此更接近的值,根据每个受访者的选择卡数量对我的数据集进行排序。 .但我总是得到潜在类概率的其他值。有人知道为什么吗?我知道 Stata 中的 lclogit 使用期望最大化算法,而 R 中的 gmnl 使用最大似然法,但我认为参数估计和对数似然不会因此而完全不同。

我还尝试使用现有的开放数据集:

状态:

use http://fmwww.bc.edu/repec/bocode/t/traindata.dta, clear
ssc install lclogit
ssc install fmlogit
lclogit y price contract local wknown tod seasonal, group(gid) id(pid) nclasses(3) seed(12345)

R:

getwd()
traindata <- read.xlsx("traindata.xlsx", 1, header = TRUE)
library(mlogit)
library(gmnl)
traindata[1:nrow(traindata),11] <- seq(1,4) #to add a column with the alternatives per choice numbered from 1 to 4
names(traindata) <- c('y', 'price', 'contract', 'local', 'wknown', 'tod', 'seasonal', 'gid', 'pid', 'X_xi', 'Alternative')
TM <- mlogit.data(traindata, choice = "y", id.var = "pid", alt.var = "Alternative", chid.var = "gid", shape = "long")
mnl <- gmnl(y ~ price + contract + local + wknown + tod + seasonal | 0 | 0 | 0 | 1, data = TM, model = 'lc', Q = 3)
summary(mnl)

但是,Stata 声明它们成为 -1117.9987 的对数似然,而 R 变为 -1329.5,并且两者都有完全不同的参数估计。

有人知道为什么会这样吗?

非常感谢您

亲切的问候

伊娃

【问题讨论】:

    标签: r


    【解决方案1】:

    这里有几件事值得考虑。

    您可能会认为两者之间存在一些细微差别,因为 i) 潜在类模型很容易根据起始值收敛到不同的局部最优值,并且 ii) 与最大似然相比,期望最大化算法更有可能。但是,这里不太可能出现这种情况。

    问题可能是 Stata 考虑了数据的面板结构,而在 R 中,您指定了一个横截面潜在类模型,将每个观察视为独立的。 LL 值的巨大差异是一个强有力的指标。

    试试这个(使用panel = TRUE):

    mnl <- gmnl(y ~ price + contract + local + wknown + tod + seasonal | 0 | 0 | 0 | 1,
     data = TM, model = 'lc', Q = 3, panel = TRUE)
    

    【讨论】:

      猜你喜欢
      • 2023-01-04
      • 2021-10-07
      • 2018-02-13
      • 1970-01-01
      • 1970-01-01
      • 2014-09-29
      • 2010-12-14
      • 2019-12-26
      • 2020-06-19
      相关资源
      最近更新 更多