Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 有没有办法通过子集循环回归?_R_Loops - Fatal编程技术网

R 有没有办法通过子集循环回归?

R 有没有办法通过子集循环回归?,r,loops,R,Loops,对于回归项目,我使用kmeans通过nbclust创建了数据集群。该项目目前有4个集群,但如果成功,该项目可能涉及分析多个美国城市。虽然我可以手动构建每个回归,但我更愿意最小化代码量,从而减少编码错误的机会 当前模型使用具有多个子集和10-15个不同自变量的glm。我目前正在手工构建每个集群回归 Tulsa$Cluster <- Tulsa$Best.partition #This creates a vector of clusters; there are currently 4 di

对于回归项目,我使用kmeans通过nbclust创建了数据集群。该项目目前有4个集群,但如果成功,该项目可能涉及分析多个美国城市。虽然我可以手动构建每个回归,但我更愿意最小化代码量,从而减少编码错误的机会

当前模型使用具有多个子集和10-15个不同自变量的glm。我目前正在手工构建每个集群回归

Tulsa$Cluster <- Tulsa$Best.partition
#This creates a vector of clusters; there are currently 4 distinct clusters

summary(Tulsa_Cluster1 <- glm(formula = Tulsa$Result ~ Tulsa$FactorA Tulsa$FactorB + Tulsa$FactorC + Tulsa$FactorD + Tulsa$FactorE + Tulsa$FactorF, 
                              method = "glm.fit", family = gaussian(),subset = Tulsa$Cluster =="1"))

summary(Tulsa_Step <- stepAIC(Tulsa_Cluster1, direction = "both",trace = FALSE, 
                              scope=list(lower = ~ Tulsa_Cluster1$FactorA)))

Tulsa$Cluster考虑使用
unique
levels
split
、和
by
来迭代集群的唯一值的以下方法之一:

唯一的
+
用于

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
}
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
+
lappy
(无子集参数)


考虑使用
unique
levels
split
by
迭代集群的唯一值的以下方法之一:

唯一的
+
用于

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
}
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
+
lappy
(无子集参数)


摘要
行的哪一部分需要循环?冻糕-两种说法。第一条语句是集群的“基本”回归。第二条语句是逐步回归,在初始回归完成后立即开始。需要循环
摘要
行的哪一部分?冻糕-这两条语句。第一条语句是集群的“基本”回归。第二种说法是逐步回归,初始回归一完成就开始了。谢谢!这太棒了,我真的很感谢你的帮助。谢谢!这太棒了,我真的很感谢你的帮助。