<div dir="ltr">mlogit error was resolved. This was caused by an extra space in the <i style="font-weight:bold">cbc.logit</i> creation command. Please see below for the complete replication of the mlogit result from the book<div><br></div><div><div><div><span style="font-family:monospace"><span style="color:rgb(0,0,0)">> attrib <- list(seat = c ("6", "7", "8"),
</span><br>+ cargo = c ("2ft", "3ft"),
<br>+ eng = c ("gas", "hyb", "elec"),
<br>+ price = c ("30", "35", "40"))
<br>>  <br>> coef.names <- NULL
<br>> for (a in seq_along(attrib)) {
<br>+ coef.names <- c( coef.names,
<br>+ paste( names(attrib)[a], attrib [[a]][-1], sep=""))}
<br>>  <br>> mu <- c(-1, -1, 0.5, -1, -2, -1, -2)
<br>> names(mu) <- coef.names
<br>>  <br>> Sigma <- diag(c(0.3, 1, 0.1, 0.3, 1, 0.2, 0.3))
<br>> dimnames(Sigma) <- list( coef.names, coef.names)
<br>> Sigma["enghyb", "engelec"] <- Sigma["engelec", "enghyb"] <- 0.3
<br>>  <br>> set.seed (33040)
<br>> <a href="http://resp.id">resp.id</a> <- 1:200 # respondent ids
<br>> carpool <- sample( c("yes", "no") , size=length(<a href="http://resp.id">resp.id</a>), replace= TRUE, prob=c (0.3 , 0.7))
<br>>  <br>> library(MASS)
<br>> coefs <- mvrnorm(length(<a href="http://resp.id">resp.id</a>), mu=mu , Sigma=Sigma)
<br>> colnames(coefs) <- coef.names
<br>>  <br>> coefs[carpool=="yes", "seat8"] <- coefs[carpool =="yes", "seat8"] + 2
<br>> coefs[carpool=="yes", "seat7"] <- coefs[carpool =="yes", "seat7"] + 1.5
<br>>  <br>> head(cbind(carpool, coefs))
<br>     carpool seat7                seat8                cargo3ft             <br>[1,] "yes"   "0.913936862899084"  "-0.292773230321404" "0.767473398523344"  <br>[2,] "no"    "-1.39931169817203"  "1.04158623887883"   "0.438770878197896"  <br>[3,] "no"    "-1.00953999951008"  "-1.55093201191349"  "-0.145367894075681"
<br>[4,] "no"    "-0.935450676968176" "-1.04763010523802"  "0.220332364401268"  <br>[5,] "yes"   "2.05490896863986"   "1.18848367038507"   "0.346730508134781"  <br>[6,] "no"    "-1.04083358088935"  "-0.697832660267619" "0.81589633270858"   <br>     enghyb               engelec              price35              <br>[1,] "-0.322881231841994" "-0.939147236856879" "-0.476138719612586"
<br>[2,] "-0.287680321572104" "-1.79368088660817"  "-0.517278704139101"
<br>[3,] "-1.28920279748932"  "-0.670035286366727" "-1.21594489462233"  <br>[4,] "-1.14087047946977"  "-4.4140241275454"   "-1.70690855917779"  <br>[5,] "-0.415563593602318" "-1.91380887322729"  "-0.883177717691026"
<br>[6,] "-0.780967034425393" "-2.37780617993883"  "-0.412090470344695"
<br>     price40             <br>[1,] "-1.46449743710436"
<br>[2,] "-2.18225594163134"
<br>[3,] "-2.04155695067495"
<br>[4,] "-1.6669298694752"  <br>[5,] "-2.27403814243176"
<br>[6,] "-1.75491072255975"
<br>>  <br>> nques <- 15
<br>> nalt <- 3
<br>>  <br>> profiles <- expand.grid(attrib)
<br>> nrow(profiles)
<br>[1] 54
<br>>  <br>> profiles.coded <- model.matrix( ~ seat + cargo + eng + price, data=profiles)[ , -1]
<br>> head(profiles.coded)
<br>  seat7 seat8 cargo3ft enghyb engelec price35 price40
<br>1     0     0        0      0       0       0       0
<br>2     1     0        0      0       0       0       0
<br>3     0     1        0      0       0       0       0
<br>4     0     0        1      0       0       0       0
<br>5     1     0        1      0       0       0       0
<br>6     0     1        1      0       0       0       0
<br>>  <br>> cbc.df <- data.frame(NULL)
<br>> for (i in seq_along(<a href="http://resp.id">resp.id</a>)) {
<br>+ profiles.i <- sample(1:nrow(profiles), size=nques*nalt)
<br>+ utility <- profiles.coded[profiles.i, ] %*% coefs[i, ]
<br>+ wide.util <- matrix(data=utility, ncol=nalt, byrow=TRUE)
<br>+ probs <- exp(wide.util) / rowSums(exp(wide.util))
<br>+ choice <- apply(probs, 1, function(x) sample(1:nalt, size=1, prob=x))
<br>+ choice <- rep(choice, each=nalt)==rep(1:nalt, nques)
<br>+ conjoint.i <- data.frame(<a href="http://resp.id">resp.id</a>=rep(i, nques),
<br>+ ques = rep (1:nques, each=nalt),
<br>+ alt = rep (1:nalt, nques),
<br>+ carpool = rep(carpool[i], nques),
<br>+ profiles[profiles.i, ],
<br>+ choice = as.numeric(choice))
<br>+ cbc.df <- rbind(cbc.df, conjoint.i)}
<br>> # Tidy up , keeping cbc.df and attrib
<br>> rm(a, i, <a href="http://resp.id">resp.id</a>, carpool, mu, Sigma, coefs, coef.names ,
<br>+ conjoint.i, profiles, profiles.i, profiles.coded , utility,
<br>+ wide.util, probs, choice, nalt, nques)
<br>> cbc.mlogit <- mlogit.data(data=cbc.df, choice="choice", shape="long",
<br>+ varying =3:6, alt.levels= paste("pos" ,1:3), id.var="<a href="http://resp.id">resp.id</a>")
<br>>  <br>> m1 <- mlogit(choice ~ 0 + seat + cargo + eng + price, data = cbc.mlogit)
<br>> summary(m1)
<br>
<br>Call:
<br>mlogit(formula = choice ~ 0 + seat + cargo + eng + price, data = cbc.mlogit,  <br>    method = "nr")
<br>
<br>Frequencies of alternatives:
<br>  pos 1   pos 2   pos 3  <br>0.32700 0.33467 0.33833  <br>
<br>nr method
<br>5 iterations, 0h:0m:0s  <br>g'(-H)^-1g = 7.84E-05  <br>successive function values within tolerance limits  <br>
<br>Coefficients :
<br>          Estimate Std. Error  z-value  Pr(>|z|)     <br>seat7    -0.535280   0.062360  -8.5837 < 2.2e-16 ***
<br>seat8    -0.305840   0.061129  -5.0032 5.638e-07 ***
<br>cargo3ft  0.477449   0.050888   9.3824 < 2.2e-16 ***
<br>enghyb   -0.811282   0.060130 -13.4921 < 2.2e-16 ***
<br>engelec  -1.530762   0.067456 -22.6926 < 2.2e-16 ***
<br>price35  -0.913656   0.060601 -15.0765 < 2.2e-16 ***
<br>price40  -1.725851   0.069631 -24.7856 < 2.2e-16 ***
<br>---
<br>Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
<br>
<br>Log-Likelihood: -2581.6<br></span></div></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Oct 29, 2020 at 6:54 PM sree datta <<a href="mailto:sreedta8@gmail.com">sreedta8@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">In addition, I tried installing <b><i>mlogit version 0.4-2 </i></b>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.<div><br></div><div><span style="font-family:monospace"><span style="color:rgb(0,0,0)">> install.packages("~/Downloads/mlogit_<b>0.4-2</b>.tar.gz", repos=NULL, type="source")      </span><br>Installing package into ‘/home/sreezach/R/x86_64-pc-linux-gnu-library/4.0’
<br>(as ‘lib’ is unspecified)
<br>* installing *source* package ‘mlogit’ ...
<br>** package ‘mlogit’ successfully unpacked and MD5 sums checked
<br>** using staged installation
<br>** R
<br>** data
<br>** inst
<br>** byte-compile and prepare package for lazy loading
<br>** help
<br>*** installing help indices
<br>** building package indices
<br>** installing vignettes
<br>** testing if installed package can be loaded from temporary location
<br>** testing if installed package can be loaded from final location
<br>** testing if installed package keeps a record of temporary installation path
<br>* DONE (mlogit)
<br>> library(mlogit)
<br>> cbc.mlogit <- mlogit.data(data=cbc.df, choice="choice", shape="long",
<br>+ varying =3:6, alt.levels= paste("pos" ,1:3), id.var="<a href="http://resp.id" target="_blank">resp.id</a> ")
<br>Error in guess(varying) :  <br>  failed to guess time-varying variables from their names
<br>In addition: Warning message:
<br>In dfidx::dfidx(data = data, dfa$idx, drop.index = dfa$drop.index,  :
<br>  the levels shouldn't be provided with a data set in wide format<br>
<br></span></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Oct 29, 2020 at 6:47 PM sree datta <<a href="mailto:sreedta8@gmail.com" target="_blank">sreedta8@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi<div><br></div><div>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</div><div><br></div><div><span style="font-family:monospace"><span style="color:rgb(0,0,0)">> install.packages("~/Downloads/mlogit_<b>1.0-1</b>.tar.gz", repos=NULL, type="source")
</span><br>Installing package into ‘/home/datasci/R/x86_64-pc-linux-gnu-library/4.0’
<br>(as ‘lib’ is unspecified)
<br>* installing *source* package ‘mlogit’ ...
<br>** package ‘mlogit’ successfully unpacked and MD5 sums checked
<br>** using staged installation
<br>** R
<br>** data
<br>** inst
<br>** byte-compile and prepare package for lazy loading
<br>** help
<br>*** installing help indices
<br>** building package indices
<br>** installing vignettes
<br>** testing if installed package can be loaded from temporary location
<br>** testing if installed package can be loaded from final location
<br>** testing if installed package keeps a record of temporary installation path
<br>* DONE (mlogit)
<br>> library(mlogit)
<br>> cbc.mlogit <- mlogit.data(data=cbc.df, choice="choice", shape="long",
<br>+ varying =3:6, alt.levels= paste("pos" ,1:3), id.var="<a href="http://resp.id" target="_blank">resp.id</a> ")
<br>Error in guess(varying) :  <br>  failed to guess time-varying variables from their names
<br>In addition: Warning message:
<br>In dfidx::dfidx(data = data, dfa$idx, drop.index = dfa$drop.index,  :
<br>  the levels shouldn't be provided with a data set in wide format<br>
<br></span></div><div><span style="font-family:monospace">Next I tried version 1.0-0 of mlogit but no success still:</span></div><div><span style="font-family:monospace"><br></span></div><div><span style="font-family:monospace"><span style="color:rgb(0,0,0)">> install.packages("~/Downloads/mlogit_1.0-0.tar.gz", repos=NULL, type="source")  </span><br>Installing package into ‘/home/sreezach/R/x86_64-pc-linux-gnu-library/4.0’
<br>(as ‘lib’ is unspecified)
<br>* installing *source* package ‘mlogit’ ...
<br>** package ‘mlogit’ successfully unpacked and MD5 sums checked
<br>** using staged installation
<br>** R
<br>** data
<br>** inst
<br>** byte-compile and prepare package for lazy loading
<br>** help
<br>*** installing help indices
<br>** building package indices
<br>** installing vignettes
<br>** testing if installed package can be loaded from temporary location
<br>** testing if installed package can be loaded from final location
<br>** testing if installed package keeps a record of temporary installation path
<br>* DONE (mlogit)
<br>> library(mlogit)
<br>> cbc.mlogit <- mlogit.data(data=cbc.df, choice="choice", shape="long",
<br>+ varying =3:6, alt.levels= paste("pos" ,1:3), id.var="<a href="http://resp.id" target="_blank">resp.id</a> ")
<br>Error in guess(varying) :  <br>  failed to guess time-varying variables from their names
<br>In addition: Warning message:
<br>In dfidx::dfidx(data = data, dfa$idx, drop.index = dfa$drop.index,  :
<br>  the levels shouldn't be provided with a data set in wide format<br>
<br></span></div><div><span style="font-family:monospace">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</span></div><div><span style="font-family:monospace"><br></span></div><div><span style="font-family:monospace">Sree</span></div></div>
</blockquote></div>
</blockquote></div>