[R] Factors and Multinomial Logistic Regression

Lorenzo Isella lorenzo.isella at gmail.com
Wed May 1 22:40:08 CEST 2013


>
> (A) The example doesn't run for me. library(ares) is not available on  
> current R versions, but even where it is available, it doesn't provide a  
> multinom() function?


Apologies, ares is not needed at all. Please find the correct script at  
the end of the email.

>
> (B) If I insert library(nnet), to get a multinom(), I get exactly the  
> same result as Stata does!
>
> Did you by any chance diddle with options(contrasts=...)?
>
> -pd

No, I did not. The point is that if I use a variable female, which has two  
levels, then I do not reproduce the results of stata for that variable  
only.
If instead I define a variable "sex" which assumes the numerical values  
0/1, then I reproduce entirely the results by stata.

Hope this helps.

Lorenzo


##################################################################


library(foreign)

## See the Stata example at http://bit.ly/11VG4ha

mydata <- read.dta("http://www.ats.ucla.edu/stat/data/hsb2.dta")


sex <- rep(0, dim(mydata)[1])

sel <- which(mydata$female=="male")

sex[sel] <- 1

mydata$sex <- sex

## IMPORTANT: redefine the base line!!!

mydata$ses2 <- relevel(mydata$ses, ref = "middle")


## NB: for some reason, if I use female (a factor assuming two values)
## I do not reproduce the results of the example.
## I need to use a variable which is numeric and assumes two values
## (that is why I introduced the variable sex))

## mymodel <- multinom(ses2 ~ science+ socst+ sex, data=mydata)


mymodel <- multinom(ses2 ~ science+ socst+ female, data=mydata)




print(summary(mymodel))

print("The relative risk ratio (RRR) is, ")

print(exp(coef(mymodel)))



More information about the R-help mailing list