James Prentice
James Prentice

Reputation: 55

Effect Plot R - Putting data points into effect Plot

current effect plot look in R

To all: I am trying to understand if there is a way to develop my effect plots further. I was wondering if there is a way to get the data points used to create the effect plots onto the effect plot itself, so it sort of looks like a scatter diagram.

I am using the packages: (ggplot2), (stargazer), (effects), (gridExtra), (plyr)

Code Used:

setwd("F:/Sussex PhD/data/constresult19")
my_gen <- read.csv("genhis.csv") #data set name

#linear models
const.qualsvc <- lm(Conpv1019 ~ + Noqual19 + Alevel19 + Degplus19 + Const5ac + Gdschlsrate + FSMKS2 + FSMAlevel, data = my_gen)
const.qualsvl <- lm(Conpv1019 ~ + Noqual19 + Alevel19 + Degplus19 + Const5ac + Gdschlsrate + FSMKS2 + FSMAlevel, data = my_gen)
stargazer(const.qualsvc, const.qualsvl, type = "text")

#effect plots
eddeg.cona <- effect(term = "Degplus19", mod = const.qualsvc)
eddeg.cona <- plot(eddeg.cona, ylim=c(-20,30), xlab = "Percent Degree Qualification Plus", ylab = "Percentage Change in Con vote 2010-19", main="Effect of Degrees on Change in Con % Vote", col.line = "blue")
eddeg.laba <- effect(term = "Degplus19", mod = const.qualsvl)
eddeg.laba <- plot(eddeg.laba, ylim=c(-20,35), xlab = "Percent Degree Qualification Plus", ylab = "Percentage Change in Lab vote 2010-19", main="Effect of Degrees on Change in Lab % Vote",col.line = "red")`

grid.arrange(eddeg.cona, eddeg.laba, ncol=2)`#effect plots side by side

Kind Regards, James Prentice (Only a fairly basic level user of R)

Upvotes: 0

Views: 2238

Answers (1)

krfurlong
krfurlong

Reputation: 897

Welcome to SO. I've never used the effects package, but it looks like you can extract the elements used to plot the fit and confidence intervals from the object produced by effect(). Without seeing your data, I can't provide much more advice than this minimal reproducible example though. Let me know if you have any questions.

library(ggplot2)
library(effects)

mpg.model <- lm(data = mtcars, mpg ~ wt + cyl) 

wt.eff <- effect(term = "wt", mod = mpg.model)

mpg.model.plot.data <- data.frame(
  wt.eff$x,
  eff.lower = wt.eff$lower,
  eff.upper = wt.eff$upper,
  eff.fit = wt.eff$fit
)

ggplot(data=mpg.model.plot.data, aes(x = wt)) +
  geom_ribbon(aes(ymin = eff.lower, ymax = eff.upper), fill = "blue", alpha = 0.3) +
  geom_line(aes(y = eff.fit), color = "blue")

Upvotes: 1

Related Questions