我尝试对来自离散选择实验的数据执行潜在类分析。受访者需要在具有以下属性的两个选项之间进行选择:他们喜欢的孩子数量,以及他们喜欢的孩子的教育水平(以孩子数量的混合表示)。我的数据的第一行如下所示:
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,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,并且两者具有完全不同的参数估计。
有人知道为什么会这样吗?
非常感谢你提前
亲切的问候
Eva
发布于 2019-11-08 00:53:38
这里有几件事可能是值得考虑的。
你可能会期望两者之间有一些细微的差异,因为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)
https://stackoverflow.com/questions/58610961
复制相似问题