1

In my plot below, d_math and d_hyp are each {0,1} variables. Given this fact, in my plot below, I was wondering if we can combine the two plots into one, just like in the desired plot further below?

ps. I'm open to any R packages.

multivariate <- read.csv('https://raw.githubusercontent.com/hkil/m/master/bv.csv')

library(nlme)
library(effects) # for plot

m2 <- lme(var ~ 0 + d_math + d_hyp + d_math:I(grade-2) + d_hyp:I(grade-2),
          random = ~ 0 + d_math + d_hyp + d_math:I(grade-2) + d_hyp:I(grade-2) | id, data = multivariate,
          na.action = na.omit, weights = varIdent(c(hyp=.3), form = ~1|grp),
          control = lmeControl(maxIter = 200, msMaxIter = 200, niterEM = 50,
                               msMaxEval = 400))

plot(allEffects(m2), multiline = TRUE, x.var="grade")

enter image description here

Desired:

enter image description here

rnorouzian
  • 7,397
  • 5
  • 27
  • 72

2 Answers2

1

We could use tidyverse to create a single plot. Loop over the list of allEffects output with imap, convert to tibble, select the columns needed, row bind the list elements to single dataset (_dfr), unite two columns to a single, and use ggplot for plotting

library(dplyr)
library(tidyr)
library(purrr)
library(ggplot2)
imap_dfr(allEffects(m2), ~ as_tibble(.x) %>% 
     mutate(dname = grep("d_", names(.), value = TRUE)) %>%
     select(dname, dvalue = starts_with('d_'), grade, fit) %>%
     mutate(grp = .y)) %>%
   unite(dname, dname, dvalue, sep=" = ") %>% 
   ggplot(aes(x = grade, y = fit, color = dname)) +
        geom_line() +
        theme_bw() #+
        # facet_wrap(~ grp)

-output

enter image description here


If we want the labels at the end of line, use directlabels

library(directlabels)
imap_dfr(allEffects(m2), ~ as_tibble(.x) %>% 
     mutate(dname = grep("d_", names(.), value = TRUE)) %>%
     select(dname, dvalue = starts_with('d_'), grade, fit) %>%
     mutate(grp = .y)) %>%
   unite(dname, dname, dvalue, sep=" = ") %>% 
   ggplot(aes(x = grade, y = fit, group = dname, color = dname)) +
        geom_line() +
        theme_bw() +
        scale_colour_discrete(guide = 'none') +
        geom_dl(aes(label = dname), method="last.qp", cex = 0.8)

Also, this can be done for each 'dvalue' as a facet

imap_dfr(allEffects(m2), ~ as_tibble(.x) %>% 
     mutate(dname = grep("d_", names(.), value = TRUE)) %>%
     select(dname, dvalue = starts_with('d_'), grade, fit) %>%
     mutate(grp = .y)) %>%
   unite(dname, dname, dvalue, sep=" = ", remove = FALSE) %>% 
   ggplot(aes(x = grade, y = fit, group = dname, color = dname)) +
        geom_line() +
        theme_bw() +
        scale_colour_discrete(guide = 'none') +
        geom_dl(aes(label = dname), method="last.qp", cex = 0.8) + 
        facet_wrap(~ dvalue)

enter image description here


Or if we need only a specific level, then filter

imap_dfr(allEffects(m2), ~ as_tibble(.x) %>% 
     mutate(dname = grep("d_", names(.), value = TRUE)) %>%
     select(dname, dvalue = starts_with('d_'), grade, fit) %>%
     mutate(grp = .y)) %>%
   unite(dname, dname, dvalue, sep=" = ") %>%
   filter(dname  %in% c("d_hyp = 1", "d_math = 1")) %>% 
   ggplot(., aes(x = grade, y = fit, colour = dname, group = dname)) + 
     geom_line() + 
     scale_colour_discrete(guide = 'none') +  
     geom_dl(aes(label = dname), method="last.qp", cex = 0.6) + 
     theme_bw()

enter image description here

akrun
  • 874,273
  • 37
  • 540
  • 662
1

You could do it like this with lattice and a bit more brute force than @akrun's approach:

e <- allEffects(m2)

f1 <- matrix(e[[1]]$fit, ncol=5) # math
f2 <- matrix(e[[2]]$fit, ncol=5) # hyp

dat = data.frame(
  fit = c(f1[5,], f2[5,]), 
  grade = rep(c(2,4,5,6,8), 2), 
  variable = factor(rep(1:2, each=5), 
                    labels=c("Math=1", "Hyp=1"))
  )


xyplot(fit ~ grade, data=dat, group=variable, type="l", 
       auto.key=list(space="top", lines=TRUE,points=FALSE))

enter image description here

DaveArmstrong
  • 18,377
  • 2
  • 13
  • 25