[R] Creation of mlogit models from text file
Bhargava Sana
Bhargava.Sana at rsginc.com
Mon Oct 17 23:28:15 CEST 2011
Yes, there seem to be a lot of elements in the mlogit model object and I don't have some of that information now since the models have been estimated in Biogeme. I had been trying some of the steps that you suggested (after I posted my question) and as you mentioned, realized that this is dangerous. I was also trying to see if I could re-estimate the models I have using mlogit with the data and model specifications. This way I don't have to worry about creating the required model objects myself and would get them as a result of estimation in R.
I ran into a little issue with mlogit. There are three kinds of explanatory variables in the MNL model specification - generic, individual (income,age), and alternative specific. In my current models, there are some individual variables that appear only in a subset of alternatives (since they are insignificant for the remaining alternatives). For some reason I have not been able to specify such a model in mlogit. I have gone through mformula documentation a couple of times to check if there was something I was missing but was not successful. So if I specify an individual variable, mlogit puts it in the utility equations of the 'all' the alternatives. I may post this under a separate thread after I try a little more tomorrow.
I think I am going abandon my current approach for the problem at hand and just try to calculate the utilities for the various choices based on a "specification dataframe" which looks like this:
ChoiceID, ChoiceDesc,Varname, Coeff
1,Car,one,0.0
1,Car,income,0.95
1,Bus,one,-0.5
1,Bus,income,-0.7
So I am thinking that I will make my own predict function which takes in two dataframes - one for data and one for model coefficients and outputs the probabilities of choosing the alternatives. I then plan to generate a random number and simulate the choices.
I used Biogeme earlier simply to learn how to use it and since my firm had excel macros to import the output in excel. Thanks a lot for all your help!
Bhargav
-----Original Message-----
From: R. Michael Weylandt [mailto:michael.weylandt at gmail.com]
Sent: Monday, October 17, 2011 4:58 PM
To: Bhargava Sana
Cc: r-help at r-project.org
Subject: Re: [R] Creation of mlogit models from text file
I'm not at all familiar with mlogit or Biogene so I can't really give
a definitive answer, but it seems that this would be a very dangerous
thing to try.
Looking at the mlogit manual, I see that, like most model objects in
R, mlogit has a variety of elements including many of those you
specifically seem to be avoiding: data, likelihood functions, etc. You
could perhaps make a trivial mlogit object and directly modify the
elements you mention, but then you'd want to check each of the
functions you run on your object to make sure they don't access the
unmodified list elements. A fair compromise is probably to run mlogit
on a small sample of your data (I suppose you are using this Biogene
program for speed reasons?) to create a "best-approximation object"
and then modify directly; that will at least reduce the chance of
something too bad happening.
I suppose this could be automated to save you a little trouble (just
create your own class whose methods consist of getting the mlogit
methods, deparsing them and making sure they don't look at an
unmodified element, and then passing to the mlogit method) but the
smarter path is probably to extract the code from those functions you
want to use for prediction and tweak them to use the object you read
in directly.
Someone may have already implemented a work around though if this is a
common problem.
Best of luck,
Michael
On Mon, Oct 17, 2011 at 12:01 PM, Bhargava Sana
<Bhargava.Sana at rsginc.com> wrote:
> Michael,
>
> Thanks for the quick response. I currently have a few models that have been estimated using Biogeme. I have attached a sample output file from the estimation.
>
> I am more concerned about creating a model object from scratch rather than the reading part. Let us just say if we have a list of variables, coefficients, and std errors, how can an R model object be created (so that it can be used for prediction on an R data frame)? I read in the mlogit documentation that a model is created using an estimation dataset and formula. I was wondering if there have been previous attempts to setup a model object without estimating it in R just based on some character variables.
>
> Bhargava
>
> -----Original Message-----
> From: R. Michael Weylandt [mailto:michael.weylandt at gmail.com]
> Sent: Monday, October 17, 2011 11:44 AM
> To: Bhargava Sana
> Cc: r-help at r-project.org
> Subject: Re: [R] Creation of mlogit models from text file
>
> You're going to have to say more about your text file if you want
> meaningful help: specifically, what is in the file: data, output from
> other software, etc?
>
> There are probably two questions to deal with here: reading in
> whatever is in the text file and mapping it to the correct R object
> and setting up the mlogit model. For the reading part, try
> read.table() or readLines depending on the formatting. For the mlogit,
> look at the mlogit package available on CRAN.
>
> Michael
>
> On Mon, Oct 17, 2011 at 10:52 AM, Bhargava Sana
> <Bhargava.Sana at rsginc.com> wrote:
>> Hello all,
>>
>> Has anyone tried to create an R mlogit model object from a text file? If yes, what would be the best way to do it? I already have models that have been estimated using other software and would like to use R to help me make predictions for new data.
>>
>> Thank you!
>>
>> Bhargava Sana
>>
>>
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
More information about the R-help
mailing list