Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.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运行for循环时崩溃_R_For Loop_Stat - Fatal编程技术网

R运行for循环时崩溃

R运行for循环时崩溃,r,for-loop,stat,R,For Loop,Stat,我是一个r-初学者,有一个代码,我试图做的平均剪影方法。当我尝试运行for循环时,R停止并出现错误:R for Windows GUI前端已停止工作。我使用的代码是: data_no_demographics<-read.csv(file.choose(),row.names=1) data_no_demographics <- na.omit(data_no_demographics) library(cluster) k_max<-20 sil<-rep(0,k_ma

我是一个r-初学者,有一个代码,我试图做的平均剪影方法。当我尝试运行for循环时,R停止并出现错误:R for Windows GUI前端已停止工作。我使用的代码是:

data_no_demographics<-read.csv(file.choose(),row.names=1)
data_no_demographics <- na.omit(data_no_demographics)
library(cluster)
k_max<-20
sil<-rep(0,k_max)
for(i in 2:k_max){
  km.res <- kmeans(scale(data_no_demographics), centers = i, nstart = 50)
  ss <- silhouette(km.res$cluster, scale(data_no_demographics))
  sil[i] <- mean(ss[, 3])
}
plot(1:k_max, sil, type = "b", pch = 20,col="black",frame = FALSE, xlab ="Number of clusters (k)", ylab="Average silhouette width (fit of data within its cluster)", main="Optimal number of clusters")
abline(v = which.max(sil), lty = 2, col="red")
输出:(数据结构(列表(DRY.FOOD.mean.PREPARATION=c)(0,0,0,0,0),BASMATI.RICE=c(0.106274747,0.086781763,0.066892377,0.039564525),JASMINE.RICE=c(0.037947215,0.101672855,0.02509401,0.026354222),意大利语.RICE=c(0,0.006335413,0),ORG.SIDE.disks=c(0,0,0),cous=c(0.014091056,0.013493856,0.009541397,0.006704727),藜麦=c(0.013921964,0.020977683,0.011593311,0.006638343),调味米=c(0.3221638223,0.225682349,0.378639581,0.340580191),即食米=c(0.063184297,0.044711557,0.092459218,0.152615507),煮米=c(0.065396593982349,0.03809),RTH.speciality=c(0.010371018,0.011771236,0.009418283,0.007833245),RTH.RICE=c(0.307790945, 0.286375991,0.29621422,0.336331652),OTHER.RICE=c(0.013020136,0.019484745,0.010382682,0.006737918),REGULAR.DRY.RICE=c(0.045838206,0.093059756,0.051215759,0.044344132),READY.TO.EAT=c(0,0,0,0,0,0,0,0,0,0),name=c(“干食品.膳食.制备”,“巴斯马蒂.米饭”,“茉莉.米饭”,“茉莉.米饭”,“意大利米饭”,“组织配菜”,“库斯”,“藜麦”,“调味米”、“即食米”、“煮熟米”、“特产米”、“特产米”、“其他米”、“普通干米”,
“READY.TO.EAT”)、row.names=c(1000L,1004L,1007L,1008L),class=“data.frame”)

我刚刚对您提供的数据进行了测试,发现了一些警告,因为使用了
剪影
。将
剪影(km.res$cluster,scale(data\no\u demographics))
更改为
剪影(km.res$cluster,dist(data\no\u demographics))
删除了警告

data_no_demographics <- data.frame(
  DRY.FOOD.MEAL.PREPARATION = c(0, 0, 0, 0),
  BASMATI.RICE =c(0.106274747, 0.086781763, 0.066892377, 0.039564525), 
  JASMINE.RICE = c(0.037947215, 0.101672855, 0.025094901, 0.026354222), 
  ITALIAN.RICE = c(0, 0.006335413, 0, 0), 
  ORG.SIDE.DISHES = c(0, 0, 0, 0), 
  COUSCOUS = c(0.014091056, 0.013493856, 0.009541397, 0.006704727), 
  QUINOA = c(0.013921964, 0.020977683, 0.011593311, 0.006638343), 
  FLAVOURED.RICE = c(0.322163823, 0.225682349, 0.378639581, 0.340580191),
  INSTANT.RICE = c(0.063184297, 0.044711557, 0.092459218, 0.152615507),
  PARBOILED.RICE = c(0.065396593, 0.089652796, 0.048548271, 0.032295539),
  RTH.SPECIALTY = c(0.010371018, 0.011771236, 0.009418283, 0.007833245), 
  RTH.RICE = c(0.307790945, 0.286375991, 0.29621422, 0.336331652), 
  OTHER.RICE = c(0.013020136, 0.019484745, 0.010382682, 0.006737918), 
  REGULAR.DRY.RICE = c(0.045838206, 0.093059756, 0.051215759, 0.044344132), 
  READY.TO.EAT = c(0, 0, 0, 0), 
  row.names = c(1000L, 1004L, 1007L, 1008L)
)
# omit columns that are just zeros
data_no_demographics <- data_no_demographics[-c(1,5,15)]

library(cluster)
k_max<-3
sil<-rep(0,k_max)
distance_matrix <- dist(data_no_demographics)
for(i in 2:k_max){
  km.res <- kmeans(scale(data_no_demographics), centers = i, nstart = 1)
  ss <- silhouette(km.res$cluster, distance_matrix)
  sil[i] <- mean(ss[, 3])
}
plot(1:k_max, sil, type = "b", pch = 20,col="black",frame = FALSE, 
  xlab ="Number of clusters (k)", 
  ylab="Average silhouette width (fit of data within its cluster)", 
  main="Optimal number of clusters")
abline(v = which.max(sil), lty = 2, col="red")

data\u no\u demographics我刚刚对您提供的数据进行了一次测试,发现了一些警告,因为使用了
substrate
。将
substrate(km.res$cluster,scale(data\u no\u demographics))
更改为
substrate(km.res$cluster,dist(data\u demographics))
删除了警告

data_no_demographics <- data.frame(
  DRY.FOOD.MEAL.PREPARATION = c(0, 0, 0, 0),
  BASMATI.RICE =c(0.106274747, 0.086781763, 0.066892377, 0.039564525), 
  JASMINE.RICE = c(0.037947215, 0.101672855, 0.025094901, 0.026354222), 
  ITALIAN.RICE = c(0, 0.006335413, 0, 0), 
  ORG.SIDE.DISHES = c(0, 0, 0, 0), 
  COUSCOUS = c(0.014091056, 0.013493856, 0.009541397, 0.006704727), 
  QUINOA = c(0.013921964, 0.020977683, 0.011593311, 0.006638343), 
  FLAVOURED.RICE = c(0.322163823, 0.225682349, 0.378639581, 0.340580191),
  INSTANT.RICE = c(0.063184297, 0.044711557, 0.092459218, 0.152615507),
  PARBOILED.RICE = c(0.065396593, 0.089652796, 0.048548271, 0.032295539),
  RTH.SPECIALTY = c(0.010371018, 0.011771236, 0.009418283, 0.007833245), 
  RTH.RICE = c(0.307790945, 0.286375991, 0.29621422, 0.336331652), 
  OTHER.RICE = c(0.013020136, 0.019484745, 0.010382682, 0.006737918), 
  REGULAR.DRY.RICE = c(0.045838206, 0.093059756, 0.051215759, 0.044344132), 
  READY.TO.EAT = c(0, 0, 0, 0), 
  row.names = c(1000L, 1004L, 1007L, 1008L)
)
# omit columns that are just zeros
data_no_demographics <- data_no_demographics[-c(1,5,15)]

library(cluster)
k_max<-3
sil<-rep(0,k_max)
distance_matrix <- dist(data_no_demographics)
for(i in 2:k_max){
  km.res <- kmeans(scale(data_no_demographics), centers = i, nstart = 1)
  ss <- silhouette(km.res$cluster, distance_matrix)
  sil[i] <- mean(ss[, 3])
}
plot(1:k_max, sil, type = "b", pch = 20,col="black",frame = FALSE, 
  xlab ="Number of clusters (k)", 
  ylab="Average silhouette width (fit of data within its cluster)", 
  main="Optimal number of clusters")
abline(v = which.max(sil), lty = 2, col="red")

data\u no\u demographics能否提供数据集
data\u no\u demographics
?您可以使用
dput(data\u no\u demographics)获得可复制/粘贴的版本
按照您发布数据的方式,将其复制到我的R会话中会非常烦人。您是否尝试过
dput
命令?我尝试过,但输出太大,无法发布到此处如何将
dput
应用到子集。例如
dput(数据编号人口统计[1:6,]))
用于前6行。我没有经常使用此功能,但上面的输出应该是怎样的?您能提供数据集
数据\u no\u人口统计信息吗
?您可以使用
dput(数据\u no\u人口统计信息)获得可复制/粘贴的版本
按照您发布数据的方式,将其复制到我的R会话中会非常烦人。您是否尝试过
dput
命令?我尝试过,但输出太大,无法发布到此处如何将
dput
应用到子集。例如
dput(数据编号人口统计[1:6,]))
对于前6行。我没有太频繁地使用此函数,但上面的输出应该是怎样的?谢谢,正因为如此,我知道在循环中使用缩放数据集时会出现问题。但是,我很担心,因为我想使用缩放数据集,因为与使用dist(数据)相比,它会给出不同的最佳数字,我的印象是,使用缩放数据可能更适合此分析,因为使用
剪影(km.res$cluster,scale(data\u no\u demographics))
的数据存在很大的偏差。另一种选择是
剪影(km.res$cluster,dist(scale(data\u no\u demographics)))
给出了几乎相同的结果
剪影
的文档说明,如果它与两个参数一起使用,则第二个参数需要是一个。
量表(数据统计)
是您的数据的重新缩放版本。它与您的数据具有相同的尺寸,因此通常不是正方形。由于距离矩阵总是正方形,这不可能是正确的。非常感谢您,Gregor!谢谢,因此,我知道在循环中使用缩放数据集时会出现问题。但是,我担心,因为我想与使用dist(数据)相比,使用缩放数据集给出了不同的最佳数量,我一直认为使用缩放数据可能更适合此分析,因为使用
轮廓(km.res$cluster,scale(data_no_demographics))的数据中存在如此多的偏斜
肯定是不正确的。另一种选择是
剪影(km.res$cluster,dist(scale(data\u no\u demographics))
这给出了几乎相同的结果
剪影
的文档说明,如果它与两个参数一起使用,第二个参数需要是一个。
比例(data\u no\u demographics)
是您的数据的重新缩放版本。它与您的数据具有相同的尺寸,因此通常不是正方形。由于距离矩阵总是正方形,这不可能是正确的。非常感谢您,Gregor!