I have built a simple lm model as shown below:
model1 = lm(score ~
study_hours +
courses_taken +
study_hours:courses_taken +
taken_before +
mediumenjoyment +
highenjoyment +
female,
data = course_data)
now, I'm trying to compute the marginal effect at the means. So, my code for doing so is:
mean_list = data.frame(mean(score),
mean(study_hours),
mean(courses_taken),
mean(study_hours:courses_taken),
mean(taken_before),
mean(mediumenjoyment),
mean(highenjoyment),
mean(female))
mar2 = margins(model1, data = mean_list)
summary(mar2)
But when I run the code, this error is raised:
Error in mean(score) : object 'score' not found
I suspect that the error is in this line:
Typically, object names in
R
do not include colons, so I imagine that you can’t take the mean.In passing, I’ll note that the
margins
package works great in many cases, but that it is not actively being developed. You could try the newermarginaleffects
package (disclaimer: I am the author). That package allows you to compute marginal effects at the mean (documentation) easily by typing: