<my code =“”>中的错误:类型&#39;关闭的对象&#39;不是子集(2)</my>

时间:2015-04-06 23:05:40

标签: r mlogit

我知道这个问题在(Error in <my code> : object of type 'closure' is not subsettable)之前提出过。但我无法理解它。

以下是我使用的软件包以及我如何准备数据

library(mlogit)

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

现在,当我定义一个mlogit对象并将其用于预测时,我得到了这个错误。

Formula <- paste('Dist ~', paste('1|',paste(c("urban", "unemp", "tuition"), collapse = " + "),'|1')) 
Model <- mlogit(as.formula(Formula), Data, shape='wide', choice='Dist')

Predict <- predict(Model, newdata=mlogit.data(Data, shape='wide', choice='Dist'), returnData=FALSE)

有趣的是,如果我将Formula替换为formula,那么它就可以了!

更新

我在函数中使用mlogit时遇到了这个问题。如果你能告诉我一条出路,我真的很感激。

modelmaker <- function(variables){
  Formula <- paste('Dist ~', paste('1|',paste(variables, collapse = " + "),'|1'))
  MODEL <- mlogit(as.formula(Formula), Data, shape='wide', choice='Dist')  
  return(MODEL)
}

Model <- modelmaker(c("urban", "unemp", "tuition"))
Predict <- predict(Model, newdata=mlogit.data(Data, shape='wide', choice='Dist'), returnData=FALSE)

即使避免使用formulaFormula,此时间也无法解决。如果您将其更改为XXX,则错误将为

object 'XXX' not found

0 个答案:

没有答案