Skip to main content
deleted 7 characters in body
Source Link
kjetil b halvorsen
  • 82.8k
  • 32
  • 201
  • 663

I would be grateful if someone could explain what is happening here. Thanks!

I would be grateful if someone could explain what is happening here. Thanks!

I would be grateful if someone could explain what is happening here.

Source Link
Lyly
  • 31
  • 1
  • 2

Unable to provide random parameter with mlogit

I'm having trouble with rpar argument in the mlogit function (package mlogit).

My dataset looks like this:

> head(scan.s)
   year id.scan day weather sealvl wave lact repro     nb.gr       HS nb.pv pup act
1 2011       1   4       2   0.30    3    1     0 0.6666667 7.600000     3   0   R
2 2011       1   4       2   0.30    3    1     0 0.6666667 7.600000     3   0   R
3 2011       1   4       2   0.30    3    1     0 0.6666667 7.600000     3   1   R
4 2011       2   4       2   0.35    3    1     0 0.6666667 8.100000     2   0   R
5 2011       2   4       2   0.35    3    1     0 0.6666667 8.100000     2   1   R
6 2011       3   4       2   0.40    3    1     0 0.6666667 8.633333     2   0   R

> str(scan.s)
'data.frame':   10140 obs. of  13 variables:
 $ year   : int  2011 2011 2011 2011 2011 2011 2011 2011 2011 2011 ...
 $ id.scan: Factor w/ 280 levels "1","2","3","4",..: 1 1 1 2 2 3 3 4 4 5 ...
 $ day    : int  4 4 4 4 4 4 4 4 4 4 ...
 $ weather: Factor w/ 3 levels "1","2","3": 2 2 2 2 2 2 2 2 2 2 ...
 $ sealvl : num  0.3 0.3 0.3 0.35 0.35 0.4 0.4 0.5 0.5 0.6 ...
 $ wave   : Factor w/ 4 levels "1","2","3","4": 3 3 3 3 3 3 3 3 3 3 ...
 $ lact   : int  1 1 1 1 1 1 1 1 1 1 ...
 $ repro  : int  0 0 0 0 0 0 0 0 0 0 ...
 $ nb.gr  : num  0.667 0.667 0.667 0.667 0.667 ...
 $ HS     : num  7.6 7.6 7.6 8.1 8.1 ...
 $ nb.pv  : int  3 3 3 2 2 2 2 2 2 2 ...
 $ pup    : int  0 0 1 0 1 0 1 0 1 0 ...
 $ act    : Factor w/ 5 levels "A","C","D","G",..: 5 5 5 5 5 5 5 5 5 5 ...

Then I used mlogit.data to transform my dataset in long shape:

> scan.l<- mlogit.data(scan, varying = NULL, choice = "act", shape = "wide")

There is no variable varying across choices.

    year id.scan day weather sealvl wave lact repro     nb.gr  HS nb.pv pup   act chid alt
1.A 2011       1   4       2    0.3    3    1     0 0.6666667 7.6     3   0 FALSE    1   A
1.C 2011       1   4       2    0.3    3    1     0 0.6666667 7.6     3   0 FALSE    1   C
1.D 2011       1   4       2    0.3    3    1     0 0.6666667 7.6     3   0 FALSE    1   D
1.G 2011       1   4       2    0.3    3    1     0 0.6666667 7.6     3   0 FALSE    1   G
1.R 2011       1   4       2    0.3    3    1     0 0.6666667 7.6     3   0  TRUE    1   R
2.A 2011       1   4       2    0.3    3    1     0 0.6666667 7.6     3   0 FALSE    2   A

> str(scan.l)
Classes ‘mlogit.data’ and 'data.frame': 50700 obs. of  15 variables:
 $ year   : int  2011 2011 2011 2011 2011 2011 2011 2011 2011 2011 ...
 $ id.scan: Factor w/ 280 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ day    : int  4 4 4 4 4 4 4 4 4 4 ...
 $ weather: Factor w/ 3 levels "1","2","3": 2 2 2 2 2 2 2 2 2 2 ...
 $ sealvl : num  0.3 0.3 0.3 0.3 0.3 0.3 0.3 0.3 0.3 0.3 ...
 $ wave   : Factor w/ 4 levels "1","2","3","4": 3 3 3 3 3 3 3 3 3 3 ...
 $ lact   : int  1 1 1 1 1 1 1 1 1 1 ...
 $ repro  : int  0 0 0 0 0 0 0 0 0 0 ...
 $ nb.gr  : num  0.667 0.667 0.667 0.667 0.667 ...
 $ HS     : num  7.6 7.6 7.6 7.6 7.6 7.6 7.6 7.6 7.6 7.6 ...
 $ nb.pv  : int  3 3 3 3 3 3 3 3 3 3 ...
 $ pup    : int  0 0 0 0 0 0 0 0 0 0 ...
 $ act    : logi  FALSE FALSE FALSE FALSE TRUE FALSE ...
 $ chid   : num  1 1 1 1 1 2 2 2 2 2 ...
 $ alt    : chr  "A" "C" "D" "G" ...
 - attr(*, "index")='data.frame':	50700 obs. of  2 variables:
  ..$ chid: Factor w/ 10140 levels "1","2","3","4",..: 1 1 1 1 1 2 2 2 2 2 ...
  ..$ alt : Factor w/ 5 levels "A","C","D","G",..: 1 2 3 4 5 1 2 3 4 5 ...
 - attr(*, "choice")= chr "act"

Then I ran the model:

mod1 <- mlogit(act ~ 1| nb.gr+nb.pv+sealvl+lact+repro+HS+day+id.scan,data = na.omit(scan.l), rpar=id.scan, format="long", reflevel="R", R=100, halton=NA, print.level=0)

The random parameter here is a factor and I am supposed to specify a distribution for rpar but is it relevant for a factor? (I tried to provide a distribution without any change).

And then I get this:

Error in coef(eval(callst, parent.frame())) : 
  error in evaluating the argument 'object' in selecting a method for function'coef' : Error in solve.default(H, g[!fixed]) : Lapack routine dgesv: system is exactly singular

There is a way to use "HS" and "day" instead, both numerical. But then I get another error:

Error in names (sup.coef) <- names.sup.coef: Attribute 'names' [1] must be the same length as the vector [0] 

traceback() did not provide any insight about what happened.

I searched for explanations with those errors and found that there could be a problem between one outcome and the random effect so I tried to subset my dataset with every combination of 3 outcomes with the same result. I found nothing relevant about the second error. Maybe it has something to do with the transformation with mlogit.data. I checked the dataset provided with the mlogit package and could not figure out what I did different.

I would be grateful if someone could explain what is happening here. Thanks!