[R] multinomial conditional logit models
Ott Toomet
otoomet at econ.dk
Thu Jan 30 10:24:03 CET 2003
Hi,
| I've been trying to implement an MCL model in R and have been mostly
| succesful. However I'm still stuck on a few points and I'm hoping
| someone can point out how to do fix them.
|
| To estimate an MCL model, the data must be restructured into a
| person-choice file.
|
| * Each case must be duplicated "ncat" times ("ncat" is the number of
| categories of the dependent variable)
| * Each case must be identified by a strata variable (id)
| * Each duplicate must be identified by a variable indexing the
| choices (newy)
| * A dichotomous variable must indicate which duplicate corresponds
| with the respondent's choice (didep)
|
| I've done this as follows:
|
| mclgen <- function (datamat, catvar) {
| ncat <- nlevels(catvar)
| id <- 1:length(catvar)
| datamat <- cbind(id,datamat)
| perschoice <-NULL
| for (i in 1:ncat) {
| perschoice<-rbind(perschoice,datamat)
| }
| perschoice <- perschoice[sort.list(perschoice$id),]
| perschoice$newy <- gl(ncat, 1, length=nrow(perschoice))
| dep <- parse(text=paste("perschoice$", substitute(catvar), sep=""))
| perschoice$depvar <- as.numeric(eval(dep) == perschoice$newy)
| perschoice
| }
|
| This works but I wonder if it's the most efficient method. I tried
| using "rep" rather than "rbind" in a loop but that replicated the
| dataset horizontally, not vertically. Is this the best solution?
Well, in general it is not efficient to add something to a matrix in a
cycle. You should in either make the datamat in required size and
fill the required parts of it, or in this case you may consider e.g.
> datamat <- matrix(1:4, 2, 2)
> matrix(t(datamat), 4, 2, byrow=T)
[,1] [,2]
[1,] 1 3
[2,] 2 4
[3,] 1 3
[4,] 2 4
| I also finally figure out how to include the argument "catvar" in a
| transformation involving another dataset but this solution seems very
| complicated (eval+parse+substitute). Is there a simpler solution?
Unfortunately, I was not able to understand quite what are you doing
here. Where is the dataset logan? I did not find it on
http://www.xs4all.nl/~jhckx. If I could get a simple example running,
I can try further.
best wishes,
Ott
More information about the R-help
mailing list