[R-marketing-bugs] Error with mlogit

sree datta sreedta8 at gmail.com
Fri Oct 30 00:54:59 CET 2020


mlogit error was resolved. This was caused by an extra space in the
*cbc.logit* creation command. Please see below for the complete replication
of the mlogit result from the book

> attrib <- list(seat = c ("6", "7", "8"),
+ cargo = c ("2ft", "3ft"),
+ eng = c ("gas", "hyb", "elec"),
+ price = c ("30", "35", "40"))
>
> coef.names <- NULL
> for (a in seq_along(attrib)) {
+ coef.names <- c( coef.names,
+ paste( names(attrib)[a], attrib [[a]][-1], sep=""))}
>
> mu <- c(-1, -1, 0.5, -1, -2, -1, -2)
> names(mu) <- coef.names
>
> Sigma <- diag(c(0.3, 1, 0.1, 0.3, 1, 0.2, 0.3))
> dimnames(Sigma) <- list( coef.names, coef.names)
> Sigma["enghyb", "engelec"] <- Sigma["engelec", "enghyb"] <- 0.3
>
> set.seed (33040)
> resp.id <- 1:200 # respondent ids
> carpool <- sample( c("yes", "no") , size=length(resp.id), replace= TRUE,
prob=c (0.3 , 0.7))
>
> library(MASS)
> coefs <- mvrnorm(length(resp.id), mu=mu , Sigma=Sigma)
> colnames(coefs) <- coef.names
>
> coefs[carpool=="yes", "seat8"] <- coefs[carpool =="yes", "seat8"] + 2
> coefs[carpool=="yes", "seat7"] <- coefs[carpool =="yes", "seat7"] + 1.5
>
> head(cbind(carpool, coefs))
    carpool seat7                seat8                cargo3ft
[1,] "yes"   "0.913936862899084"  "-0.292773230321404" "0.767473398523344"
[2,] "no"    "-1.39931169817203"  "1.04158623887883"   "0.438770878197896"
[3,] "no"    "-1.00953999951008"  "-1.55093201191349"  "-0.145367894075681"
[4,] "no"    "-0.935450676968176" "-1.04763010523802"  "0.220332364401268"
[5,] "yes"   "2.05490896863986"   "1.18848367038507"   "0.346730508134781"
[6,] "no"    "-1.04083358088935"  "-0.697832660267619" "0.81589633270858"
    enghyb               engelec              price35
[1,] "-0.322881231841994" "-0.939147236856879" "-0.476138719612586"
[2,] "-0.287680321572104" "-1.79368088660817"  "-0.517278704139101"
[3,] "-1.28920279748932"  "-0.670035286366727" "-1.21594489462233"
[4,] "-1.14087047946977"  "-4.4140241275454"   "-1.70690855917779"
[5,] "-0.415563593602318" "-1.91380887322729"  "-0.883177717691026"
[6,] "-0.780967034425393" "-2.37780617993883"  "-0.412090470344695"
    price40
[1,] "-1.46449743710436"
[2,] "-2.18225594163134"
[3,] "-2.04155695067495"
[4,] "-1.6669298694752"
[5,] "-2.27403814243176"
[6,] "-1.75491072255975"
>
> nques <- 15
> nalt <- 3
>
> profiles <- expand.grid(attrib)
> nrow(profiles)
[1] 54
>
> profiles.coded <- model.matrix( ~ seat + cargo + eng + price,
data=profiles)[ , -1]
> head(profiles.coded)
 seat7 seat8 cargo3ft enghyb engelec price35 price40
1     0     0        0      0       0       0       0
2     1     0        0      0       0       0       0
3     0     1        0      0       0       0       0
4     0     0        1      0       0       0       0
5     1     0        1      0       0       0       0
6     0     1        1      0       0       0       0
>
> cbc.df <- data.frame(NULL)
> for (i in seq_along(resp.id)) {
+ profiles.i <- sample(1:nrow(profiles), size=nques*nalt)
+ utility <- profiles.coded[profiles.i, ] %*% coefs[i, ]
+ wide.util <- matrix(data=utility, ncol=nalt, byrow=TRUE)
+ probs <- exp(wide.util) / rowSums(exp(wide.util))
+ choice <- apply(probs, 1, function(x) sample(1:nalt, size=1, prob=x))
+ choice <- rep(choice, each=nalt)==rep(1:nalt, nques)
+ conjoint.i <- data.frame(resp.id=rep(i, nques),
+ ques = rep (1:nques, each=nalt),
+ alt = rep (1:nalt, nques),
+ carpool = rep(carpool[i], nques),
+ profiles[profiles.i, ],
+ choice = as.numeric(choice))
+ cbc.df <- rbind(cbc.df, conjoint.i)}
> # Tidy up , keeping cbc.df and attrib
> rm(a, i, resp.id, carpool, mu, Sigma, coefs, coef.names ,
+ conjoint.i, profiles, profiles.i, profiles.coded , utility,
+ wide.util, probs, choice, nalt, nques)
> cbc.mlogit <- mlogit.data(data=cbc.df, choice="choice", shape="long",
+ varying =3:6, alt.levels= paste("pos" ,1:3), id.var="resp.id")
>
> m1 <- mlogit(choice ~ 0 + seat + cargo + eng + price, data = cbc.mlogit)
> summary(m1)

Call:
mlogit(formula = choice ~ 0 + seat + cargo + eng + price, data =
cbc.mlogit,
   method = "nr")

Frequencies of alternatives:
 pos 1   pos 2   pos 3
0.32700 0.33467 0.33833

nr method
5 iterations, 0h:0m:0s
g'(-H)^-1g = 7.84E-05
successive function values within tolerance limits

Coefficients :
         Estimate Std. Error  z-value  Pr(>|z|)
seat7    -0.535280   0.062360  -8.5837 < 2.2e-16 ***
seat8    -0.305840   0.061129  -5.0032 5.638e-07 ***
cargo3ft  0.477449   0.050888   9.3824 < 2.2e-16 ***
enghyb   -0.811282   0.060130 -13.4921 < 2.2e-16 ***
engelec  -1.530762   0.067456 -22.6926 < 2.2e-16 ***
price35  -0.913656   0.060601 -15.0765 < 2.2e-16 ***
price40  -1.725851   0.069631 -24.7856 < 2.2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Log-Likelihood: -2581.6

On Thu, Oct 29, 2020 at 6:54 PM sree datta <sreedta8 at gmail.com> wrote:

> In addition, I tried installing *mlogit version 0.4-2 *and the error
> persists. I'm beginning to think the error is due to another library mlogit
> depends on unless all the older versions and their requirements /
> dependencies have changed.
>
> > install.packages("~/Downloads/mlogit_*0.4-2*.tar.gz", repos=NULL,
> type="source")
> Installing package into ‘/home/sreezach/R/x86_64-pc-linux-gnu-library/4.0’
> (as ‘lib’ is unspecified)
> * installing *source* package ‘mlogit’ ...
> ** package ‘mlogit’ successfully unpacked and MD5 sums checked
> ** using staged installation
> ** R
> ** data
> ** inst
> ** byte-compile and prepare package for lazy loading
> ** help
> *** installing help indices
> ** building package indices
> ** installing vignettes
> ** testing if installed package can be loaded from temporary location
> ** testing if installed package can be loaded from final location
> ** testing if installed package keeps a record of temporary installation
> path
> * DONE (mlogit)
> > library(mlogit)
> > cbc.mlogit <- mlogit.data(data=cbc.df, choice="choice", shape="long",
> + varying =3:6, alt.levels= paste("pos" ,1:3), id.var="resp.id ")
> Error in guess(varying) :
>  failed to guess time-varying variables from their names
> In addition: Warning message:
> In dfidx::dfidx(data = data, dfa$idx, drop.index = dfa$drop.index,  :
>  the levels shouldn't be provided with a data set in wide format
>
>
> On Thu, Oct 29, 2020 at 6:47 PM sree datta <sreedta8 at gmail.com> wrote:
>
>> Hi
>>
>> I was training some colleagues on use of mlogit (version 1.0-1 as
>> specified in the errata) for cbc using R and your example data set cbc.df
>> in the book. It still failed. Here is the output. I installed the package
>> from a local file
>>
>> > install.packages("~/Downloads/mlogit_*1.0-1*.tar.gz", repos=NULL,
>> type="source")
>> Installing package into ‘/home/datasci/R/x86_64-pc-linux-gnu-library/4.0’
>> (as ‘lib’ is unspecified)
>> * installing *source* package ‘mlogit’ ...
>> ** package ‘mlogit’ successfully unpacked and MD5 sums checked
>> ** using staged installation
>> ** R
>> ** data
>> ** inst
>> ** byte-compile and prepare package for lazy loading
>> ** help
>> *** installing help indices
>> ** building package indices
>> ** installing vignettes
>> ** testing if installed package can be loaded from temporary location
>> ** testing if installed package can be loaded from final location
>> ** testing if installed package keeps a record of temporary installation
>> path
>> * DONE (mlogit)
>> > library(mlogit)
>> > cbc.mlogit <- mlogit.data(data=cbc.df, choice="choice", shape="long",
>> + varying =3:6, alt.levels= paste("pos" ,1:3), id.var="resp.id ")
>> Error in guess(varying) :
>>  failed to guess time-varying variables from their names
>> In addition: Warning message:
>> In dfidx::dfidx(data = data, dfa$idx, drop.index = dfa$drop.index,  :
>>  the levels shouldn't be provided with a data set in wide format
>>
>> Next I tried version 1.0-0 of mlogit but no success still:
>>
>> > install.packages("~/Downloads/mlogit_1.0-0.tar.gz", repos=NULL,
>> type="source")
>> Installing package into
>> ‘/home/sreezach/R/x86_64-pc-linux-gnu-library/4.0’
>> (as ‘lib’ is unspecified)
>> * installing *source* package ‘mlogit’ ...
>> ** package ‘mlogit’ successfully unpacked and MD5 sums checked
>> ** using staged installation
>> ** R
>> ** data
>> ** inst
>> ** byte-compile and prepare package for lazy loading
>> ** help
>> *** installing help indices
>> ** building package indices
>> ** installing vignettes
>> ** testing if installed package can be loaded from temporary location
>> ** testing if installed package can be loaded from final location
>> ** testing if installed package keeps a record of temporary installation
>> path
>> * DONE (mlogit)
>> > library(mlogit)
>> > cbc.mlogit <- mlogit.data(data=cbc.df, choice="choice", shape="long",
>> + varying =3:6, alt.levels= paste("pos" ,1:3), id.var="resp.id ")
>> Error in guess(varying) :
>>  failed to guess time-varying variables from their names
>> In addition: Warning message:
>> In dfidx::dfidx(data = data, dfa$idx, drop.index = dfa$drop.index,  :
>>  the levels shouldn't be provided with a data set in wide format
>>
>> Any help/feedback would be great. Thanks for an excellent book - my top
>> go-to reference book for my day-to-day R analytics and one I recommend the
>> most for a lot of colleagues and new R learners in analytics
>>
>> Sree
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/r-marketing-bugs/attachments/20201029/728a1cd6/attachment-0001.html>


More information about the R-marketing-bugs mailing list