0

mnlogit パッケージを使用してモデルを適合させ、それを使用して標本外予測を行いたいと考えています。mnlogit に付属の釣りデータを使用して、おもちゃの例をセットアップしました。

library(data.table)
library(mnlogit)

data(Fish, package="mnlogit")
fish_dt <- data.table(Fish)
rm(Fish)

unique_id <- unique(fish_dt[, chid])
set.seed(54321)
train_id <- sample(unique_id, size=0.5*length(unique_id))

setkey(fish_dt, chid, alt)
train <- fish_dt[J(train_id)]
test <- fish_dt[!J(train_id)]
setkey(train, chid, alt)
setkey(test, chid, alt)
stopifnot(nrow(train) + nrow(test) == nrow(fish_dt))  # Partition fish_dt

mnlogit_formula <- mode ~ catch | income
mnlogit_model <- mnlogit(mnlogit_formula, data=train, choiceVar="alt")

train_predictions <- predict(mnlogit_model, probability=F)
stopifnot(length(train_predictions) == length(unique(train[, chid])))  # One per choice
mean(subset(train, mode)[, alt] == train_predictions)  # Around 0.42 accuracy in sample

## Would like to do the same out of sample, i.e. with data table "test"
test_predictions <- predict(mnlogit_model, newdata=test, probability=F)  # Error
test_predictions <- predict(mnlogit_model, newdata=as.data.frame(test), probability=F)  # Same error

私が得るエラーは次のとおりです。

colnames<-(のエラー*tmp*、value = list(chid = c(1L, 2L, 3L, 4L, 5L, : 'dimnames' の長さ [2] が配列の範囲と等しくない

Ubuntu 14.04.2 LTS で R バージョン 3.0.2 (2013-09-25) を実行しています。

パッケージを間違って使用していますか、それともバグですか?

編集:コメントを参照してください:「テスト」データテーブルから「モード」列を削除しようとしましたが、「新しいデータにはトレーニングデータと同じ列が必要です」というエラーが表示されます:

test[, mode := NULL]
mnlogit_predictions <- predict(mnlogit_model, newdata=test, probability=F)  # Error

編集: これは、mlogit パッケージを使用する例です (これは似ていますが、大きな問題の場合は大幅に遅くなる可能性があります)。

library(data.table)
library(mlogit)

data(Fish, package="mnlogit")
fish_dt <- data.table(Fish)
rm(Fish)

unique_id <- unique(fish_dt[, chid])
set.seed(54321)
train_id <- sample(unique_id, size=0.5*length(unique_id))

setkey(fish_dt, chid, alt)
train <- fish_dt[J(train_id)]
test <- fish_dt[!J(train_id)]
setkey(train, chid, alt)
setkey(test, chid, alt)
stopifnot(nrow(train) + nrow(test) == nrow(fish_dt))  # Partition fish_dt

train_mlogit <- mlogit.data(train, choice="mode", shape="long",
                            chid.var="chid", alt.var="alt")
test_mlogit <- mlogit.data(test, choice="mode", shape="long",
                           chid.var="chid", alt.var="alt")

model_formula <- mode ~ catch | income
mlogit_model <- mlogit(model_formula, data=train_mlogit)

## In-sample performance
train_predictions <- predict(mlogit_model, newdata=train_mlogit)
stopifnot(nrow(train_predictions) == length(unique(train[, chid])))  # One per choice
train_predictions <- colnames(train_predictions)[apply(train_predictions, 1, which.max)]
mean(subset(train, mode)[, alt] == train_predictions)  # Around 0.42 accuracy in sample

## Out-of-sample performance
test_predictions <- predict(mlogit_model, newdata=test_mlogit)
test_predictions <- colnames(test_predictions)[apply(test_predictions, 1, which.max)]
mean(subset(test, mode)[, alt] == test_predictions)  # Around 0.41 accuracy out of sample

私はまさにそれをやりたいのですが、mlogit の代わりに mnlogit を使用します。

4

1 に答える 1

0

Predict は mnlogit オブジェクトでうまく機能します。ただし、「テスト」オブジェクトはデータ テーブルであり、mlogit.data オブジェクトではないため、予測呼び出しでも ChoiceVar を渡す必要があります。

test_predictions <- predict(mnlogit_model, newdata=test,choiceVar="alt")  # works
test_predictions <- predict(mnlogit_model, newdata=as.data.frame(test), probability=F,choiceVar="alt")  # this also works

ありがとう

于 2017-06-07T08:58:52.557 に答える