R预测包VS斯塔塔边距

编程入门 行业动态 更新时间:2024-10-18 18:28:39
本文介绍了R预测包VS斯塔塔边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我从Stata切换到R,当我使用预测来计算边距pred和Stata命令 margins 的结果时发现不一致的结果,将变量的值固定为 x .这是示例:

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

这是结果:

# 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

如果我尝试使用Stata的边距复制它,则结果是:

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

R和Stata中x2的边距相同,但是当涉及x1时,存在差异,我不知道为什么.非常感谢您的帮助.谢谢,

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

推荐答案

您的Stata和R代码不相同.要复制该Stata代码,您需要:

Your Stata and R code are not equivalent. To replicate that Stata code you would need:

> prediction(m2, at = list(x1 = c("1", "2"), x2 = "1")) Average predictions for 6 observations: at(x1) at(x2) value 1 1 3.125 2 1 4.275 > prediction(m2, at = list(x2 = c("1", "2", "3"), x1 = "1")) Average predictions for 6 observations: at(x2) at(x1) value 1 1 3.125 2 1 2.825 3 1 2.425

这是因为当您说margins i.x1时,您要求对数据集的反事实版本进行预测,其中x1被替换为1,然后被替换为2,并且附加的约束条件是两个反事实x2都保持为1在您的第二个Stata示例中也发生了同样的事情.

That is because when you say margins i.x1 you are asking for predictions for counterfactual versions of the dataset where x1 is replaced with 1 and then replaced with 2, with the additional constraint that in both counterfactual x2 is held at 1. The same thing is occurring in your second Stata example.

这是由于以下事实:Stata的margins命令具有歧义,或者两个语法表达式获得相同的输出.一个是您的代码:

This is due to the fact that Stata's margins command has an ambiguity or rather two syntactic expressions that obtain the same output. One is your code:

. margins i.x1, at(x2 == 1) Predictive margins Number of obs = 6 Model VCE : OLS Expression : Linear prediction, predict() at : x2 = 1 ------------------------------------------------------------------------------ | Delta-method | Margin Std. Err. t P>|t| [95% Conf. Interval] -------------+---------------------------------------------------------------- x1 | 1 | 3.125 .0829156 37.69 0.017 2.071457 4.178543 2 | 4.275 .0829156 51.56 0.012 3.221457 5.328543 ------------------------------------------------------------------------------

另一个是关于上面实际发生的事情的更明确的说明:

The other is more explicit about what is actually happening in the above:

. margins, at(x1 = (1 2) x2 == 1) Predictive margins Number of obs = 6 Model VCE : OLS Expression : Linear prediction, predict() 1._at : x1 = 1 x2 = 1 2._at : x1 = 2 x2 = 1 ------------------------------------------------------------------------------ | Delta-method | Margin Std. Err. t P>|t| [95% Conf. Interval] -------------+---------------------------------------------------------------- _at | 1 | 3.125 .0829156 37.69 0.017 2.071457 4.178543 2 | 4.275 .0829156 51.56 0.012 3.221457 5.328543 ------------------------------------------------------------------------------

更多推荐

R预测包VS斯塔塔边距

本文发布于:2023-11-29 16:19:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646927.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:斯塔   塔边距

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!