Combine with grid.arrange two residue charts (lme vs. lm)

Asked

Viewed 138 times

1

I’m trying to put in the same image, side by side, the residue graphs adjusted by the linear model and the mixed linear model. However, I consulted the following links below and still my error persists:

https://stackoverflow.com/questions/34802295/control-layout-when-displaying-a-series-of-ggplot-plots-stored-in-a-list

https://stackoverflow.com/questions/29121372/how-to-plot-multiple-lme-residual-plots-in-one-device

How can I combine these charts into one row and two columns?

library(nlme)
Orthodont[c(1:20),]
data("Orthodont")

lm1 <- lm(distance ~ age, data=Orthodont)
fm1 <- lme(distance ~ age, Orthodont, random = ~ age | Subject)

p1=plot(fm1)
p2=plot(lm1$residuals)
myplots = list(p1,p2)

grid.arrange(grobs=myplots,ncol=2,top="Main Title", layout_matrix=cbind(c(1,2),c(3,4)))
do.call(grid.arrange, c(myplots,ncol=2,top="Main Title"))

> grid.arrange(grobs=myplots,ncol=2,top="Main Title", layout_matrix=cbind(c(1,2),c(3,4)))
Error in gList(list(p = list(formula = .y ~ .x, as.table = FALSE, aspect.fill = TRUE,  : 
  only 'grobs' allowed in "gList"
> do.call(grid.arrange, c(myplots,ncol=2,top="Main Title"))
Error in gList(list(p = list(formula = .y ~ .x, as.table = FALSE, aspect.fill = TRUE,  : 
  only 'grobs' allowed in "gList"

1 answer

4


Before I give my answer to the question, I would like to say that there are a number of problems with this waste analysis. Created graphs do not even show the same information. The graph for the model lm1 shows standardized residues versus adjusted values (which is not generally used in this specific context), while the graph for the model fm1 shows the Residues versus Position in the database (which is a graph that makes no sense, because the order of the waste should not have influence on the quality of the adjustment performed).

In addition to this specific problem, there are usually several graphs used to analyze the residues of a linear model, whether mixed or mixed. Below I show the most common four for the adjusted model and saved in the object lm1.

library(nlme)
library(ggfortify)
#> Loading required package: ggplot2

lm1 <- lm(distance ~ age, data=Orthodont)

autoplot(lm1)

Created on 2020-11-13 by the reprex package (v0.3.0)

Note that there is more than one graph being plotted simultaneously. In particular, the graph in the upper right corner is what allows us to determine whether the residues follow an approximately normal distribution. I recommend taking a look at some reference of linear models to know what each of these graphs means (Kutner et al (2013), Applied Linear Statistical Models is a good starting point, although as I recall it does not deal with mixed models with many details).

That said, the problem here is the origin of each of the graphics. The package nlme uses the package lattice to make model charts lm1, while the graph refers to the model fm1 is done with the standard graphics of R. Notice how the graphics styles are very different from each other.

library(nlme)

lm1 <- lm(distance ~ age, data=Orthodont)
fm1 <- lme(distance ~ age, Orthodont, random = ~ age | Subject)

plot(fm1)

plot(lm1$residuals)

Created on 2020-11-13 by the reprex package (v0.3.0)

Therefore, my suggestion is that both graphics be done in the same style, be the standard of R, lattice or ggplot2. For simplicity, I’ll do both lattice, using the function for this xyplot in the case of the second graph.

With the two graphics ready, just use the function grid.arrange package gridExtra to obtain the desired result.

library(gridExtra)

p1 <- plot(fm1)
p2 <- xyplot(residuals ~ 1:length(lm1$residuals), lm1)

grid.arrange(p1, p2, ncol = 2)

Created on 2020-11-13 by the reprex package (v0.3.0)

  • 1

    Thanks @Marcus Nunes!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.