如何解决模型的聚类标准误
我想计算以下回归模型的 clustered standard errors。不知道怎么定义?我不知道如何将公式翻译和应用到 R?
我的回归模型:
#include<iostream>
#include<vector>
using namespace std;
class node //node definition
{
public:
int data;
node* next;
node(int value=0)
{
data=value;
}
};
node* insert(node* head,int data) //node insertion
{
node* ins=new node(data);
if(head==NULL)
{
return ins;
}
else
{
node* ptr=head;
while(head->next!=NULL)
head=head->next;
head->next=ins;
ins->next=NULL;
return ptr;
}
}
void print(node* head) //printing the values of linked list
{
while(head!=NULL)
{
cout<<head->data<<" ";
head=head->next;
}
}
int main()
{
vector <int> a{1,2,3,6,8};
node* list=NULL;
for(int x:a)
{
list=insert(list,x);
}
print(list);
}
dput(df[1:3,])
model3<- lm(Dishwash_dur ~ work_minutes +
work_minutes_squared +
relevel(employment,ref = "FT")+
work_minutes*relevel(employment,ref = "FT")+
dishwas_betw +
DVAge +
DMSex+
DVHsize+
Income+
NumChild+
dishwas_betw * work_minutes+
dishwas_betw * work_minutes_squared,data = df,weights=dishwas_timedep)
Sample data:
解决方法
在sandwich::vcovCL
中使用聚类稳健协方差矩阵lmtest::coeftest
。你的数据不完整,所以我用 R 内置的 iris
数据做一个例子。
fit <- lm(Sepal.Length ~ .,iris)
library(lmtest);library(sandwich)
ct <- coeftest(fit,vcov.=vcovCL(fit,cluster= ~ Species))
ct
# t test of coefficients:
#
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 2.171266 0.314159 6.9114 1.438e-10 ***
# Sepal.Width 0.495889 0.121453 4.0830 7.344e-05 ***
# Petal.Length 0.829244 0.097682 8.4892 2.325e-14 ***
# Petal.Width -0.315155 0.110369 -2.8555 0.004933 **
# Speciesversicolor -0.723562 0.383713 -1.8857 0.061351 .
# Speciesvirginica -1.023498 0.528100 -1.9381 0.054571 .
# ---
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
如果出于某种原因您只需要标准错误,您可以这样做:
se <- ct[,2]
se
# (Intercept) Sepal.Width Petal.Length Petal.Width Speciesversicolor Speciesvirginica
# 0.31415923 0.12145252 0.09768204 0.11036863 0.38371255 0.52809998
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。