我正在使用ChoiceModelR进行分层多项式logit。我想获得外部商品效用的估计值(其遵循正态分布)。外部货物没有内部货物的协变量 - 例如,它不能有价格或品牌虚拟 - 因此我设置了list(none = TRUE),并且不要将这个无选择项添加到X数据(如ChoiceModelR的文档中所述),而仅添加到y(选择)数据。ChoiceModelR,none = TRUE,“betadraw [good,] = newbeta [good,]中的错误”
的迭代正常启动,然后在某个时候停止,并说
"Error in betadraw[good, ] = newbeta[good, ] : NAs are not allowed in subscripted assignments".
这可能是因为在函数“choicemodelr” 388行中,“好”下标NA。
我看着约choicemodelr(this,this,this)的一些问题,也即将在标(this,this)来港定居,但我的猜测是,我的问题是特定于在这个意义上这个功能,可能一些投入在迭代中只是变得如此大/小,这样“好”会变成NA。
下面是一个非常简单的例子。我使用具有不同属性的3种产品生成数据。在一半的时间内不提供产品3。 2000年的消费者有偏好 - 通常分配 - 超过3个属性(以及对外部商品的偏好)。添加的Logit错误与模型一致。外部商品被指定为产品4(当3个和2个产品都在选择集合中时)。
我怎样才能避免NA错误?我做错了什么,还是它在函数中的一个常见错误?
我也搜索了在线设置示例none = TRUE的示例,但是我没有找到任何可重现的示例。也许这个选项只是有问题的事情,因为如果我设置none = FALSE,恢复真实参数没有问题,并且我不让客户选择外部选项。
因此,这导致NA的bug的代码如下:
library("ChoiceModelR")
library("MASS")
set.seed(36)
# Set demand pars
beta_mu = c(-3,4,1)
beta_sigma = diag(c(1,1,1))
alfa_mu = 5 #outside good mean utility
alfa_sigma = 2 #outside good sd
# Three/two products, 3 vars (2 continuous,1 dummy)
threeprod <- list()
twoprod <- list()
purchase <- list()
for (t in 1:1000){
threeprod[[t]] = cbind(rep(t,3),c(1,1,1),c(1,2,3),runif(3),runif(3),ceiling(runif(3,-0.5,0.5)))
purchase[[t]] = which.max(rbind(threeprod[[t]][,c(4,5,6)]%*%mvrnorm(1,beta_mu,beta_sigma) +
matrix(-log(-log(runif(3))), 3, 1),rnorm(1,alfa_mu,alfa_sigma)))
threeprod[[t]] = cbind(threeprod[[t]],c(purchase[[t]],0,0))
}
for (t in 1001:2000){
twoprod[[t]] = cbind(rep(t,2),c(1,1),c(1,2),runif(2),runif(2),ceiling(runif(2,-0.5,0.5)))
purchase[[t]] = which.max(rbind(twoprod[[t]][,c(4,5,6)]%*%mvrnorm(1,beta_mu,beta_sigma) +
matrix(-log(-log(runif(2))), 2, 1),rnorm(1,alfa_mu,alfa_sigma)))
if (purchase[[t]] == 3) {purchase[[t]] <- 4}
twoprod[[t]] = cbind(twoprod[[t]],c(purchase[[t]],0))
}
X <- rbind(do.call(rbind,threeprod),do.call(rbind,twoprod))
xcoding <- c(1,1,1)
mcmc = list(R = 5000, use = 2000)
options = list(none=TRUE, save=TRUE, keep=5)
out = choicemodelr(X, xcoding, mcmc = mcmc,options = options)