【发布时间】:2016-07-01 13:09:52
【问题描述】:
我有以下data.frame:
time values outlier
20/01/2010 11 no
20/02/2010 12 no
20/03/2010 11 no
20/04/2010 12 no
20/05/2010 10 no
20/06/2010 20 yes
20/07/2010 11 no
20/02/2010 12 no
我想以values 作为我的自变量和time 作为因变量对这个数据框进行回归。但我想排除outlier 列中所有带有“是”的行。
这是我尝试过的:
temp <- subset(df, outlier==yes)
fit <- lm(as.vector(temp$value) ~ as.vector(temp$time))
slope <- fit$coefficients[[2]]
intrcpt <- fit$coefficients[[1]]
temp$regression_points <- temp$value*fit$coefficients[[2]]+fit$coefficients[[1]]
现在我想使用获得的回归模型来预测来自temp 的原始值,并将结果放回原始数据框中,如下所示:
time values outlier regression_points
20/01/2010 11 no 11
20/02/2010 12 no 11
20/03/2010 11 no 11
20/04/2010 12 no 11
20/05/2010 10 no 11
20/06/2010 20 yes
20/07/2010 11 no 11
20/02/2010 12 no 11
我该如何解决这个问题。
【问题讨论】:
-
您可以从生成一个最小的可重现示例开始。包含带有
dput的数据框样本。此外,您是否知道异常稳健的回归方法?他们应该不必事先删除异常值。
标签: r statistics linear-regression lm