Graphing the fixed part of the model produces jittery results
Posted: Fri Jul 22, 2016 9:35 am
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!
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!