我正在使用以下套餐:
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"
答案 0 :(得分: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"