r-如何从数据集中删除对回归模型有很大影响的两个数据点



我在数据集中发现了两个异常数据点,但我不知道如何删除它们。我在网上找到的所有指南似乎都强调绘制数据,但我的问题不需要绘制,只需要回归模型拟合。我很难找到如何从我的数据集中删除这两个数据点,然后用新模型拟合新的数据集。这是我写的代码和我发现的异常值:

library(alr4)
library(MASS)
data(lathe1)
head(lathe1)
y=lathe1$Life
x1=lathe1$Speed
x2=lathe1$Feed
x1_square=(x1)^2
x2_square=(x2)^2
#part A (Box-Cox method show log transformation)
y.regression=lm(y~x1+x2+(x1)^2+(x2)^2+(x1*x2))
mod=boxcox(y.regression, data=lathe1, lambda = seq(-1, 1, length=10))
best.lam=mod$x[which(mod$y==max(mod$y))]
best.lam
#part B (null-hypothesis F-test)
y.regression1_Reduced=lm(log(y)~1)
y.regression1=lm(log(y)~x1+x2+x1_square+x2_square+(x1*x2))
anova(y.regression1_Reduced, y.regression1)
#part D (F-test of log(Y) without beta1)
y.regression2=lm(log(y)~x2+x2_square)
anova(y.regression1_Reduced, y.regression2)
#part E (Cook's distance and refit)
cooks.distance(y.regression1)
Outliers:
9                10
0.7611370235     0.7088115474

我认为您可以(如果执行时间/语料库大小允许的话(使用循环传递数据,并根据您的标准复制/删除elems,以获得您想要的结果,例如

corpus_list_without_outliers = []
for elem in corpus_list:
if(elem.speed <= 10000) # elem.[any_param_name] < arbitrary_outlier_value
# push to corpus_list_without_outliers because it is OK :)
print corpus_list_without_outliers
# regression algorithm after

这就是我对情况的看法,但如果使用remove语句来避免创建第二个列表等,则可以更改以上内容,例如

for elem in corpus_list:
if(elem.speed > 10000) # elem.[any_param_name]
# remove from current corpus because it is an outlier :(
print corpus_list
# regression algorithm after

希望它能帮助你!

最新更新