You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

mlogit是否要求特定行名?运行代码遇行名长度错误求助

Fixing the mlogit Row Name Error with Your R Data

Hey there, let's break down what's going wrong here and how to fix it:

First, the core issue is that mlogit doesn't work with standard wide-format data like regular linear regression does. It expects a choice-specific data structure (either long format or explicitly indexed), and your current setup plus the missing value in row 5 are causing the row name mismatch error.

Here's a step-by-step solution:

1. Remove Rows with Missing Response Values

Your 5th row has NA for correct_hyp_nomlogit can't handle missing values in the response variable, so we'll drop this row first:

library(dplyr)
clean_data <- my_data %>% filter(!is.na(correct_hyp_no))

2. Convert Data to mlogit-Compatible Format

Your data is in wide format (one row per sample, columns for each option's attributes). mlogit needs long format (one row per option per sample) or to explicitly define the individual-option index. Let's use mlogit.data to handle this conversion smoothly:

library(mlogit)
# Convert wide data to mlogit's required structure
mlogit_data <- mlogit.data(clean_data, 
                           shape = "wide", 
                           choice = "correct_hyp_no", 
                           alt.levels = c("h0", "h1", "h2", "h3", "h4"),
                           id.var = "sampleno")
  • shape = "wide" tells the function your data is organized with one row per individual
  • choice = "correct_hyp_no" specifies which column holds the chosen option
  • alt.levels lists all the option columns (h0-h4) that represent available choices
  • id.var marks the column that identifies each unique sample

3. Run the mlogit Model Correctly

Now you can fit the model with the properly formatted data:

model <- mlogit(correct_hyp_no ~ h0 + h1 + h2 + h3 + h4, data = mlogit_data)

Quick Recap of Why Your Original Code Failed

  • You passed a standard wide-format tibble directly to mlogit, which expects a structure that explicitly links each individual to their available options
  • The NA in row 5 disrupted the automatic row name generation process that mlogit uses when parsing input data

内容的提问来源于stack exchange,提问作者Mohan

火山引擎 最新活动