【发布时间】: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