Рассмотрим любой из следующих подходов, использующих unique
, levels
, split
и by
для итерации по уникальным значениям Cluster :
unique
+ for
for (c in unique(Tulsa$Cluster)) {
Tulsa_Cluster <- glm(formula = Result ~ FactorA + FactorB + FactorC + FactorD + FactorE + FactorF,
data = Tulsa, method = "glm.fit", family = gaussian(), subset = Cluster == c)
print(summary(Tulsa_Cluster)) # REQUIRED IN for LOOPS TO FOR CONSOLE OUTPUT
Tulsa_Step <- stepAIC(Tulsa_Cluster, direction = "both", trace = FALSE,
scope=list(lower = ~ Tulsa_Cluster$FactorA))
print(summary(Tulsa_Step)) # REQUIRED IN for LOOPS TO FOR CONSOLE OUTPUT
}
levels
+ for
for (c in levels(factor(Tulsa$Cluster))) {
Tulsa_Cluster <- glm(formula = Result ~ FactorA + FactorB + FactorC + FactorD + FactorE + FactorF,
data = Tulsa, method = "glm.fit", family = gaussian(), subset = Cluster == c)
print(summary(Tulsa_Cluster))
Tulsa_Step <- stepAIC(Tulsa_Cluster, direction = "both", trace = FALSE,
scope=list(lower = ~ Tulsa_Cluster$FactorA))
print(summary(Tulsa_Step))
}
Ниже двух возвращает именованный список объектов.
split
+ lapply
(без подмножества аргументов)
data_list <- split(Tulsa, Tulsa$Cluster)
model_list <- lapply(data_list, function(sub_df) {
Tulsa_Cluster <- glm(formula = Result ~ FactorA + FactorB + FactorC + FactorD + FactorE + FactorF,
data = sub_df, method = "glm.fit", family = gaussian())
Tulsa_Step <- stepAIC(Tulsa_Cluster, direction = "both", trace = FALSE,
scope=list(lower = ~ Tulsa_Cluster$FactorA))
# RETURN BOTH SUMMARIES INTO LIST
return(list(Tulsa_Cluster, Tulsa_Step))
})
by
(без подмножества аргументов)
model_list <- by(Tulsa, Tulsa$Cluster, function(sub_df) {
Tulsa_Cluster <- glm(formula = Result ~ FactorA + FactorB + FactorC + FactorD + FactorE + FactorF,
data = sub_df, method = "glm.fit", family = gaussian())
Tulsa_Step <- stepAIC(Tulsa_Cluster, direction = "both", trace = FALSE,
scope=list(lower = ~ Tulsa_Cluster$FactorA))
# RETURN BOTH SUMMARIES INTO LIST
return(list(Tulsa_Cluster, Tulsa_Step))
})