Page 1 of 1

Graphing the fixed part of the model produces jittery results

Posted: Fri Jul 22, 2016 9:35 am
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!

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

Posted: Mon Jul 25, 2016 11:29 am
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")

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

Posted: Thu Jul 28, 2016 9:20 am
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.