最初,我主要想在R中使用带聚类标准误的probit / logit模型,这在Stata中非常直观。我在这里找到了答案 Logistic regression with robust clustered standard errors in R。
因此,我尝试比较使用鲁棒标准误和聚类标准误的Stata和R的结果。但是我注意到两种软件的标准误输出并不完全相同。然而,如果我使用这里建议的方法https://diffuseprior.wordpress.com/2012/06/15/standard-robust-and-clustered-standard-errors-computed-in-r/。我可以从R和Stata获得线性回归的精确输出。因此,我担心我在R中编写的代码是否正确,以及如果我想运行probit模型而不是logit模型应该使用什么命令。或者是否有任何优雅的替代方案来解决这个问题?谢谢。
R代码
## 1. linear regression
library(rms)
# model<-lm(Sepal.Length~Sepal.Width+Petal.Length+Petal.Width,iris)
summary(model)
fit=ols(Sepal.Length~Sepal.Width+Petal.Length+Petal.Width, x=T, y=T, data=iris)
fit
robcov(fit) #robust standard error
robcov(fit, cluster=iris$Species) #clustered standard error
## 2. logistic regression
##demo data generation
set.seed(1234)
subj<-rep(1:20,each=4)
con1<-rep(c(1,0),40)
con2<-rep(c(1,1,0,0),20)
effect<-rbinom(80,1,0.34)
data<-data.frame(subj,con1,con2,effect)
library(foreign);write.dta(data,'demo_data.dta')
library(rms)
fit=lrm(effect ~ con1 + con2, x=T, y=T, data=data)
fit
robcov(fit) ##robust standard error
robcov(fit, cluster=data$subj) ## clustered standard error
Stata 代码
## 1. linear regression
webuse iris
reg seplen sepwid petlen petwid
reg seplen sepwid petlen petwid,r
reg seplen sepwid petlen petwid,cluster(iris)
## 2. logistic regression
use demo_data,clear
logit effect con1 con2
logit effect con1 con2,r
logit effect con1 con2,cluster(subj)
not exactly the same
是什么意思吗?这里涉及到很多默认值,它们可能是不同的。我们无法事先确定哪些默认值更好。但如果你想得到完全相同的值,你需要弄清楚Stata
和robcov
使用了哪些默认值,并进行相应的调整。 - coffeinjunkylogistic
前就使用了logit
?“logistic
显示估计值为比率;若要查看系数,请在运行logistic
后输入logit
”。 - noumenal