length of variable in mlogit

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

length of variable in mlogit

Lee van Cleef
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: length of variable in mlogit

Ingmar Visser
hard to judge what the problem is without an example and without the actual
error message that mlogit (which one? there are several functions with that
name in various packages) gives you.
best, Ingmar

On Mon, Jul 30, 2012 at 11:30 AM, Lee van Cleef <[hidden email]> wrote:

> Dear all,
>
> does anybody have experience with building logits in Mlogit?
>
> I want to test the use of a couple of alternative specific variables with a
> generic regression coefficient. However, one of them simply does not work.
> R
> says the length of this variable is different.
>
> Problem: If I check the length of this special variable, I get a value,
> which also other variables have – and with those, the logit model works! I
> also checked the class of the explaining variables. The problem-laden
> variable is a matrix – but the model is working with other variables which
> are of the matrix and the data frame class. I also see no problems because
> of wide/long formats.
>
> Does anybody happen to know what else I can check?
>
> Any help would be much appreciated.
>
> Best regards!
>
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/length-of-variable-in-mlogit-tp4638323.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>
        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: length of variable in mlogit

Lee van Cleef
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: length of variable in mlogit

Ingmar Visser
Lee,
A working, or in this case failing, example is necessary to detect what the
problem is.
This would include a statement loading the package that you use to run your
mlogit models; the latter is necessary because there are various packages
with functions that are called mlgoit.
Best, Ingmar

On Wed, Aug 1, 2012 at 3:05 PM, Lee van Cleef <[hidden email]> wrote:

> Ingmar, many thanks. I get that one from R:
>
> Error in model.frame.default(terms(formula, lhs = lhs, rhs = rhs, data =
> data),  :
>   variable lengths differ (found for 'X')
>
> X is the variable I have used.
>
> Any comment would be much appreciated.
>
> Best regards!
>
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/length-of-variable-in-mlogit-tp4638323p4638667.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: length of variable in mlogit

Lee van Cleef
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: length of variable in mlogit

Michael Weylandt
On Fri, Aug 3, 2012 at 4:27 AM, Lee van Cleef <[hidden email]> wrote:

> Ingmar,
>
> many thanks for your answer.
>
> I give you a smaller version of my program with the isolated "strange"
> variable, which I used when trying to elaborate the problem .
>
> [Start of R-Editor quote]
> library(foreign)
> library(gdata)
> library(gtools)
> library(gmodels)
> library(gplots)
> library(xtable)
> library(mlogit)
> library(survival)
>
> #First I import the data, a survey in the wide format.
> masterdata <- read.dta("E:/Masterdata.dta")

I'm afraid this line makes it hard for us to reproduce this otherwise
quite helpful example: can you give us dput(head(masterdata, 20))
which will give a plain text representation of the same? Don't worry
if you can't understand what shows up -- it's super helpful for us.

Best,
Michael

>
> #Then I define the number of cases which is the dimension of rows of the
> imported dataset.
>
> cases <- dim(masterdata)[1]
>
> # The conditional Logit-Model must have the form Y = aX, with Y as the
> choice variable
> # and X as the explaining one.
>
> # These are the headings for the explaining variable.
> Numbers <- c("2", "3", "4", "5", "6", "7")
>
> # I define the matrix for the explaining variable. ncol corresponds to the
> number of choices; cases is the number of people surveyed.
> X <- matrix(0, ncol=6, nrow=cases)
>
> # I define the choice variable. First, the answers become numeric values,
> the I define the n.a.'s.
> Y <- as.numeric(masterdata[,"V3D"])
> Y[Y == 1 | Y ==8 | Y == 9 | Y == 10 | Y == 11] <- NA
> Y <- as.factor(Y - 1)
>
> #I import the answers from the survey in my matrix for the explaining
> variable.
> for (i in 1:5){X[,i][as.numeric(factor(masterdata$VS))==i] <- 1}
> colnames (X) <- paste("X", Numbers, sep=".")
> # I put my data set together and delete the NA options.
> masterdata.wide <- cbind (Y, X)
> masterdata.wide <- na.omit(masterdata.wide)
> head(masterdata.wide)
>
> masterdata.long <- mlogit.data(masterdata.wide, varying = c(2:7), shape =
> "wide", choice = "Y")
>
> [End of R-Editor quote]
>
> The first problem arises before the Condlog-order. R does not transform wide
> into long and says: Error in data[[choice]] : subscript out of bounds
>
> In the extended version, transforming from wide to long is no problem, no
> idea why. But then, R says after Condlog: Error in
> model.frame.default(terms(formula, lhs = lhs, rhs = rhs, data = data),  :
> variable lengths differ (found for 'X').
>
> Other variables (alternative specific or with a generic regression
> coefficient) apart from X work. I checked the variable lengths, but the
> complete Logit-Model is working without problems for other variables with
> the same lengths.
>
> Which options do I have to address the problems with X? I did not find
> anything via the Str-Option on first sight.
>
> Any comment where and how I can look for would really be appreciated.
>
> Best regards!
>
>
>
>
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/length-of-variable-in-mlogit-tp4638323p4639025.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: length of variable in mlogit

Lee van Cleef
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: length of variable in mlogit

Michael Weylandt
On Fri, Aug 3, 2012 at 11:49 AM, Lee van Cleef <[hidden email]> wrote:
> Hi Michael,
>
> many thanks for your comment.
>
> Below the original data as imported from Stata format.

Hi Lee,

I apologize for being intransigent (well, no -- I actually don't) but
could you provide your data using dput() as asked?

Best,
Michael

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: length of variable in mlogit

Lee van Cleef
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: length of variable in mlogit

Michael Weylandt
Hi Lee,

I've finally had time to look at this:

If you look at

?mlogit.data

you'll see that choice must be "the variable indicating the choice
made: it can be either a logical vector, a numerical vector with 0
where the alternative is not chosen, a factor with level 'yes' when
the alternative is chosen."

For the data and script you provided me, we have a few problems:
firstly, masterdata.wide should be a data.frame, not a matrix -- this
can be rectified by wrapping it in as.data.frame. Once you do that,
take a look at the column "Y" which you supply for the choice
variable. It is a numerical vector, but it has no 0's so it doesn't
fit the requested input format.

For anyone else who wants to look at this:

dput(masterdata.wide)

structure(c(1, 2, 1, 4, 1, 5, 4, 1, 4, 3, 1, 2, 4, 0, 1, 0, 0,
0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(13L, 7L), .Dimnames = list(
    NULL, c("Y", "X.2", "X.3", "X.4", "X.5", "X.6", "X.7")), na.action
= structure(c(1,
3, 7, 8, 12, 14, 17), class = "omit"))

Hope that helps,
Michael

On Fri, Aug 10, 2012 at 2:31 AM, Lee van Cleef <[hidden email]> wrote:

> Hi Michael,
>
> I have sent youi the data.
>
> Best,
> Lee
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/length-of-variable-in-mlogit-tp4638323p4639873.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: length of variable in mlogit

Lee van Cleef
CONTENTS DELETED
The author has deleted this message.