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,]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>-- View this message in context: http://n4.nabble.com/mlogit-tp1583698p1583698.html Sent from the R help mailing list archive at Nabble.com.
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
I have tried doing it whithout attaching the mydata file, and i still get the same output. When i create mldata using mlogit.data i do not generate the chid and alt columns, is this where the problem is, if so how do i fix this problem. Cheers for any help -- View this message in context: http://n4.nabble.com/mlogit-tp1583698p1583729.html Sent from the R help mailing list archive at Nabble.com.
In the code of the mlogit function, the call is saved under the name 'mldata' that you use to store the result of the mlogit.data function. When calling mlogit with 'mldata' as the data argument, there is a conflict between these two objects with the same name, one of class 'call' and one of class mlogit.data. As mentionned in previous messages on this list, the problem disappear if you use another name for the mlogit.data object. Regards Yves