简体   繁体   中英

How can I filter out rows from linear regression based on another linear regression

I would like to conduct a linear regression that will have three steps: 1) Running the regression on all data points 2) Taking out the 10 outiers as found by using the absolute distanse value of rstandard 3) Running the regression again on the new data frame. I know how to do it manually but these is very awkwarding. Is there a way to do it automatically? Can it be done for taking out columns as well?

Here is my toy data frame and code (I'll take out 2 top outliers):

df <- read.table(text = "userid target birds    wolfs     
                 222       1        9         7 
                 444       1        8         4 
                 234       0        2         8 
                 543       1        2         3 
                 678       1        8         3 
                 987       0        1         2 
                 294       1        7         16 
                 608       0        1         5 
                 123       1        17        7 
                 321       1        8         7 
                 226       0        2         7 
                 556       0        20        3 
                 334       1        6         3 
                 225       0        1         1 
                 999       0        3         11 
                 987       0        30         1  ",header = TRUE) 
model<- lm(target~ birds+ wolfs,data=df)
rstandard <- abs(rstandard(model))
df<-cbind(df,rstandard)
g<-subset(df,rstandard > sort(unique(rstandard),decreasing=T)[3])
g
       userid target birds wolfs rstandard    
    4     543      1     2     3  1.189858    
   13    334      1     6     3  1.122579  

   modelNew<- lm(target~ birds+ wolfs,data=df[-c(4,13),])

I don't see how you could do this without estimating two models, the first to identify the most influential cases and the second on the data without those cases. You could simplify your code and avoid cluttering the workspace, however, by doing it all in one shot, with the subsetting process embedded in the call to estimate the "final" model. Here's code that does this for the example you gave:

model <- lm(target ~ birds + wolfs,
    data = df[-(as.numeric(names(sort(abs(rstandard(lm(target ~ birds + wolfs, data=df))), decreasing=TRUE)))[1:2]),])

Here, the initial model, evaluation of influence, and ensuing subsetting of the data are all built into the code that comes after the first data = .

Also, note that the resulting model will differ from the one your code produced. That's because your g did not correctly identify the two most influential cases, as you can see if you just eyeball the results of abs(rstandard(lm(target ~ birds + wolfs, data=df))) . I think it has to do with your use of unique() , which seems unnecessary, but I'm not sure.

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