gpt4 book ai didi

r - 为 mlogit 格式化数据

转载 作者:行者123 更新时间:2023-12-05 08:09:56 25 4
gpt4 key购买 nike

我在通过 mlogit 整理数据集以进行多项式 logit 分析时非常痛苦。我的数据集可从 url 获得在下面的代码中。

我收到以下错误:

Error in row.names<-.data.frame(*tmp*, value = c("1.Accessible", "1.Accessible", : duplicate 'row.names' are not allowed

我在别处检查过,似乎出现了这个问题。我试过玩 alt.levels而不是 alt.var争论,那是行不通的。

#Loadpackages 
library(RCurl)
library(mlogit)
library(tidyr)
library(dplyr)
#URL where data is stored
dat.url<- 'https://raw.githubusercontent.com/sjkiss/Survey/master/mlogit.out.csv'
#Get data
dat<-read.csv(dat.url)
#Complete cases only as it seems mlogit cannot handle missing values or tied data which in this case you might get because of median imputation
dat<-dat[complete.cases(dat),]
#Tidy data to get it into long format
dat.out<-dat %>%
gather(Open, Rank, -c(1,9:12))
#Try to replicate code on pp.26-27 of http://cran.r- project.org/web/packages/mlogit/vignettes/mlogit.pdf
mlogit.out<-mlogit.data(dat.out, shape='long',alt.var='Open',choice='Rank', id.var='X',ranked=TRUE)
#Try this option as per a discussion on stackexchange
mlogit.out<-mlogit.data(dat.out, shape='long',alt.levels='Open',choice='Rank', id.var='X',ranked=TRUE)

最佳答案

我的建议是您尝试 multinom () 函数在 nnet 包中。它不需要 mlogit 或 mnlogit 的特殊格式。

library(RCurl)
library(nnet)

Data<-getURL("https://raw.githubusercontent.com/sjkiss/Survey/master/mlogit.out.csv")
Data<-read.csv(text=Data,header=T)
Data<-na.omit(Data) # Get rid of NA's
Data<-as.data.frame(Data)
# relevel the dependent variable (must be a factor)
Data$Job<-factor(Data$Job)
# Using "Online Blogger" as the reference, substitute with your choice
Data$Job<-relevel(Data$Job,ref="Online blogger")
# Run the multinomial logistic regression
# (seems like an awful lot of variables btw)
Data<-multinom(formula=Job~Accessible+Information+Responsive+Debate+Officials+Social+Trade.Offs+economic+gender+age,data=Data)

关于r - 为 mlogit 格式化数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30852155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com