[R] multinomial conditional logit models
John Hendrickx
john_hendrickx at yahoo.com
Thu Jan 30 16:47:33 CET 2003
--- Friedrich.Leisch at univie.ac.at wrote:
> >>>>> On Wed, 29 Jan 2003 11:57:20 -0800 (PST),
> >>>>> John Hendrickx (JH) wrote:
>
[snip]
> > 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'm not completely sure if I understand your target format
> completely,
> but it sounds like reshape() could do all you want:
>
>
> R> df3
> id age dose1 dose2 dose4
> 1 1 40 1 2 3
> 2 2 50 2 1 3
> 3 3 60 1 2 3
> 4 4 50 2 1 3
>
>
>
> R> reshape(df3, direction = "long", idvar=1,
> varying=list(dose=c("dose1", "dose2", "dose4")))
>
> id age time dose1
> 1.1 1 40 1 1
> 2.1 2 50 1 2
> 3.1 3 60 1 1
> 4.1 4 50 1 2
> 1.2 1 40 2 2
> 2.2 2 50 2 1
> 3.2 3 60 2 2
> 4.2 4 50 2 1
> 1.3 1 40 3 3
> 2.3 2 50 3 3
> 3.3 3 60 3 3
> 4.3 4 50 3 3
>
What I want can be done using "reshape" in conjunction with "rep". My
data structure isn't wide but can be made so using "rep". For
example:
> dataset=matrix(1:6,3,2)
> dset<-data.frame(dataset)
> dset
X1 X2
1 1 4
2 2 5
3 3 6
> c<-as.data.frame(rep(dset,2))
>
reshape(c,direction="long",varying=list(x1=c("X1","X1"),x2=c("X2","X2")))
time X1 X2 id
1.1 1 1 4 1
2.1 1 2 5 2
3.1 1 3 6 3
1.2 2 1 4 1
2.2 2 2 5 2
3.2 2 3 6 3
This is what I want to do when creating a person-choice file. The
only problem though is the last step, specifying a list of varying
variables. It should be possible to generate this from "names(dset)"
but I can't get it right. "as.list(rep(names(dset),2))" produces a
list with four elements rather than a list with 2 components each
containing two elements. "dim()" gives a matrix as a result. Could
someone show me how to create "list(x1=c("X1","X1"),x2=c("X2","X2"))"
from "names(dset)"?
Advance thanks for any help,
John Hendrickx
More information about the R-help
mailing list