Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add custom models to be used in the transition functions #80

Closed
4 of 5 tasks
asiripanich opened this issue May 27, 2020 · 2 comments
Closed
4 of 5 tasks

add custom models to be used in the transition functions #80

asiripanich opened this issue May 27, 2020 · 2 comments
Labels
feature New feature or request

Comments

@asiripanich
Copy link
Member

asiripanich commented May 27, 2020

@asiripanich asiripanich added the feature New feature or request label May 27, 2020
@asiripanich
Copy link
Member Author

asiripanich commented May 28, 2020

The multinomial predict function of ModelCustom works as intended when there is no alternative specific constants or variables in the formula. However, if those were to be added then it would give a wrong result. This should be generalised to work with all mlogit formulations.

library(mlogit)
#> Loading required package: Formula
#> Loading required package: zoo
#> 
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#> 
#>     as.Date, as.Date.numeric
#> Loading required package: lmtest
library(dymiumCore) # branch: mlogit
#> ── * dymium's options * ───────────────────────────────────────────────────────────────────────────────────────────────────
#> ● dymium.input_dir: /var/folders/0d/9srpj_750lxbkfs2_8nwkcpw0000gn/T//Rtmp3LJUfZ/scenario/inputs
#> ● dymium.output_dir: /var/folders/0d/9srpj_750lxbkfs2_8nwkcpw0000gn/T//Rtmp3LJUfZ/scenario/outputs
#> ● dymium.scenario_dir: /var/folders/0d/9srpj_750lxbkfs2_8nwkcpw0000gn/T//Rtmp3LJUfZ/scenario
#> ● dymium.simulation_clock: 0
#> ● dymium.simulation_scale: 1
data("Fishing", package = "mlogit")
Fish <- mlogit.data(Fishing, varying = c(2:9), shape = "wide", choice = "mode")

## a pure "conditional" model
fm <- mFormula(mode ~ price + catch | 0)
mm <- model.matrix(fm, Fish)

mod <- mlogit(mode ~ price + catch | 0, data = Fish)
md <- ModelCustom$new(params = mod$coefficients, formula = fm, type = "multinomial")

mlogit_prediction <- predict(mod, Fish)
my_prediction <- md$predict(Fish, chooser_id_col = "chid", choice_id_col = "alt")

head(my_prediction)
#>    chooser_id choice_id linear_comb      prob
#> 1:          1     beach -3.16923742 0.2424937
#> 2:          1      boat -2.98595663 0.2912717
#> 3:          1   charter -3.23195532 0.2277521
#> 4:          1      pier -3.18591664 0.2384826
#> 5:          2     beach -0.20950218 0.2470243
#> 6:          2      boat -0.06568204 0.2852332
head(mlogit_prediction)
#>        beach      boat   charter       pier
#> 1 0.24249366 0.2912717 0.2277521 0.23848260
#> 2 0.24702432 0.2852332 0.2344037 0.23333881
#> 3 0.03617426 0.4577779 0.4725643 0.03348346
#> 4 0.34947274 0.1661746 0.1311631 0.35318957
#> 5 0.11609212 0.4605316 0.3092044 0.11417185
#> 6 0.02923272 0.5866468 0.3570623 0.02705826

Created on 2020-05-28 by the reprex package (v0.3.0)

@asiripanich
Copy link
Member Author

asiripanich commented May 28, 2020

I retract my earlier comment. Apparently the model.matrix object that gets created from mFormula (from mlogit) does have dummy columns for alternative specific constants. This works in with alternative specific variables as well.

library(mlogit)
#> Loading required package: Formula
#> Loading required package: zoo
#> 
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#> 
#>     as.Date, as.Date.numeric
#> Loading required package: lmtest
library(dymiumCore) # branch: mlogit
#> ── * dymium's options * ───────────────────────────────────────────────────────────────────────────────────────────────────
#> ● dymium.input_dir: /var/folders/0d/9srpj_750lxbkfs2_8nwkcpw0000gn/T//Rtmp3LJUfZ/scenario/inputs
#> ● dymium.output_dir: /var/folders/0d/9srpj_750lxbkfs2_8nwkcpw0000gn/T//Rtmp3LJUfZ/scenario/outputs
#> ● dymium.scenario_dir: /var/folders/0d/9srpj_750lxbkfs2_8nwkcpw0000gn/T//Rtmp3LJUfZ/scenario
#> ● dymium.simulation_clock: 0
#> ● dymium.simulation_scale: 1
data("Fishing", package = "mlogit")
Fish <- mlogit.data(Fishing, varying = c(2:9), shape = "wide", choice = "mode")

## a pure "conditional" model
fm <- mFormula(mode ~ price + catch)
mm <- model.matrix(fm, Fish)

mod <- mlogit(fm, data = Fish)
md <- ModelCustom$new(params = mod$coefficients, formula = fm, type = "multinomial")

mlogit_prediction <- predict(mod, Fish)
my_prediction <- md$predict(Fish, chooser_id_col = "chid", choice_id_col = "alt")

head(my_prediction, 4)
#>    chooser_id choice_id linear_comb      prob
#> 1:          1     beach   -3.889442 0.1282391
#> 2:          1      boat   -2.945537 0.3295740
#> 3:          1   charter   -2.832532 0.3690034
#> 4:          1      pier   -3.588987 0.1731834
head(mlogit_prediction, 1)
#>       beach     boat   charter      pier
#> 1 0.1282391 0.329574 0.3690034 0.1731834

Created on 2020-05-28 by the reprex package (v0.3.0)

This was referenced May 29, 2020
@asiripanich asiripanich changed the title choice models add custom models to be used in the transition functions Jun 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant