Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6809111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:02:49+00:00 2026-05-26T20:02:49+00:00

I am having some trouble with levels… Running the following: library(mlogit) panel.datasm = data.frame(

  • 0

I am having some trouble with levels… Running the following:

library(mlogit)

panel.datasm = data.frame(
    cbind( 
        round(runif(100, min=1, max=6)), 
        rep(1:20,each=5), runif(100, min=0, max=1), 
        runif(100, min=0, max=6), 
        runif(100, min=2, max=6) , 
        runif(100, min=0, max=1), 
        runif(100, min=0, max=6), 
        runif(100, min=2, max=6)  ))
names(panel.datasm) = c("choice", "id", "data_1991","data_1992",
  "data_1993", "data2_1991", "data2_1992","data2_1993") 


logit.data <- mlogit.data(panel.datasm, id = "id", choice = "choice", 
    varying= 3:5, shape = "wide", sep = "_")

Keep getting the error Error in Ops.factor(data[[choice]], alt) : level sets of factors are different

I have also tried assigning levels manually:

panel.datasm$id= factor(
    panel.datasm$id, 
    levels = sort(as.character(unique(panel.datasm$id)))  )

I have tried a number of things and can’t figure out what is going wrong. For comparison take a look at :

data("Electricity", package = "mlogit")
head(Electricity)
Electr <- mlogit.data(Electricity, id = "id", choice = "choice", 
    varying = 3:26, shape = "wide", sep = "")

Which as far as I can tell is identical to my data format. What’s going on here? I’m at my whit’s end.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:02:49+00:00Added an answer on May 26, 2026 at 8:02 pm

    I believe I have traced the problem. Your choice variables and your alternative variables should be the same.

    If you change your the first column of your data.frame to have values between 1991:1993 it will work.

    panel.datasm = data.frame(
        cbind( 
            sample(1991:1993, 100, replace=TRUE), 
            rep(1:20,each=5), runif(100, min=0, max=1), 
            runif(100, min=0, max=6), 
            runif(100, min=2, max=6) , 
            runif(100, min=0, max=1), 
            runif(100, min=0, max=6), 
            runif(100, min=2, max=6)  ))
    names(panel.datasm) = c("choice", "id", "data_1991","data_1992",
        "data_1993", "data2_1991", "data2_1992","data2_1993") 
    
    
    logit.data <- mlogit.data(panel.datasm, id = "id", choice = "choice", 
        varying= 3:5, shape = "wide", sep = "_") 
    

    The results:

    head(logit.data)
           choice id  alt       data     data2 chid
    1.1991  FALSE  1 1991 0.03540498 0.9726110    1
    1.1992  FALSE  1 1992 5.85285278 2.7973798    1
    1.1993   TRUE  1 1993 5.80795641 3.7360297    1
    2.1991   TRUE  1 1991 0.59255235 0.2564928    2
    2.1992  FALSE  1 1992 5.81443351 3.0820215    2
    2.1993  FALSE  1 1993 2.11699854 5.4161634    2
    

    If you now compare it with Electricity, the difference is obvious. Notice that the choices are 1:4, and each alternative ranges from 1 to 4.

    head(Electricity)
      choice id pf1 pf2 pf3 pf4 cl1 cl2 cl3 cl4 loc1 loc2 loc3 loc4 wk1 wk2 wk3 wk4
    1      4  1   7   9   0   0   5   1   0   5    0    1    0    0   1   0   0   1
    2      3  1   7   9   0   0   0   5   1   5    0    0    1    0   1   1   0   0
    3      4  1   9   7   0   0   5   1   0   0    0    0    0    1   0   1   1   0
    4      4  1   0   9   7   0   1   1   0   5    0    0    1    0   1   0   0   1
    5      1  1   0   9   0   7   0   1   0   5    1    0    0    0   0   1   0   1
    6      4  1   0   9   0   7   0   0   1   5    0    0    1    0   0   0   0   1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a big trouble on dealing with levels names of a data frame.
Having some trouble with what should be a very simple scenario. For example purposes,
Hey having some trouble trying to maintain transparency on a png when i create
Basically having some trouble with using Hover to hide or show an item. The
Been having some trouble implementing a single-column index in LaTeX. I've been employing the
Im having some trouble with an oracle database. Every time i try to connect,
I´m having some trouble retrieving a collection of strings in a projection: say that
Im having some trouble writing a getstring function, this is what I have so
I am having some trouble with the Google Maps API . I have an
I'm having some trouble importing and accessing a private key with the ASPNET user.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.