Plotting regression model with dummy variable

I woul need help with plotting regression slopes for dummy variable. I would like to get the same plot as the one from the image
image .

Dataset has three variables: score (score achieved at exam), exercise (number of hours spent preparing for exam) and attend (dummy variable with two levels - 0 - didn't attend lectures and 1 - attended lectures)

I would like to plote regression slopes for those who have attended lectures and those who didn't. Plot from image is created in Stata but I would like to recreate it in R.

In Stata it's created by function

scatter score exercise if attend == 0, msymbol(square) legend(lab(1 "did not attend lectures")) ///
|| scatter score exercise if attend == 1, msymbol(lgx) legend(lab(2 "attended lectures")) ///
|| function y = -4.58 + 1.28x, range(0 25) lcolor(black) ///
|| function y = 6.41 + 1.28
x, range(0 25) lcolor(black) lpattern(dash) ///
ytitle("Scores achieved on the test")

regression_data <- foreign::read.dta("exam.dta")
reg_model <- lm(score ~ exercise + attend, data = regression_data)


To learn to plot i recommend

Particularly chapter 3

So you don't need help with plotting. OK.

Maybe you need help with predicting values from the model, given inputs. I.e. the predict function?

Thank you but I use R Studio for a long time and I don't have problem with plotting but in this case I would need to come up with function that would plot regression slop fro dummy coded variables.

Dear nirgrahamum I would like to thank you for your kind feedback but I need to recreate plot from the image that I've uploded. If you can help me with that great if not thank you.

Kind regards,


(mydata <- tibble(,50,replace=TRUE),
  attend = factor(,1),times=25))
) %>% mutate(
  score=(attend=="1")*5+exercise*2.5 +,50,replace=TRUE)/100

(mymodel <- lm(score ~ ., data=mydata))

(lines_df <- tribble(
  ~exercise, ~ attend ,
) %>% mutate(
  attend=factor(attend)) %>% mutate(
  score=predict.lm(object = mymodel,
                   newdata = .)))

                   color=attend)) + geom_point() +
  geom_line(data=lines_df %>% filter(attend==0)) +
  geom_line(data=lines_df %>% filter(attend==1))


1 Like