加速R模型预测

时间:2015-09-13 14:37:08

标签: r optimization

我试图在R中编写一个模拟,其中涉及从几个不同的模型中进行一系列预测(来自负二项GLM的约200个和来自多项logit的5000个)。这些预测真的让我的代码陷入困境,并且需要很长时间才能运行。有什么办法可以加快这个预测过程吗?

多项logit的简化示例:

library(mnlogit)
library(mlogit)
data("Fishing", package = "mlogit")
Fish <- mlogit.data(Fishing, varying = c(2:9), shape = "wide", choice = "mode")
m <- mnlogit(mode ~price+ catch | income, data = Fish)
iterations <- 5000
for (i in (1:iterations)){
  newdata = data.frame("mode" = c("beach", "boat", "charter", "pier"), "price" = runif(4,15,150),
                       "catch" = runif(4,0,1), "income" = rep(runif(1,1500,7500),4))
  newdata<-mlogit.data(newdata, shape="long", choice="mode", alt.var="mode")
  prediction <- predict(m, newdata=newdata)
}

0 个答案:

没有答案