에있는 lm 개체의 목록을 에 lm
개의 모델 개체가 반복 될 수 있으므로이 두 개의 개체가 동일한 지 확인하는 방법을 찾고 싶습니다. 즉, 내 list
을 "중복 제거"하고 싶습니다.중복 된 R
대단히 감사합니다.
문제의 예 :
## Creates outcome and predictors
outcome <- c(names(mtcars)[1:3])
predictors <- c(names(mtcars)[4:11])
dataset <- mtcars
## Creates model list
model_list <- lapply(seq_along((predictors)), function(n) {
left_hand_side <- outcome[1]
right_hand_side <- apply(X = combn(predictors, n), MARGIN = 2, paste, collapse = " + ")
paste(left_hand_side, right_hand_side, sep = " ~ ")
})
## Convert model list into a verctor
model_vector <- unlist(model_list)
## Fit linear models to all itens from the vector of models
list_of_fit <- lapply(model_vector, function(x) {
formula <- as.formula(x)
fit <- step(lm(formula, data = dataset))
fit
})
# Exclude possible missing
list_of_fit <- Filter(Negate(function(x) is.null(unlist(x))), list_of_fit)
# These models are the same in my list
lm253 <- list_of_fit[[253]];lm253
lm254 <- list_of_fit[[254]];lm254
lm255 <- list_of_fit[[255]];lm255
내가 list_of_fit
에서 중복 된 항목을 제외 할.
'vector' 또는'data.frame'에서'duplicated()'를 사용할 수는 있지만'list's에 대한 유사점은없는 것 같습니다. 원칙적으로 두 개의 'for'루프를 중첩하고 'identical'를 사용하여 수동으로 두 항목을 테스트 할 수 있습니다 ('? identical'를 반드시 읽으십시오!). –
'all.equal (lm253, lm254)'는'TRUE'를 반환하지 않으므로 중복되지 않습니다. 하지만 전화가 중복되는 것에 신경 쓰는 것 같습니까? – shadow
불쌍한 영어로 유감스럽게 생각합니다. 저는 BR 출신이고 언어를 배우고 있습니다! Stephan Kolassa에게 감사드립니다. – user2154480