 # 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 .

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")

``````
reg_model <- lm(score ~ exercise + attend, data = regression_data)

``````

Thx

To learn to plot i recommend

Particularly chapter 3

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.

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?

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,

``````set.seed(42)
library(tidyverse)

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

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

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

ggplot(data=mydata,
mapping=aes(x=exercise,
y=score,
color=attend)) + geom_point() +
geom_line(data=lines_df %>% filter(attend==0)) +
geom_line(data=lines_df %>% filter(attend==1))
`````` 1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.