如何为多个参数运行函数,返回输出并将其存储在单个数据表中? [英] How do I run a function for multiple parameters, return an output and have this in a single data table?

查看:45
本文介绍了如何为多个参数运行函数,返回输出并将其存储在单个数据表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了用于为给定参数集计算值的代码,该代码适用于单个参数集.

I have developed code that calculates a value for a given set of parameters, this works for a single set of parameters.

library(spatstat)
library(ggplot2)
library(dplyr)
library(tidyr)

#Generating a clustered landscape
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()

#Calculating a metric for clustering


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.我想使用radiusCluster和randmod作为我的变量,并对一组这些变量运行此实验.首先,生成所需的数据表.

So I can say for radiusCluster of 100 and randmod of 0, I get a kk_mean of "value". I want to use radiusCluster and randmod as my variables and run this experiment for a set of these variables. I begin by generating the data table that I want.


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.

Here r is randmod, rho is radiusCluster and sigma is kk_mean.

然后,我创建上述代码的功能,以生成聚类景观并计算指标.

Then I create a function of the above code for generating the clustered landscape and calculating the metric.

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,但是,这不起作用.

I then try running cluster_function for a set of parameters, however, this does not work.

cluster_function(dim <- 2000,
                       lambdaParent <-.02,
                       lambdaDaughter<-30,
                      hosts<-900,
                      randmod<-0,
                      radiusCluster<-0)

参数是在全局环境中定义的,但是没有任何反应.因此,我决定从该函数中删除landscape和ggplot命令,并将该函数调用为输出.然后希望输出将是我在cds中生成的坐标的数据帧,并且可以在ppp()函数中使用并可以绘图.

The parameters are defined in the global environment but nothing happens. So I decide to remove the landscape and ggplot command from the function and call the function to an output. Then hopefully the output will be data frame of the co ordinates that I generated in cds and can be used in a ppp() function and be plottable.

output<-cluster_function(dim <- 2000,
                       lambdaParent <-.02,
                       lambdaDaughter<-30,
                      hosts<-900,
                      randmod<-0,
                      radiusCluster<-0)

输出为数字(空).如何使该函数适用于cluster_function()中的参数,是否可以针对多个参数运行此函数?我在想一些类似的事情:

Output is numeric (empty). How can I get the function to work for the parameters in the cluster_function() and is it possible to run this for multiple parameters? I was thinking something along the lines of:

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"])

推荐答案

然后我尝试为一组参数运行cluster_function,但是,这不起作用

I then try running cluster_function for a set of parameters, however, this does not work

似乎对我有用;)您是否要打印ggplot?您可以添加 p<-ggplot(...),然后添加 print(p)进行查看(您可能需要刷新绘图查看器...).

It looks like it's working to me ;) Do you want the ggplot to be printed? You can addp <- ggplot(...) followed be print(p) to see it (you may need to refresh the plot viewer...).

输出为数字(空).如何使该功能正常工作

Output is numeric (empty). How can I get the function to work

添加明确的回报: return(cds)

您当然可以多次运行该函数. for 循环有效,或者您可以签出 purrr :: pmap() mapply().祝你好运!

And you can of course run the function multiple times. A for loop works, or you could check out purrr::pmap() or mapply(). Good luck!

这篇关于如何为多个参数运行函数,返回输出并将其存储在单个数据表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆