Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Plotting different models

Hello!

I'm wondering how I could reproduce the two-panels figure comparing a model with interaction and a model without interaction on the Bayes Factor page:
https://richarddmorey.github.io/BayesFactor/#glm
Anyone knows the r code for plotting this?

Thank you in advance!

Comments

  • The basic steps are:

    1. Make a scatterplot
    2. Fit the model
    3. Extract the model parameters from the object (whatever it is)
    4. Use abline() to plot the lines

    You can do this with either lm() estimates or BayesFactor::posterior() estimates (or whatever). I'll demonstrate below with lm() because it is slightly easier.

    # scatter plot
    plot(len ~ dose, data = ToothGrowth, log = "x", 
         pch = as.integer(ToothGrowth$supp)+20,
         bg = as.integer(ToothGrowth$supp), 
         las = 1)
    
    # Use log10 because I plotted the log of x above
    cf = coef(lm(len ~ log10(dose)*supp, data = ToothGrowth))
    # coef extracts the coefficients
    abline(a = cf["(Intercept)"], b = cf["log10(dose)"])
    abline(a = cf["(Intercept)"] + cf["suppVC"], 
           b = cf["log10(dose)"] + cf["log10(dose):suppVC"], col= "red")
    
    
    ## Now for the no-interaction model
    # scatter plot
    plot(len ~ dose, data = ToothGrowth, log = "x", 
         pch = as.integer(ToothGrowth$supp)+20,
         bg = as.integer(ToothGrowth$supp), 
         las = 1)
    
    # Use log10 because I plotted the log of x above
    cf = coef(lm(len ~ log10(dose) + supp, data = ToothGrowth))
    # coef extracts the coefficients
    abline(a = cf["(Intercept)"], b = cf["log10(dose)"])
    abline(a = cf["(Intercept)"] + cf["suppVC"], 
           b = cf["log10(dose)"], col= "red")
    
    Thanked by 2profgaelle MSB
  • For many supported models, you can also the tidyverse approach, with the ggeffects package.

Sign In or Register to comment.