据我所知,Stata和 R具有“预测”功能。我正在尝试使用 R 复制在 Stata 中执行的结果,结果涉及计算预测值的标准偏差。 R 中是否有一个功能,也许使用它的“预测”功能,可以让我这样做?我似乎无法完美地复制结果。如果有帮助,Stata 代码会执行以下操作:
reg Y X1 X2 if condition
predict resid, r
predict stdf, stdf
stdf
参数的定义是:
stdf
calculates the standard error of the forecast, which is the standard error of the point prediction for 1 observation. It is commonly referred to as the standard error of the future or forecast value. By construction, the standard errors produced bystdf
are always larger than those produced bystdp
; see Methods and formulas in [R] predict
我一直在写的 R 代码是:
fit <- lm(Y ~ X1 + X2, data=df)
new.df <- data.frame(...) # This is a new data frame with my new data period I want to predict in
predict(fit, new.df, se.fit = TRUE)
但是,当我将标准误差转换为标准偏差时,它们与 Stata 输出不匹配。
提前致谢!
最佳答案
在我看来你需要:
predict(fit, new.df, se.fit = TRUE, interval="prediction")
“标准误差”适用于均值估计值的置信限度,而预测误差可能很容易描述为预测值的“标准偏差”。
> dfrm <- data.frame(a=rnorm(30), drop=FALSE)
> dfrm$y <- 4+dfrm$a*5+0.5*rnorm(30)
> plot( dfrm$a, predict(mod) )
> plot( dfrm$a, predict(mod, newdata=dfrm) )
> points( rep(seq(-2,2,by=0.1),2), # need two copies for upper and lower
c(predict(mod, newdata=list(a=seq(-2,2,by=0.1)),
interval="prediction")[, c("lwr","upr")]),
col="red")
> points(dfrm$a, dfrm$y, col="blue" )
关于r - Stata 与预测的标准差和 R 与预测的标准差有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32320046/