[R] length of variable in mlogit

Lee van Cleef l.van.cleef at gmx.net
Fri Aug 3 11:27:43 CEST 2012


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")

#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.



More information about the R-help mailing list