Удаление результатов теста Бонферрони на выбросы в цикле

Я смоделировал свои данные, используя линейную регрессию. Я хочу запустить тест Бонферрони несколько раз и удалить соответствующие записи из своих данных. Моя проблема: я не могу извлечь идентификатор из outlierResult. Вот воспроизводимый код. Я хочу написать цикл 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)

person Hamideh    schedule 09.03.2016    source источник
comment
Я рассматриваю это как проблему, связанную с программированием. Может быть, SO было бы лучшим местом для этого :)   -  person Dawny33    schedule 09.03.2016


Ответы (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)

}
person user1357015    schedule 22.04.2016
comment
Спасибо за ваш ответ. Существует проблема в третьем раунде цикла, когда результат выброса равен Нет студенческих остатков с Бонферонни p ‹ 0,05. Он проходит через самого большого ученика и никогда не выходит из цикла, пока не будут использованы все строки данных. - person Hamideh; 24.04.2016