3

我正在使用以下软件包:

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"     
4

1 回答 1

3

您可以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"
于 2015-04-06T09:49:20.050 回答