[R] mlogit error

arun smartpink111 at yahoo.com
Sun Apr 7 17:45:44 CEST 2013


HI Graham,
Try this:
 lst1<-split(Mult3,Mult3$obs)
 Mult3New<-do.call(rbind,lst1[lapply(lst1,nrow)==6]) # YOu can change 6 to other numbers 
res1<- mlogit.data(Mult3New,shape="long",choice="CHOICE",alt.var="mode.ids",indivs="set3",chid.vars="obs")
head(res1,2)
#    dn obs choice br mth tcomp num prom1 prom2 prom3 prom4 prom5 tprom cLOSEC
#1.1  4   1      0  1 487     0 642     0     0     0     0     0     0      0
#1.2  4   1      0  2 487     0 642     0     0     0     0     0     0      0
#    cNEXIUM cPARIET cPROTIUM cZANTAC cZOTON comp1 comp2 comp3 comp4 lpat zpat
#1.1       0       0        0       0      0     0     0     0     0    0    0
#1.2       0       0        0       0      0     0     0     0     0    0    0
#    doq part NurP PhP NoNon              GPspec                    Newp
#1.1  78    3    0   0     1 No special interest Prefer tried and tested
#1.2  78    3    0   0     1 No special interest Prefer tried and tested
#                                            Guide    SHA           size exp
#1.1 Often follow local guidelines/advice from PCT London 3 - 5 PARTNERS  32
#1.2 Often follow local guidelines/advice from PCT London 3 - 5 PARTNERS  32
#        ALL_P sex disp form set3 set set2 tag1 mode.ids indivs CHOICE
#1.1 0.5295166   1    0    0    1   1    1    4        1      1  FALSE
#1.2 0.5295166   1    0    0    1   1    1    4        2      1  FALSE



A.K.



----- Original Message -----
From: "Leask, Graham" <g.leask at aston.ac.uk>
To: arun <smartpink111 at yahoo.com>
Cc: 
Sent: Sunday, April 7, 2013 11:28 AM
Subject: RE: [R] mlogit error

Hi Arun,

I think you are right the issue may be how to identify those obs where this doesn't apply for whatever reason.
The issue I think is the size of the data which makes identifying aberrant observations difficult.

This should be a balanced choice set but...


Acid brand |
choices as |
    numeric |      Freq.     Percent        Cum.
------------+-----------------------------------
          1 |     41,037       16.69       16.69
          2 |     41,037       16.69       33.38
          3 |     41,036       16.69       50.08
          4 |     40,849       16.62       66.69
          5 |     40,849       16.62       83.31
          6 |     41,036       16.69      100.00
------------+-----------------------------------
      Total |    245,844      100.00

I'm sure this is at the root of the problem. Is there a straightforward way in R to keep the balanced and lose the non balanced obs?


> Mult3$CHOICE<-factor(Mult3$choice,labels=c("no","yes"))
> Mult3$mode.ids<- as.numeric(as.character(Mult3$mode.ids))
>  res<-mlogit.data(Mult3[-25,],shape="long",choice="CHOICE",alt.var="mode.ids",indivs="set3",chid.vars="obs")
Error in `row.names<-.data.frame`(`*tmp*`, value = c("1.1", "1.2", "1.3",  : 
  duplicate 'row.names' are not allowed
In addition: Warning message:
non-unique values when setting 'row.names': '1.3', '20779.6', '20780.6', '20781.6', '20782.6', '20783.6', '20784.6', '20785.6', '20786.6', '20787.6', '20788.6', '20789.6', '20790.6', '20791.6', '20792.6', '20793.6', '20794.6', '20795.6', '20796.6', '20797.6', '20798.6', '20799.6', '20800.6', '20801.6', '20802.6', '20803.6', '20804.6', '20805.6', '20806.6', '20807.6', '20808.6', '20809.6', '20810.6', '20811.1', '20812.1', '20813.1', '20814.1', '20815.1', '20816.1', '20817.1', '20818.1', '20819.1', '20820.1', '20821.1', '20822.1', '20823.1', '20824.1', '20825.1', '20826.1', '20827.1', '20828.1', '20829.1', '20830.1', '20831.1', '20832.1', '20833.1', '20834.1', '20835.1', '20836.1', '20837.1', '20838.1', '20839.1', '20840.1', '20841.1', '20842.1', '20842.2', '20843.2', '20844.2', '20845.2', '20846.2', '20847.2', '20848.2', '20849.2', '20850.2', '20851.2', '20852.2', '20853.2', '20854.2', '20855.2', '20856.2', '20857.2', '20858.2', '20859.2', '20860.2',
 '20861.2', '20862.2', '20863.2', '20 [... truncated] 
> head(res,2)
  dn obs choice   acid br date cdate situation mth year set
1  4   1      0  LOSEC  1   NA    NA           487   NA   1
2  4   1      0 NEXIUM  2   NA    NA           487   NA   1

Best wishes


Graham

-----Original Message-----
From: arun [mailto:smartpink111 at yahoo.com] 
Sent: 07 April 2013 15:57
To: Leask, Graham
Cc: R help
Subject: Re: [R] mlogit error

Hi,
Try this:
 Mult3$mode.ids
 #[1] 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1
#Levels: 1 2 3 4 5 6
I guess the key is to have equal number of alternatives.
 Mult3$mode.ids<- as.numeric(as.character(Mult3$mode.ids))
 res<-mlogit.data(Mult3[-25,],shape="long",choice="CHOICE",alt.var="mode.ids",indivs="set3",chid.vars="obs")
head(res,2)
#    dn obs choice br mth tcomp num prom1 prom2 prom3 prom4 prom5 tprom cLOSEC
#1.1  4   1      0  1 487     0 642     0     0     0     0     0     0      0
#1.2  4   1      0  2 487     0 642     0     0     0     0     0     0      0
  #  cNEXIUM cPARIET cPROTIUM cZANTAC cZOTON comp1 comp2 comp3 comp4 lpat zpat
#1.1       0       0        0       0      0     0     0     0     0    0    0
#1.2       0       0        0       0      0     0     0     0     0    0    0
 #   doq part NurP PhP NoNon              GPspec                    Newp
#1.1  78    3    0   0     1 No special interest Prefer tried and tested
#1.2  78    3    0   0     1 No special interest Prefer tried and tested
 #                                           Guide    SHA           size exp
#1.1 Often follow local guidelines/advice from PCT London 3 - 5 PARTNERS  32
#1.2 Often follow local guidelines/advice from PCT London 3 - 5 PARTNERS  32
 #       ALL_P sex disp form set3 set set2 tag1 mode.ids indivs CHOICE
#1.1 0.5295166   1    0    0    1   1    1    4        1      1  FALSE
#1.2 0.5295166   1    0    0    1   1    1    4        2      1  FALSE A.K.




----- Original Message -----
From: "Leask, Graham" <g.leask at aston.ac.uk>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc: 
Sent: Sunday, April 7, 2013 10:39 AM
Subject: [R] mlogit error

Dear List

I am trying to fit a multinomial model using the mlogit package. Attempting to load the data into mlogit presents the following error.

MLOG<-mlogit.data(Mult3,shape="long",choice="CHOICE",alt.var="mode.ids",indivs = "set3",chid.var = "obs") Error in `row.names<-.data.frame`(`*tmp*`, value = c("1.1", "1.2", "1.3",  :
  duplicate 'row.names' are not allowed
In addition: Warning message:
non-unique values when setting 'row.names': '1.1', '1.2', '1.3', '1.4', '1.5', '1.6', '10.1', '10.2', '10.3', '10.4', '10.5', '10.6', '100.1', '100.2', '100.3', '100.4', '100.5', '100.6', '1000.1', '1000.2', '1000.3', '1000.4', '1000.5', '1000.6', '1001.1', '1001.2', '1001.3', '1001.4', '1001.5', '1001.6', '1002.1', '1002.2', '1002.3', '1002.4', '1002.5', '1002.6', '1003.1', '1003.2', '1003.3', '1003.4', '1003.5', '1003.6', '1004.1', '1004.2', '1004.3', '1004.4', '1004.5', '1004.6', '1005.1', '1005.2', '1005.3', '1005.4', '1005.5', '1005.6', '1006.1', '1006.2', '1006.3', '1006.4', '1006.5', '1006.6', '1007.1', '1007.2', '1007.3', '1007.4', '1007.5', '1007.6', '1008.1', '1008.2', '1008.3', '1008.4', '1008.5', '1008.6', '1009.1', '1009.2', '1009.3', '1009.4', '1009.5', '1009.6', '101.1', '101.2', '101.3', '101.4', '101.5', '101.6', '1010.1', '1010.2', '1010.3', '1010.4', '1010.5', '1010.6', '1011.1', '1011.2', '1011.3', '1011.4', '1011.5', '1011.6', 
 '1012.1', '1012.2', '1012.3!
', '1012.4' [... truncated]
>

The code that I am running is as follows

Mult3$mode.ids<-as.factor(Mult3$br)
Mult3$indivs<-factor(Mult3$set3)
Mult3$CHOICE<-factor(Mult3$choice,labels=c("no","yes"))
MLOG<-mlogit.data(Mult3,shape="long",choice="CHOICE",alt.var="mode.ids",indivs = "set3",chid.var = "obs")

My data is structured as follows

dput(head(Mult3,25))
structure(list(dn = c(4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4), obs = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L), choice = c(0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L), br = c(1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L), mth = c(487, 487, 487, 487, 487, 487, 488, 488, 488, 488, 488, 488, 488, 488, 488, 488, 488, 488, 489, 489, 489, 489, 489, 489, 489), tcomp = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), num = c(642, 642, 642, 642, 642, 642, 642, 642, 642, 642, 642, 642, 642, 642, 642, 642, 642, 642, 642, 642, 642, 642, 642, 642, 642), prom1 = c(0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0), prom2 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ), prom3 = c(0,
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0), prom4 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), prom5 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), tprom = c(0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0), cLOSEC = c(0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 3, 0, 0, 0, 3, 0), cNEXIUM = c(0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0),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 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), PhP = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), NoNon = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L ), GPspec = structure(c(2L, 2L, 2L, 2L,
 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Special interest GP", "No special interest"), class = "factor"), Newp = structure(c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("Always among first to try new product", "Like to try new regimens when available", "Wait to be confortable how the product works", "Prefer tried and tested", "Reluctant to try new products"), class = "factor"),
    Guide = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
    ), .Label = c("Always follow local guidelines/advice from PCT",
    "Often follow local guidelines/advice from PCT", "Sometimes follow local guidelines/advice from PCT",
    "Rarely follow local guidelines/advice from PCT", "Prefer to make my own decision regardless of advice"
    ), class = "factor"), SHA = structure(c(9L, 9L, 9L, 9L, 9L,
    9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
    9L, 9L, 9L, 9L, 9L), .Label = c("North East", "North West",
    "Yorkshire & Humber", "West Midlands", "East Midlands", "East of England",
    "South West", "South Central", "London", "South East Coast",
    "Scotland", "Wales"), class = "factor"), size = structure(c(3L,
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("SINGLE HANDED",
    "2 PARTNERS", "3 - 5 PARTNERS", "6 - 9 PARTNERS", "10 PLUS PARTNERS"
    ), class = "factor"), exp = c(32, 32, 32, 32, 32, 32, 32,
    32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
    32, 32, 32), ALL_P = c(0.529516577720642, 0.529516577720642,
    0.529516577720642, 0.529516577720642, 0.529516577720642,
    0.529516577720642, 0.529516577720642, 0.529516577720642,
    0.529516577720642, 0.529516577720642, 0.529516577720642,
    0.529516577720642, 0.529516577720642, 0.529516577720642,
    0.529516577720642, 0.529516577720642, 0.529516577720642,
    0.529516577720642, 0.529516577720642, 0.529516577720642,
    0.529516577720642, 0.529516577720642, 0.529516577720642,
    0.529516577720642, 0.529516577720642), sex = structure(c(2L,
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("0", "1"), class = "factor"),
    disp = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
    ), .Label = c("0", "1"), class = "factor"), form = structure(c(1L,
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0", "1"), class = "factor"),
    set3 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1), set = c(1, 1, 1, 1, 1, 1, 2,
    2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5), set2 = c(1,
    1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4,
    4, 4, 4, 4, 5), tag1 = c(4L, 4L, 4L, 4L, 4L, 0L, 4L, 4L,
    0L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 0L, 4L, 4L, 0L, 4L, 4L,
    4L, 4L), mode.ids = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 1L,
    2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L,
    5L, 6L, 1L), .Label = c("1", "2", "3", "4", "5", "6"), class = "factor"),
    indivs = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
    1L), .Label = c("1", "2", "3", "4", "5", "6", "7", "8", "9",
    "10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
    "20", "21", "23", "25", "26", "27", "28", "29", "30", "31",
    "32", "33", "34", "35", "36", "37", "38", "40", "41", "42",
    "43", "44", "45", "47", "48", "49", "50", "51", "52", "53",
    "54", "55", "56", "57", "58", "59", "60", "61", "62", "63",
    "64", "65", "66", "67", "68", "69", "70", "71", "72", "73",
    "74", "75", "76", "77", "78", "79", "80", "82", "83", "84",
    "85", "86", "87", "88", "89", "90", "91", "92", "93"), class = "factor"),
    CHOICE = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L,
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L,
    1L), .Label = c("no", "yes"), class = "factor")), .Names = c("dn", "obs", "choice", "br", "mth", "tcomp", "num", "prom1", "prom2", "prom3", "prom4", "prom5", "tprom", "cLOSEC", "cNEXIUM", "cPARIET", "cPROTIUM", "cZANTAC", "cZOTON", "comp1", "comp2", "comp3", "comp4", "lpat", "zpat", "doq", "part", "NurP", "PhP", "NoNon", "GPspec", "Newp", "Guide", "SHA", "size", "exp", "ALL_P", "sex", "disp", "form", "set3", "set", "set2", "tag1", "mode.ids", "indivs", "CHOICE"), row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25"), class = "data.frame")

I have noticed several previous posts regarding this error but no resolution has been posted to my knowledge.

Any help in resolving this would be appreciated.

Best wishes


Graham

    [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list