-
Notifications
You must be signed in to change notification settings - Fork 1
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
Comments
The multinomial 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) |
I retract my earlier comment. Apparently the 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) |
ModelCustom: provides the base class for building custom models.
ModelLinear: for creating a linear combination model.
ModelBinaryChoice: for creating a logistic regression model.
ModelMultinomialLogit: for creating a multinomial logistic regression model. This requires on the mlogit package for
formula
and structuring of prediction data.choice simulation (eventually will replace the MatchingMarket classes)(move to market simulation #85 )To construct choiceset for mlogit see https://stackoverflow.com/questions/51458223/multinomial-logit-estimation-on-a-subset-of-alternatives-in-r
To manually calculate the probs see https://monashdatafluency.github.io/r-linear/topics/linear_models.html
The text was updated successfully, but these errors were encountered: