【问题标题】:Removing Bonferroni Outlier Test Results in a loop在循环中删除 Bonferroni 异常值测试结果
【发布时间】: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


【解决方案1】:

请见下文。诀窍是要注意,如果我没看错,异常值结果会给出行名。

library(car, quietly=TRUE)
galton <-read.csv("http://www.math.uah.edu/stat/data/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




currentData <- galton
linear_reg <- lm(Height~Father+Mother,data=currentData)

outlierResult <-outlierTest(linear_reg)
outlierResult

while(length(outlierResult)!=0){
  exclusionRows <-names(outlierResult[[1]])
  inclusionRows <- !(rownames(currentData) %in% exclusionRows)
  currentData <- currentData[inclusionRows,]

  linear_reg <- lm(Height~Father+Mother,data=currentData)
  outlierResult <-outlierTest(linear_reg)

}

【讨论】:

  • 感谢您的回答。当 outlierResult 等于“No Studentized residuals with Bonferonni p
猜你喜欢
  • 2017-09-25
  • 2020-10-12
  • 1970-01-01
  • 2019-03-22
  • 1970-01-01
  • 1970-01-01
  • 2021-09-22
  • 2016-04-25
  • 1970-01-01
相关资源
最近更新 更多