I'm switching from Stata to R, and I find inconsistent results when I use prediction to compute marginal pred and the results from the Stata command margins fixing the values of a variable to x. Here is the example:
library(dplyr)
library(prediction)
d <- data.frame(x1 = factor(c(1,1,1,2,2,2), levels = c(1, 2)),
x2 = factor(c(1,2,3,1,2,3), levels = c(1, 2, 3)),
x3 = factor(c(1,2,1,2,1,2), levels = c(1, 2)),
y = c(3.1, 2.8, 2.5, 4.3, 4.0, 3.5))
m2 <- lm(y ~ x1 + x2 + x3, d)
summary(m2)
marg2a <- prediction(m2, at = list(x2 = "1"))
marg2b <- prediction(m2, at = list(x1 = "1"))
marg2a %>%
select(x1, fitted) %>%
group_by(x1) %>%
summarise(error = mean(fitted))
marg2b %>%
select(x2, fitted) %>%
group_by(x2) %>%
summarise(error = mean(fitted))
This is the result:
# A tibble: 2 x 2
x1 error
<fctr> <dbl>
1 1 3.133333
2 2 4.266667
# A tibble: 3 x 2
x2 error
<fctr> <dbl>
1 1 3.125
2 2 2.825
3 3 2.425
while if I try to replicate this using Stata's margins, this is the result:
regress y i.x1 i.x2 i.x3
margins i.x1, at(x2 == 1)
margins i.x2, at(x1 == 1)
------------------------------------------------------------------------------
| Delta-method
| Margin Std. Err. t P>|t| [95% Conf. Interval]
-------------+----------------------------------------------------------------
x1 |
1 | 3.125 .0829157 37.69 0.017 2.071456 4.178544
2 | 4.275 .0829157 51.56 0.012 3.221456 5.328544
------------------------------------------------------------------------------
------------------------------------------------------------------------------
| Delta-method
| Margin Std. Err. t P>|t| [95% Conf. Interval]
-------------+----------------------------------------------------------------
x2 |
1 | 3.125 .0829157 37.69 0.017 2.071456 4.178544
2 | 2.825 .0829157 34.07 0.019 1.771456 3.878544
3 | 2.425 .0829157 29.25 0.022 1.371456 3.478544
------------------------------------------------------------------------------
The margins for x2 are the same in R and Stata, but when it comes to x1 there are differences and I don't know why. Really appreciate any help. Thanks,
P