[R] mlogit

Peter Ehlers ehlers at ucalgary.ca
Sun Mar 7 18:50:04 CET 2010


On 2010-03-07 10:30, David Winsemius wrote:
>
> On Mar 7, 2010, at 12:06 PM, cmc wrote:
>
>>
>>
>> I am trying to follow this example for multinomial logistic regression
>>
>> http://www.ats.ucla.edu/stat/r/dae/mlogit.htm
>>
>> However, I cannot get it to work properly.
>>
>> This is the output I get, and I get an error when I try to use the mlogit
>> function. Any ideas as to why this happens?
>>
>>
>>
>>> mydata <- read.csv(url("http://www.ats.ucla.edu/stat/r/dae/mlogit.csv"))
>>> attach(mydata)
>>> names(mydata)
>> [1] "brand" "female" "age"
>>> library(mlogit)
>> Loading required package: Formula
>> Loading required package: statmod
>>>
>>> mydata[1:10,]
>> brand female age
>> 1 1 0 24
>> 2 1 0 26
>> 3 1 0 26
>> 4 1 1 27
>> 5 1 1 27
>> 6 3 1 27
>> 7 1 0 27
>> 8 1 0 27
>> 9 1 1 27
>> 10 1 0 27
>>> mydata$brand<-as.factor(mydata$brand)
>>> mldata<-mlogit.data(mydata, varying=NULL, choice="brand", shape="wide")
>>>
>>> mldata[1:10,]
>
> You do not get the same result as the example page, (while I do). You
> need to see if you have other objects with names that may be confusing
> the interpreter. I did not attach() mydata, which despite the UCLA's use
> of it is considered bad practice in R programming because of frequent
> obscure bugs that trip up newbies such as us. The errors do not occur
> when the next commands are run.

I do get the same error (I didn't use attach() either), but I
can avoid the error if I don't call the mlogit.data() output
'mldata'. This seems to be a hardcoded name in mlogit() which
appears to cause the problem. This works:

  mdata<-mlogit.data(mydata, varying=NULL, choice="brand", shape="wide")
  mlogit.model<- mlogit(brand~1|female+age, data = mdata, reflevel="1")

  -Peter Ehlers

>
>> brand female age
>> 1.1 TRUE 0 24
>> 1.2 FALSE 0 24
>> 1.3 FALSE 0 24
>> 2.1 TRUE 0 26
>> 2.2 FALSE 0 26
>> 2.3 FALSE 0 26
>> 3.1 TRUE 0 26
>> 3.2 FALSE 0 26
>> 3.3 FALSE 0 26
>> 4.1 TRUE 1 27
>>> mlogit.model<- mlogit(brand~1|female+age, data = mldata, reflevel="1")
>> Error in as.data.frame.default(data) :
>> cannot coerce class "call" into a data.frame
>>> summary(mlogit.model)
>> Error in summary(mlogit.model) : object 'mlogit.model' not found
>>>
>> --
>
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> 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.
>
>

-- 
Peter Ehlers
University of Calgary



More information about the R-help mailing list