Graphing the fixed part of the model produces jittery results

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
adeldaoud
Posts: 63
Joined: Sat Aug 15, 2015 4:00 pm

Graphing the fixed part of the model produces jittery results

Post by adeldaoud »

What I would like to produce are prediction graphs for the fixed part of the model, and in a separate graph the random part of the model. With confidence intervals. However, I have been experimenting with this without success for the fixed part of the model.

I followed http://www.bris.ac.uk/cmm/media/r2mlwin/UserGuide05.R. But I am having a hard time figuring out how to hold a set of variables at their mean (or bootstrapped), whereas letting my main predictor vary over its value range. In other words, this follows basic a prediction procedure.

Most (all) examples in UserGuide05 use one predictor in their models and subsequently one can use predLines, predCurves, predict functions with no problems. The problem is that when adding more than one predictors, these functions produce jittery graphs:

(A)
data(tutorial, package = "R2MLwiN")

# only the main predictor
## ==> things work fine.
(mymodel1 <- runMLwiN(normexam ~ 1 + standlrt + (1 | school) + (1 | student), estoptions = list(resi.store = T, debugmode=F),
data = tutorial))
predLines(mymodel1, xname = "standlrt", probs = c(.025,.975))

# the main predictor and other controls
## ==> jittery graph
(mymodel2 <- runMLwiN(normexam ~ 1 + standlrt + avslrt + sex + (1 | school) + (1 | student), estoptions = list(resi.store = T, debugmode=F),
data = tutorial))
predLines(mymodel1, xname = "standlrt", probs = c(.025,.975))
(mymodel2 <- runMLwiN(normexam ~ 1 + standlrt + avslrt + sex + (1 | school) + (1 | student), estoptions = list(resi.store = T, debugmode=F),
data = tutorial))
predLines(mymodel1, xname = "standlrt", probs = c(.025,.975))

(B) Note that this applies to the predictCurves() function as well

# Works fine
predCurves(mymodel1, xname = "standlrt")

# Produces jittery graphs
predCurves(mymodel2, xname = "standlrt")

# Produces an jittery graph
predCurves(mymodel2, xname = "standlrt", group ="sexgirl")

# Produces ok graph (only running with the relevant variables)
(mymodel3 <- runMLwiN(normexam ~ 1 + standlrt + sex + (1 | school) + (1 | student), estoptions = list(resi.store = T, debugmode=F),
data = tutorial))

predCurves(mymodel3, xname = "standlrt", group =" sexgirl ")

(C) I can produce similar results with the predict()

# Jittery graph
xb <- predict(mymodel1)
pred <- as.data.frame(cbind(mymodel1@data$school, mymodel1@data$standlrt, xb)[order(mymodel1@data$school, mymodel1@data$standlrt),
])
colnames(pred) <- c("school", "standlrt", "xb")

xyplot(xb ~ standlrt, type = "l", group = school, data = pred)

# Ok graph (even if it tricks us by collapsing all schools to similar values)
xb <- predict(mymodel2)
pred <- as.data.frame(cbind(mymodel2@data$school, mymodel2@data$standlrt, xb)[order(mymodel2@data$school, mymodel2@data$standlrt),
])
colnames(pred) <- c("school", "standlrt", "xb")

xyplot(xb ~ standlrt, type = "l", group = school, data = pred)



Any input is much appreciated!

ChrisCharlton
Posts: 1193
Joined: Mon Oct 19, 2009 10:34 am

Re: Graphing the fixed part of the model produces jittery results

Post by ChrisCharlton »

One way to do this would be to create a new data-set where you have replaced the values of the other predictors with their means. You would then pass in this data to the predict function with the newdata parameter. Unfortunately this option does not work with the released version of R2MLwiN. I have just fixed this in the development version, however as the git mirror does not appear to be currently updating I have attached a source build here. This can be installed with the command:

Code: Select all

install.packages("R2MLwiN_0.8-3.tar.gz", repos=NULL, type="source")
Attachments
R2MLwiN_0.8-3.tar.gz
(334.81 KiB) Downloaded 174 times

adeldaoud
Posts: 63
Joined: Sat Aug 15, 2015 4:00 pm

Re: Graphing the fixed part of the model produces jittery results

Post by adeldaoud »

Thanks, Chris. I will have a test drive asap. I assume that the other recent changes are built into this file as well.

Post Reply