I viewed lots of relevant bloggers who explained how to generate marginal effects, but somehow most of them did not take interaction effects as sample.
When I use Stata to do it, the margin effects are quite easy to compute, but R seems in another way and show different results.
I want to know how to compute proper marginal effects with interaction properly,
- why function
margins()
does not show the interaction factor? - if I compute this by hand
coef(glm)*mean(dlogis(predict(glm, type = "link")),na.rm=T)
,it looks more close to the Stata result, not with the functionmargins()
? - what's more, how can I compute the
standard error of marginal effects
handly, will it like thissqrt(diag(vcov(glm1)))*mean(dlogis(predict(glm, type = "link")),na.rm=T)
?
trydata=read.csv("https://vincentarelbundock.github.io/Rdatasets/csv/palmerpenguins/penguins.csv")
trydata[c("species","sex")]=lapply(trydata[c("species","sex" )], as.factor)
contrasts(trydata$species)=contr.treatment(3)
contrasts(trydata$sex)=contr.treatment(2)
try=glm(bill_length_mm ~sex+species+bill_depth_mm + sex*species, data=trydata)
summary(try)
summary(margins(try))
some links I saw before and thought useful:
code: marginal effects with interaction
Marginal effects calculated by hand, but standard error doesn't show