【发布时间】:2016-06-24 01:12:39
【问题描述】:
我使用线性回归对数据进行建模。我想多次运行 Bonferroni 异常值测试并从我的数据中删除相应的记录。我的问题是:我无法从 outlierResult 中提取 id。这是可重现的代码。我想根据伪代码写一个while循环。我在 R 中编码。
# URL <- "http://www.math.uah.edu/stat/data/Galton.csv"
# download.file(URL, destfile = "./galton.csv", method="curl")
galton <-read.csv("galton.csv")
attach(galton)
dim(galton)
head(galton)
##creating outliers
set.seed(1)
random_index <- sample(1:nrow(galton), size = 5, replace = FALSE, prob = NULL)
print(random_index)
galton[random_index,"Height"] = galton[random_index,"Height"] +100
set.seed(2)
random_index2 <- sample(1:nrow(galton), size = 5, replace = FALSE, prob = NULL)
galton[random_index2,"Height"] = galton[random_index2,"Height"] +75
set.seed(3)
random_index3 <- sample(1:nrow(galton), size = 5, replace = FALSE, prob = NULL)
galton[random_index3,"Height"] = galton[random_index3,"Height"] +50
linear_reg <- lm(Height~Father+Mother,data=galton)
require(car, quietly=TRUE)
outlierResult <-outlierTest(linear_reg)
outlierResult
# the pseudocode
# while outlierResult is not empty
# remove the corresponding records
# linear_reg <- lm(Height~Father+Mother,data=galton)
# outlierResult <-outlierTest(linear_reg)
【问题讨论】:
-
我认为这是一个与编程相关的问题。也许 SO 会是一个更好的地方:)
标签: r linear-regression outliers data-cleaning