[R] mlogit

David Winsemius dwinsemius at comcast.net
Sun Mar 7 18:30:42 CET 2010


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.

>    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



More information about the R-help mailing list