【问题标题】:Extracting Independent variables List from a mlogit object从 mlogit 对象中提取自变量列表
【发布时间】:2015-04-06 09:41:20
【问题描述】:

我正在使用以下软件包:

library(mlogit)

我的数据准备如下

data(CollegeDistance, package="AER")
testdata <- CollegeDistance
testdata$Dist[testdata$distance<0.4] <- 1
testdata$Dist[testdata$distance<1 & testdata$distance>=0.4] <- 2
testdata$Dist[testdata$distance<2.5 & testdata$distance>=1] <- 3
testdata$Dist[testdata$distance>=2.5] <- 4

这是我的模型

testmodel <- mlogit(as.formula(Dist ~ 1|urban + unemp + tuition|1), testdata, shape='wide', choice='Dist')

现在当我运行以下代码时,结果很奇怪

as.character(attr(testmodel$formula, 'rhs')[[2]])

> as.character(attr(testmodel$formula, 'rhs')[[2]])
[1] "+"             "urban + unemp" "tuition"    

我的期望是这样的:

chr [1:3] "urban" "unemp" "tuition"     

【问题讨论】:

    标签: r mlogit


    【解决方案1】:

    您可以改用all.vars

    all.vars(testmodel$formula) # return all the variables 
    ## "Dist"    "urban"   "unemp"   "tuition"
    all.vars(testmodel$formula)[-1] # to remove the dependent variable
    [1] "urban"   "unemp"   "tuition"
    

    还有你用过的功能

    all.vars(attr(testmodel$formula, 'rhs')[[2]])
    ## [1] "urban"   "unemp"   "tuition"
    

    【讨论】:

      猜你喜欢
      • 2013-04-13
      • 2010-11-27
      • 2021-04-09
      • 2011-09-08
      • 2015-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多