Why this error in my multinomial logistic regression code?

What and how should I change the variable Success or otherwise to not have this error?

> Chat.Up.Lines <- read.delim("~/Chat-Up Lines.dat", stringsAsFactors=TRUE)
>   View(Chat.Up.Lines)
> chat<-Chat.Up.Lines
> chat$Gender<-relevel(chat$Gender, ref=2)
> levels<-chat$Gender
> is.factor(chat$Success)
[1] TRUE
> chatwide<-mlogit.data(chat, choice="Success", shape="wide")
> chatwide
~~~~~~~
 first 10 observations out of 3060 
~~~~~~~
   Success Funny Sex Good_Mate Gender chid                  alt    idx
1     TRUE     3   7         6   Male    1     Get Phone Number 1:mber
2    FALSE     3   7         6   Male    1  Go Home with Person 1:rson
3    FALSE     3   7         6   Male    1 No response/Walk Off 1: Off
4    FALSE     5   7         2   Male    2     Get Phone Number 2:mber
5     TRUE     5   7         2   Male    2  Go Home with Person 2:rson
6    FALSE     5   7         2   Male    2 No response/Walk Off 2: Off
7     TRUE     4   6         6   Male    3     Get Phone Number 3:mber
8    FALSE     4   6         6   Male    3  Go Home with Person 3:rson
9    FALSE     4   6         6   Male    3 No response/Walk Off 3: Off
10   FALSE     3   7         5   Male    4     Get Phone Number 4:mber

~~~ indexes ~~~~
   chid                  alt
1     1     Get Phone Number
2     1  Go Home with Person
3     1 No response/Walk Off
4     2     Get Phone Number
5     2  Go Home with Person
6     2 No response/Walk Off
7     3     Get Phone Number
8     3  Go Home with Person
9     3 No response/Walk Off
10    4     Get Phone Number
indexes:  1, 2 
> levels("Success")
NULL
> chatmodel<-mlogit(Success~1 | Good_Mate+Funny+Gender+Sex+Gender:Sex+Funny:Gender, data=chatwide, reflevel=3)
Error in model.frame.dfidx(formula = list(Success = c(TRUE, FALSE, FALSE,  : 
  unknown reference level


Could you edit this to provide a reprex, please. See the FAQ: How to do a minimal reproducible example reprex for beginners which will allow reproducing the error. You can use dput(chat) and cut and paste the output. Also need to know which package provides mlogit.data and mlogit.

Hi, I'm studing Andy Field's 'statisic using R' too. I have the same problem that the mlogit function not worked.

Actually R version is updateed to 4.0.0, and mlogit package is updateted too.

we should use "reference.level" not "reflevel".

And mlogit do not print t-value. Now mlogit print z-value(Wald statistic).

I hope my answer is helpful.

Like this?

Datapaste
data.frame(
stringsAsFactors = FALSE,
Success = c("Get Phone Number",
"Go Home with Person","Get Phone Number","Go Home with Person",
"Get Phone Number","Get Phone Number",
"Go Home with Person","No response/Walk Off","Get Phone Number",
"Get Phone Number"),
Funny = c(3L, 5L, 4L, 3L, 5L, 4L, 5L, 5L, 3L, 4L),
Sex = c(7L, 7L, 6L, 7L, 1L, 7L, 8L, 7L, 6L, 7L),
Good_Mate = c(6L, 2L, 6L, 5L, 6L, 5L, 6L, 6L, 9L, 6L),
Gender = as.factor(c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA))
)

Reference.level does seem to help with the error, however one step before, it already mentions mlogit.data is deprecated and I should use dfidx: in stead of this:

chatwide<-mlogit.data(chat, choice="Success", shape="wide")

I don't know how to transform this to dfidx, what to fill in where.

Also I don't see how to change this:

chatmodel<-mlogit(Success~1 | Good_Mate+Funny+Gender+Sex+Gender:Sex+Funny:Gender, data=chatwide, reflevel=3)

To dfidx.

library(mlogit)
#> Loading required package: dfidx
#> 
#> Attaching package: 'dfidx'
#> The following object is masked from 'package:stats':
#> 
#>     filter
# full data with gender variable populated
chatData <- read.delim("https://www.discoveringstatistics.com/docs/Chat-Up%20Lines.dat")
# mlogit.data deprecated
chatwide <- dfidx(chatData, shape = "wide", choice = "Success")
chatwide
#> ~~~~~~~
#>  first 10 observations out of 3060 
#> ~~~~~~~
#>    Success Funny Sex Good_Mate Gender    idx
#> 1     TRUE     3   7         6   Male 1:mber
#> 2    FALSE     3   7         6   Male 1:rson
#> 3    FALSE     3   7         6   Male 1: Off
#> 4    FALSE     5   7         2   Male 2:mber
#> 5     TRUE     5   7         2   Male 2:rson
#> 6    FALSE     5   7         2   Male 2: Off
#> 7     TRUE     4   6         6   Male 3:mber
#> 8    FALSE     4   6         6   Male 3:rson
#> 9    FALSE     4   6         6   Male 3: Off
#> 10   FALSE     3   7         5   Male 4:mber
#> 
#> ~~~ indexes ~~~~
#>    id1                  id2
#> 1    1     Get Phone Number
#> 2    1  Go Home with Person
#> 3    1 No response/Walk Off
#> 4    2     Get Phone Number
#> 5    2  Go Home with Person
#> 6    2 No response/Walk Off
#> 7    3     Get Phone Number
#> 8    3  Go Home with Person
#> 9    3 No response/Walk Off
#> 10   4     Get Phone Number
#> indexes:  1, 2
# works without reference level
summary(mlogit(Success ~ 1 | Good_Mate + Funny + Gender + Sex + Gender:Sex + Funny:Gender, data = chatwide))
#> 
#> Call:
#> mlogit(formula = Success ~ 1 | Good_Mate + Funny + Gender + Sex + 
#>     Gender:Sex + Funny:Gender, data = chatwide, method = "nr")
#> 
#> Frequencies of alternatives:choice
#>     Get Phone Number  Go Home with Person No response/Walk Off 
#>              0.47549              0.13235              0.39216 
#> 
#> nr method
#> 6 iterations, 0h:0m:0s 
#> g'(-H)^-1g = 0.00121 
#> successive function values within tolerance limits 
#> 
#> Coefficients :
#>                                    Estimate Std. Error z-value  Pr(>|z|)    
#> (Intercept):Go Home with Person  -6.4834302  1.0348514 -6.2651 3.726e-10 ***
#> (Intercept):No response/Walk Off  3.4292930  0.5799859  5.9127 3.365e-09 ***
#> Good_Mate:Go Home with Person    -0.0018205  0.0770265 -0.0236 0.9811443    
#> Good_Mate:No response/Walk Off   -0.1318400  0.0537261 -2.4539 0.0141306 *  
#> Funny:Go Home with Person         0.8590296  0.1390523  6.1777 6.502e-10 ***
#> Funny:No response/Walk Off       -0.6318303  0.0865757 -7.2980 2.920e-13 ***
#> GenderMale:Go Home with Person    3.9801455  1.2106769  3.2875 0.0010107 ** 
#> GenderMale:No response/Walk Off  -1.6462233  0.7962471 -2.0675 0.0386891 *  
#> Sex:Go Home with Person           0.0127639  0.1040658  0.1227 0.9023826    
#> Sex:No response/Walk Off          0.0721201  0.0569356  1.2667 0.2052641    
#> GenderMale:Go Home with Person    0.1283129  0.1496895  0.8572 0.3913377    
#> GenderMale:No response/Walk Off  -0.3483262  0.1058754 -3.2900 0.0010020 ** 
#> Funny:Go Home with Person        -0.6799621  0.1651761 -4.1166 3.845e-05 ***
#> Funny:No response/Walk Off        0.4924415  0.1399925  3.5176 0.0004354 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Log-Likelihood: -868.74
#> McFadden R^2:  0.13816 
#> Likelihood ratio test : chisq = 278.52 (p.value = < 2.22e-16)

Created on 2023-05-21 with reprex v2.0.2

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.