简体   繁体   English

限制回归并在R中聚类SE

[英]restricting a regression and clustering SE's in R

I'm trying to restrict a regression, only using the datapoints where a variable everevac==1 .. i figured out one way to do it, but am wondering if there's a better way. 我试图限制回归,仅使用其中变量everevac == 1 ..的数据点。我想出了一种方法,但是想知道是否有更好的方法。

In STATA, I would have just run something like this: 在STATA中,我将只运行以下内容:

reg outcome y2006 age black male etc if everevac==1, cluster(persid)

I came up with this for R: 我为R想到了这个:

fit <- ols(formula = outcome[everevac==1] ~ y2006[everevac==1] + 
             age[everevac==1] + black[everevac==1] + 
             male[everevac==1] + hsgrad[everevac==1] + 
             hsgrad[everevac==1] + someco[everevac==1] + 
             ba[everevac==1] + postgrad[everevac==1], x=TRUE, y=TRUE, data = ps2_new)
robcov(fit, cluster = ps2_new$persid[ps2_new$everevac==1])

notice that I just restricted all of the variables, to make it var[everevac==1] ..is this even doing what I think it's doing? 请注意,我只是限制了所有变量,使其变为var [everevac == 1] ..这是否还在做我认为正在做的事情? is there a beter way to do it? 有更好的方法吗? i tried using an "if" statement like this: 我尝试使用这样的“ if”语句:

if(everevac==1){ <lm function above, taking out the [everevac==1] on each variable> }

but it didn't work. 但这没用。

Add this parameter to the ols call, then do not refer to the cluster id by the external value, but rather by the name that will then be evaluated in the context of the fit -object which only has the subset -ed data: 将此参数添加到ols调用中,然后不要通过外部值来引用集群ID,而是通过将在fit -object的上下文中进行评估的名称(仅包含subset -ed数据)来引用:

... , subset = everevac==1)

fit <- ols(formula = outcome ~ y2006 + 
                age + black + 
                male + 
                hsgrad + someco + 
                ba + postgrad,
            x=TRUE, y=TRUE, 
            data = ps2_new, subset = everevac==1)
robcov(fit, cluster = persid)

BTW, it is considered courteous to include library(rms) in the code block. 顺便说一句,在代码块中包含library(rms)被认为是礼貌的。

Simply indexing the dataframe should be sufficient 只需索引数据框就足够了

fit <- ols(formula = outcome ~ y2006 + age + black + male + hsgrad + 
             hsgrad + someco + ba + postgrad, x=TRUE, y=TRUE, 
             data = ps2_new[ps2_new$everevac==1,])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM