predLines after multinomial logit.

Welcome to the forum for R2MLwiN users. Feel free to post your question about R2MLwiN here. The Centre for Multilevel Modelling take no responsibility for the accuracy of these posts, we are unable to monitor them closely. Do go ahead and post your question and thank you in advance if you find the time to post any answers!

Go to R2MLwiN: Running MLwiN from within R >> http://www.bris.ac.uk/cmm/software/r2mlwin/
Post Reply
nuavgkim
Posts: 3
Joined: Thu May 31, 2018 12:03 am

predLines after multinomial logit.

Post by nuavgkim »

Hello R2MLwiN users,

I am trying to plot predicted probabilities after running a multinomial logistic regression with MCMC algorithm. As I type in the following commands:

Code: Select all

F5<-logit(lca) ~ 1+age+gender+...+polpart+rile_polarizationC+rile_polarizationC:polpart+(1|lev2)+(1|lev3)
m5<-runMLwiN(Formula=F5, data=QP_LCA, D="Unordered Multinomial", estoptions=list(EstM = 1, resioptions="standardized", resi.store=TRUE, resi.store.levs = 3, xc=FALSE, sort.force=TRUE, seed=220), MLwiNPath=("C:/Program Files (x86)/MLwiN v2.36/x64"))
and run predLines as follows:

Code: Select all

predLines(m5, xname="polpart", lev=3, legend=FALSE, probs=c(0.025, 0.975))
I get the following error message:
Error in as.vector(X) %*% t(as.vector(Y)) :
requires numeric/complex matrix/vector arguments

Any suggestions or help will be appreciated!
ChrisCharlton
Posts: 1351
Joined: Mon Oct 19, 2009 10:34 am

Re: predLines after multinomial logit.

Post by ChrisCharlton »

Unfortunately the predLines() function is not supported for multinomial models as the data stored in the model object is the unexpanded version, as sent to MLwiN, rather than the expanded version used for estimation. This results in the predLines() function being unable to match the model parameters with the corresponding data columns. Workarounds might be to turn on the debugmode option and then create the desired predictions within the MLwiN interface after the model as run and before the results are returned to R. Alternatively you could try manually expanding the data (making sure that the column names match those referenced in the parameter names), and then replacing the data in the model object with this new data. Sorry I couldn't be much more help.
nuavgkim
Posts: 3
Joined: Thu May 31, 2018 12:03 am

Re: predLines after multinomial logit.

Post by nuavgkim »

Thanks for the response, Chris. I have two follow-up questions.

1. How can I manually expand the dataset? I see that columns used in the model are renamed with prefixes and suffixes, such as FP_ or RP_ and _1 or _2. I wonder (1) how I should rename them and (2) how the expanded dataset suitable for predLines command should look like.

2. As I use the debug mode, MLWIN seems to recognize interaction terms (i.e., A:B) as linear terms. Is there a way to specify them as interaction terms on MLWIN under debug mode? Is it okay to delete the A:B terms and add cross-products instead? Would it allow me to run predLInes after tossing the dataset back to R?
ChrisCharlton
Posts: 1351
Joined: Mon Oct 19, 2009 10:34 am

Re: predLines after multinomial logit.

Post by ChrisCharlton »

1. To manually expand the data you would need to increase the number of rows so that within each level-1 unit there is a row for each of the response categories (except the base). Within each level-1 group you would then put a 1 in the appropriate row to indicate which category was in the original row. You would then for each explanatory variable create columns for each category and put the original value into the matching row/column. The column names would need to match the fixed-part parameter names in your model (minus the FP_) prefix).

2. If you just need the data for prediction in R then you can leave the terms as they are, however if you want to use the customised predictions faculties in MLwiN then you would probably want to specify the terms as actual interactions.

As predLines was not written to work with multinomial models it would require some effort to adapt it. The following example gets to the point where the data matches the model parameters, however it still does not provide the expected graph:

Code: Select all

# Load the R2MLwiN library
library(R2MLwiN)
# Load the "bang" example dataset
data(bang, package = "R2MLwiN")
# Set the base category
bang$use4 <- relevel(bang$use4, 4)
# Run the model in debug mode in order to extract the data
# To do this check which columns correspond to the expanded data (in this case bcons.1 up to age.Traditional_method).
# Then choose File->Export from the menu, select these columns and save as a dta file (in this example expanded.dta).
# Having done this click the "resume" button each time it appears until you are back in R.
(mymodel <- runMLwiN(log(use4) ~ 1 + age + (1 | district), D = "Unordered Multinomial", estoptions = list(EstM = 1, debugmode=TRUE, resi.store=TRUE, resi.store.levs = 2), data = bang))
# Load the foreign package to read the expanded data
library(foreign)
# Read the data into R
expanded <- read.dta("expanded.dta")
# Replace "." with "_" in the column names to match the parameter names
colnames(expanded) <- sub("\\.", "_", colnames(expand))
# Attach the expanded data to the saved model object
mymodel@data <- expanded
# Attempt to call predLines (but note the graph is blank)
predLines(mymodel, xname="age_Modern_reversible_method", lev=2, legend=FALSE, probs=c(0.025, 0.975))
predLines() (see https://github.com/rforge/r2mlwin/blob/ ... redLines.r) would therefore need to be adapted to allow it to work for this case. I will put this on the TODO list, but I suspect that it is unlikely to be implemented any time soon. If you get anything working however I would be interested in incorporating it into the package.

I suspect that the easiest method would be instead to use the prediction functionality within MLwiN once it has received the model and data from R.
nuavgkim
Posts: 3
Joined: Thu May 31, 2018 12:03 am

Re: predLines after multinomial logit.

Post by nuavgkim »

Thanks for sharing the code, Chris. As you suggested, I will forward the equation to MLWIN and plot from there. I will keep you posted if I make any successful modification to predLines in R.
Post Reply