[R] Mlogit: Error in model.frame.default/ Error in solve.default
Dear R users, 1) My problem in short: Mlogit cannot calculate certain conditional models. 2) My database: The target was a logistical regression analysis and a probability function which should include generic coefficients and alternative-specific ones. The database was a survey, the dependent variable consisted of six possible choices. I worked with two databases: First, the true survey data (out of the six choices, one is quite dominant with a share of around 40%, two have around 20% each, and the remaining are marginal). Second, the survey with randomized values for both choice and independent variables to avoid the possible problems with the less uniform distribution of the choice variable in the true set. Both datasets include 725 individuals with twelve variables due to receive a generic coefficient and 25 individual ones which should have an alternative specific coefficient. I have already to note that the problems above emerged when estimating with only one independent variable. Before the estimate, both datasets were brought into the required long format. The original data in the long format looked ok. 3) The estimates and the issues. I started in both datasets with an estimate with only one variable with a generic coefficient. I used the following order: mlogit(choice ~ variable, data = survey_long) R’s comment was “Error in solve.default(H, g[!fixed]) : system is computationally singular: reciprocal condition number = ...”. I was able to get an estimate for such a comparably simple model when using mlogit(choice ~ variable | 0, data = survey_long). However, the output did not contain estimates on the alternative specific coefficents for the intercept. I continued by gradually adding variables with generic coefficients. If it was more than five of them, R’s message was “Error in solve.default(H, g[!fixed]) : system is computationally singular: reciprocal condition number = ...”. One of the variables had the problem “Error in model.frame.default(terms(formula, lhs = lhs, rhs = rhs, data = data), : variable lengths differ (found for 'C15_CC_Dist')”. I compared the length for all the variables, and it was all the same. Another thing: It was impossible to add variables with an alternative specific coefficient: “Error in solve.default(H, g[!fixed]) :system is computationally singular: reciprocal condition number = ...”. These statements appeared both when using the true survey as well as when using the randomized data. Does anyone have any comment or help on that? What can I do? I can send the datasets if required. Any comment would be helpful. Best regards, LvC -- View this message in context: http://r.789695.n4.nabble.com/Mlogit-Error-in-model-frame-default-Error-in-solve-default-tp4701843.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
[R] M logit/problems
Dear R users, 1) My problem in short: Mlogit cannot calculate certain conditional models. 2) My database: The target was a logistical regression analysis and a probability function which should include generic coefficients and alternative-specific ones. The database was a survey, the dependent variable consisted of six possible choices. I worked with two databases: First, the true survey data (out of the six choices, one is quite dominant with a share of around 40%, two have around 20% each, and the remaining are marginal). Second, the survey with randomized values for both choice and independent variables to avoid the possible problems with the less uniform distribution of the choice variable in the true set. Both datasets include 725 individuals with twelve variables due to receive a generic coefficient and 25 individual ones which should have an alternative specific coefficient. I have already to note that the problems above emerged when estimating with only one independent variable. Before the estimate, both datasets were brought into the required long format. The original data in the long format looked ok. 3) The estimates and the issues. I started in both datasets with an estimate with only one variable with a generic coefficient. I used the following order: mlogit(choice ~ variable, data = survey_long) R’s comment was “Error in solve.default(H, g[!fixed]) : system is computationally singular: reciprocal condition number = ...”. I was able to get an estimate for such a comparably simple model when using mlogit(choice ~ variable | 0, data = survey_long). However, the output did not contain estimates on the alternative specific coefficents for the intercept. I continued by gradually adding variables with generic coefficients. If it was more than five of them, R’s message was “Error in solve.default(H, g[!fixed]) : system is computationally singular: reciprocal condition number = ...”. One of the variables had the problem “Error in model.frame.default(terms(formula, lhs = lhs, rhs = rhs, data = data), : variable lengths differ (found for 'C15_CC_Dist')”. I compared the length for all the variables, and it was all the same. Another thing: It was impossible to add variables with an alternative specific coefficient: “Error in solve.default(H, g[!fixed]) :system is computationally singular: reciprocal condition number = ...”. These statements appeared both when using the true survey as well as when using the randomized data. Does anyone have any comment or help on that? What can I do? I can send the datasets if required. Any comment would be helpful. Best regards, LvC -- View this message in context: http://r.789695.n4.nabble.com/M-logit-problems-tp4701596.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] length of variable in mlogit
Hi Michael, many thanks for your time and the hint. This is much appreciated. Indeed, I managed it to get part of the dataset from which I want to develop my final logit model in the correct long format with true/false standing in the column for the choice variable,. But the problem with the variable I want to define with an alternative specific coefficient (=P_id) still persists. If doing the mlogit procedure and defining the table for this variable as 1) a data frame I get the comment [Start of R quote] Error in model.frame.default(terms(formula, lhs = lhs, rhs = rhs, data = data), : invalid type (list) for variable 'P_id' [End of R quote] Btw, length of the variable/data frame P_id is 6, the length of the choice variable is 1,406 and the length of a variable with a generic coefficient (and which is working fine) is 8,436. 2) as a matrix, I get the comment [Start of R quote] Error in model.frame.default(terms(formula, lhs = lhs, rhs = rhs, data = data), : variable lengths differ (found for 'P_id') [End of R quote] Btw, length of the variable/matrix P_id is 8,436, the length of the choice variable is 1,406 and the length of a variable with a generic coefficient (and which is working fine) is 8,436. Any comment or hint (also on literature, in case my mistakes are too simple and obvious) would be helpful. Best regards, Lee -- View this message in context: http://r.789695.n4.nabble.com/length-of-variable-in-mlogit-tp4638323p4640250.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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.
Re: [R] length of variable in mlogit
Hi Michael, many thanks for the useful hints which gave me some deeper knowledge of R. It is definitely much appreciated. I think I have found the mistake - the problems did not arise from variable definitions etc. It was an intellectual mistake. The cause for the problems was that I had to divide a variable into single alternatives. The problematic variable referred to Party Identity. The whole matrix with the single parties as columns was of course not suitable for a variable with an alternative specific coefficient. Instead, each column/party was a single individual specific variable with an alternative specific coefficient. Now it works. Best wishes! -- View this message in context: http://r.789695.n4.nabble.com/length-of-variable-in-mlogit-tp4638323p4640266.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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.
Re: [R] length of variable in mlogit
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. __ R-help@r-project.org 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.
Re: [R] length of variable in mlogit
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. __ R-help@r-project.org 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.
Re: [R] length of variable in mlogit
Hi Michael, many thanks for your comment. Below the original data as imported from Stata format. I deleted some columns with explaining variables because they were unnecessary and working; the model has several explaining variables with generic regression coefficients. pid Choice V7.Ch 1 V7.Ch 2 V7.Ch 3 V7.Ch 4 V7.Ch 5 V7.Ch 6 V4 VS 1 Choice 14 4 3 1 3 2 Choice 1Choice 1 2 Choice 13 4 -3 -5 3 -5 Choice 1Choice 1 3 n.a.n.a.n.a.5 4 -5 -5 n.a.0 4 Choice 2-3 -4 1 2 -3 -2 Choice 2Choice 2 5 Choice 13 3 2 -1 1 1 Choice 1Choice 1 6 Choice 41 -3 -3 3 1 0 Choice 4Choice 4 7 n.a.-3 -3 0 -2 -5 -5 n.a.0 8 n.a.n.a.n.a.n.a.n.a.n.a.n.a.Choice 60 9 Choice 13 2 2 -1 2 3 Choice 30 10 n.a.-5 -5 -5 -5 -5 -5 Choice 60 11 Choice 22 -2 4 2 -5 n.a.Choice 2Choice 2 12 n.a.2 2 2 0 3 2 Choice 30 13 Choice 15 3 -5 -5 3 3 Choice 1Choice 1 14 n.a.1 1 -2 -2 3 3 n.a.0 15 n.a.1 -1 0 -1 2 3 Choice 30 16 Choice 33 2 2 1 3 3 n.a. Choice 3 17 Choice 32 2 2 2 4 5 n.a.0 18 Choice 15 3 0 0 2 2 Choice 1Choice 1 19 Choice 2-3 -4 0 2 -5 -5 Choice 4Choice 2 20 Choice 43 2 2 1 1 0 Choice 4Choice 5 V7 and the other variables with generic variables work perfect. So does V4 which can be used with both a generic and a alternative specific regression coefficient. I only have problems with VS. There, R tells me that the length of the variable causes problems (“Error in model.frame.default(terms(formula, lhs = lhs, rhs = rhs, data = data), : variable lengths differ (found for 'P_id')). However, comparing lengths and dimensions of V4 and VS, I see no difference! Both original data are transformed into numeric values (as.numeric) (all values become to either zero or one, depending on the alternative) and then put into (previously defined) matrixes of the same dimensions. There were no differences visible when checking the transformed variables with STR or CLASS. What else can I check? As stated above, I tried to isolate the problem-laden variable VS to set up a single conditional logit model, but I did not manage it to make it to the long format (R comment was: Error in data[[choice]] : subscript out of bounds). No idea whether there is a connection between the two problems. Any comment would be much appreciated. Best regards and a nice weekend! -- View this message in context: http://r.789695.n4.nabble.com/length-of-variable-in-mlogit-tp4638323p4639069.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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.
Re: [R] length of variable in mlogit
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. __ R-help@r-project.org 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.
[R] length of variable in mlogit
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. __ R-help@r-project.org 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.
[R] Marginal effects in mlogit
Dear all, does anybody have experience with the calculation of marginal effects (“effects”) in Mlogit (see Croissant, Package ‘mlogit’, p.8)? 1) Is there a good qualitative explanation available for the listed options for the argument “type” (“aa”, …)? When do I have to choose aa, ar…? And when can I leave it out? 2) The table in the output – how is it to read? Does it refer to relative changes (percentage) in the probability or absolute values (percentage points)? Any help including hints for literature is welcome. Best regards! -- View this message in context: http://r.789695.n4.nabble.com/Marginal-effects-in-mlogit-tp4638322.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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.