简体   繁体   中英

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.

In STATA, I would have just run something like this:

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

I came up with this for 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? is there a beter way to do it? i tried using an "if" statement like this:

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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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