如何修复R聚类函数调用问题并实现多参数批量运行输出至数据表
R函数调用无输出且批量运行失败问题求助
我已经写好了一段能针对单组参数计算聚类指标的R代码,具体代码如下:
library(spatstat) library(ggplot2) library(dplyr) library(tidyr) # 生成聚类景观 dim <- 2000 radiusCluster<-100 lambdaParent<-.02 lambdaDaughter<-30 hosts<-900 randmod<-0 numbparents<-rpois(1,lambdaParent*dim) xxParent<-runif(numbparents,0+radiusCluster,dim-radiusCluster) yyParent<-runif(numbparents,0+radiusCluster,dim-radiusCluster) numbdaughter<-rpois(numbparents,(lambdaDaughter)) sumdaughter<-sum(numbdaughter) theta<-2*pi*runif(sumdaughter) rho<-radiusCluster*sqrt(runif(sumdaughter)) xx0=rho*cos(theta) yy0=rho*sin(theta) xx<-rep(xxParent,numbdaughter) yy<-rep(yyParent,numbdaughter) xx<-xx+xx0 yy<-yy+yy0 cds<-data.frame(xx,yy) is_outlier<-function(x){ x > dim| x < 0 } cds<-cds[!(is_outlier(cds$xx)|is_outlier(cds$yy)),] sampleselect<-sample(1:nrow(cds),hosts,replace=F) cds<-cds%>%slice(sampleselect) randfunction<-function(x){ x<-runif(length(x),0,dim) } randselect<-sample(1:nrow(cds),floor(hosts*randmod),replace=F) cds[randselect,]<-apply(cds[randselect,],1,randfunction) landscape<-ppp(x=cds$xx,y=cds$yy,window=owin(xrange=c(0,dim),yrange=c(0,dim))) ggplot(data.frame(landscape))+geom_point(aes(x=x,y=y))+coord_equal()+theme_minimal() # 计算聚类指标 kk<-Kest(landscape) plot(kk) kk_iso<-kk$iso kk_pois<-kk$theo kk_div_na<-kk_iso/kk_pois kk_div_0<-replace_na(kk_div_na,0) kk_mean<-round(mean(kk_div_0),3)
这段代码能正常针对单组参数(比如radiusCluster=100、randmod=0)计算出kk_mean这个聚类指标。
接下来我想把randmod和radiusCluster作为变量,批量运行多组实验,把结果存入提前生成的parameter_table数据表中:
random_parameter<-rep(c(0,.5,1),3) radiusCluster_parameter<-rep(c(100,300,600),each=3) Cluster_metric<-rep(NA,length(radiusCluster_parameter)) parameter_table<-data.frame(random_parameter,radiusCluster_parameter,Cluster_metric) colnames(parameter_table)<-c("r", "rho", "sigma")
这里r对应randmod,rho对应radiusCluster,sigma要用来存储计算得到的kk_mean。
于是我把之前的代码封装成了cluster_function函数:
cluster_function <- function (dim, lambdaParent, lambdaDaughter, hosts, randmod, radiusCluster) { numbparents <- rpois(1, lambdaParent * dim) xxParent <- runif(numbparents, 0 + radiusCluster, dim - radiusCluster) yyParent <- runif(numbparents, 0 + radiusCluster, dim - radiusCluster) numbdaughter <- rpois(numbparents, (lambdaDaughter)) sumdaughter <- sum(numbdaughter) theta <- 2 * pi * runif(sumdaughter) rho <- radiusCluster * sqrt(runif(sumdaughter)) xx0 = rho * cos(theta) yy0 = rho * sin(theta) xx <- rep(xxParent, numbdaughter) yy <- rep(yyParent, numbdaughter) xx <- xx + xx0 yy <- yy + yy0 cds <- data.frame(xx, yy) is_outlier <- function(x) { x > dim | x < 0 } cds <- cds[!(is_outlier(cds$xx) | is_outlier(cds$yy)), ] sampleselect <- sample(1:nrow(cds), hosts, replace = F) cds <- cds %>% slice(sampleselect) randfunction <- function(x) { x <- runif(length(x), 0, dim) } randselect <- sample(1:nrow(cds), floor(hosts * randmod), replace = F) cds[randselect, ] <- apply(cds[randselect, ], 1, randfunction) landscape<-ppp(x=cds$xx,y=cds$yy,window=owin(xrange=c(0,dim),yrange=c(0,dim))) ggplot(data.frame(landscape))+geom_point(aes(x=x,y=y))+coord_equal()+theme_minimal() kk<-Kest(landscape) plot(kk) kk_iso<-kk$iso kk_pois<-kk$theo kk_div_na<-kk_iso/kk_pois kk_div_0<-replace_na(kk_div_na,0) kk_mean<-round(mean(kk_div_0),3) }
但是调用这个函数的时候(比如cluster_function(dim <- 2000, lambdaParent <-.02, lambdaDaughter<-30, hosts<-900, randmod<-0, radiusCluster<-0)),完全没有任何反应。就算我把里面的绘图语句删掉,调用函数后也得不到任何输出。
现在我想请教大家两个问题:
- 如何修复这个函数的调用问题,让它能正常返回
kk_mean的值? - 如何实现针对
parameter_table里的多组参数批量运行,把计算得到的kk_mean存入sigma列中?
我原本设想的批量运行代码是这样的,但显然也有问题:
for (i in length(parameter_table)){ cluster_function(dim <- 2000, lambdaParent <-.02, lambdaDaughter<-30, hosts<-900, randmod<-parameter_table[i,"r"], radiusCluster<-parameter_table[i,"rho"]) }
内容的提问来源于stack exchange,提问作者OpenSauce
相关产品推荐
相关产品推荐

