使用R语言的mlogit在`row.names<-.. data.frame中出错 [英] Error in `row.names<-.data.frame using mlogit in R language

查看:478
本文介绍了使用R语言的mlogit在`row.names<-.. data.frame中出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我执行多项式线性回归的步骤.

Here are the steps I'm following to do a Multinomial Linear Regression.

> z<-read.table("2008 Racedata.txt", header=TRUE, sep="\t", row.names=NULL)

> head(z)

     datekey raceno horseno place winner draw winodds log_odds jwt  hwt
1 2008091501      1       8     1      1    2    12.0 2.484907 128 1170
2 2008091501      1      11     2      0    3     8.6 2.151762 123 1135
3 2008091501      1       6     3      0    5     7.0 1.945910 127 1114
4 2008091501      1      12     4      0   10    23.0 3.135494 123 1018
5 2008091501      1      14     5      0    4    11.0 2.397895 113 1027
6 2008091501      1       5     6      0   14    50.0 3.912023 131  972

> x<-mlogit.data(z,choice="winner",shape="long",id.var="datekey",alt.var="horseno")

Error in `row.names<-.data.frame`(`*tmp*`, value = c("1.8", "1.11", "1.6",  : 
  duplicate 'row.names' are not allowed

In addition: Warning message:
non-unique values when setting 'row.names': ‘10.2’, ‘10.4’, ‘10.8’,
‘100.7’, ‘101.12’, ‘102.1’, ‘102.3’, ‘103.2’, ‘103.4’, 
‘103.6’, ‘104.12’, ‘104.3’, ‘104.9’, ‘105.1’, ‘105.5’, 
‘105.6’, ‘105.8’, ‘106.11’, ‘106.12’, ‘106.13’, ‘106.7’, 
‘107.10’, ‘107.14’, ‘107.3’, ‘108.12’, ‘108.2’, ‘108.6’, 
‘108.9’, ‘109.1’, ‘109.14’, ‘109.7’, ‘11.12’, ‘11.5’, 
‘11.9’, ‘110.2’, ‘110.3’, ‘110.4’, ‘110.9’, ‘111.1’, 
‘111.7’, ‘112.12’, ‘112.3’, ‘112.6’, ‘112.8’, ‘113.10’, 
‘113.13’, ‘113.7’, ‘114.12’, ‘114.2’, ‘114.9’, ‘115.10’, 
‘115.13’, ‘115.5’, ‘116.11’, ‘116.6’, ‘117.14’, ‘117.3’, 
‘117.7’, ‘118.1’, ‘118.13’, ‘118.2’, ‘118.9’, ‘119.10’, 
‘119.5’, ‘119.6’, ‘119.8’, ‘12.1’, ‘12.10’, ‘12.3’, 
‚Äò12.6‚Äô, ‚Äò120.2‚Äô, ‚Äò120.4‚Äô, ‚Äò120.7‚ [... truncated] 
> 

我在这里错过了哪一步?为什么在row.names中重复?

What step am I missing here? Why the duplicates in row.names?

谢谢, 沃尔特

推荐答案

两个问题.

您似乎在编码方面遇到了一些问题,因为我们在该错误消息中看到很多变音符号和重音符号.此外,我想知道该datekey列是否已转换为因子类?

You seem to have some problem with encoding since we are seeing lots of umlauts and accent marks in that error message. Furthernore I am wondering if that datekey column got converted into a factor class?

在这种情况下,它表示新对象x的row.names属性的构造错误.如果您这样做:

In this case it it referring to an error in construction of the row.names attribute of the new object, x. If you do:

 with( z, table( datekey, horseno) )

...您可能会看到一匹马在同一天有多个条目.

... you may see an a horse with multiple entries on the same day.

实际上,没有重复的datekey x horseno组合.更改为horseno和datekey的因数,然后将"long"参数切换为"wide"可产生无错误结果,并显示以下结果:

Actually there were no duplicate datekey x horseno combos. Changing to factor for horseno and datekey and then switching the "long" argument to "wide" produces error free result with this result:

z$datekey <- as.character(z$datekey)
z$horseno <- as.character(z$horseno)
x<-mlogit.data(z,choice="winner",shape="wide",id.var="datekey",alt.var="horseno")
str(x)
#----------
Classes ‘mlogit.data’ and 'data.frame': 18312 obs. of  11 variables:
 $ datekey : Factor w/ 733 levels "2008091501","2008091502",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ raceno  : int  1 1 1 1 1 1 1 1 1 1 ...
 $ horseno : chr  "0" "1" "0" "1" ...
 $ place   : int  1 1 2 2 3 3 4 4 5 5 ...
 $ winner  : logi  FALSE TRUE TRUE FALSE TRUE FALSE ...
 $ draw    : int  2 2 3 3 5 5 10 10 4 4 ...
 $ winodds : num  12 12 8.6 8.6 7 7 23 23 11 11 ...
 $ log_odds: num  2.48 2.48 2.15 2.15 1.95 ...
 $ jwt     : int  128 128 123 123 127 127 123 123 113 113 ...
 $ hwt     : int  1170 1170 1135 1135 1114 1114 1018 1018 1027 1027 ...
 $ chid    : num  1 1 2 2 3 3 4 4 5 5 ...
 - attr(*, "index")='data.frame':   18312 obs. of  3 variables:
  ..$ chid: Factor w/ 9156 levels "1","2","3","4",..: 1 1 2 2 3 3 4 4 5 5 ...
  ..$ alt : Factor w/ 2 levels "0","1": 1 2 1 2 1 2 1 2 1 2 ...
  ..$ id  : Factor w/ 733 levels "2008091501","2008091502",..: 1 1 1 1 1 1 1 1 1 1 ...
 - attr(*, "choice")= chr "winner"

这篇关于使用R语言的mlogit在`row.names&lt;-.. data.frame中出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆