如何遍历参数值,运行函数和保存结果

问题描述 投票:0回答:1

我将模拟写入函数,以便可以手动设置参数值并使用这些参数值运行多次模拟。为了了解不同的设置如何影响模拟结果,我一直在手动更改参数值,运行模拟并保存输出。我已经反复这样做,并将输出数据绑定在一起以进行分析/可视化,但是如果我可以自动执行此过程,将会更加方便。

我如何遍历参数值,运行仿真并将所有结果保存在单个数据框中?


这里是我的代码看起来像:

#### load libraries ####

library(plyr)
library(igraph)

#### set parameters N and StDv ####

N <- 10 

StDv <- 0.1

#### my model to be simulated, written as a function ####
myModel <- function(){

  #generate small world network, netSim, for the agents
  netSim <- sample_smallworld(dim = 1, nei = 1, size = N, p = 0.1) 

  #retrieve an adjacency matrix from net
  adjMatrix <- as.matrix(as_adjacency_matrix(netSim, names = TRUE, edges = FALSE)) 

  #create dataframe with numbered agents and assigned prior  
  data <- data.frame("agent" = c(1:N),
                     "t0" = rnorm(N, mean = 0.5, sd = StDv))

  #simulate communication and in the network for 5 rounds

  #round 1
  data$t1 <- with(data, ifelse(rowSums(adjMatrix) > 0,  
                               0.75 * t0 + (1-0.75) * (adjMatrix %*% t0 / rowSums(adjMatrix)), 
                               t0)) 

  #round 2
  data$t2 <- with(data, ifelse(rowSums(adjMatrix) > 0,  
                               0.75 * t1 + (1-0.75) * (adjMatrix %*% t1 / rowSums(adjMatrix)), 
                               t1)) 

  #round 3
  data$t3 <- with(data, ifelse(rowSums(adjMatrix) > 0,  
                               0.75 * t2 + (1-0.75) * (adjMatrix %*% t2 / rowSums(adjMatrix)), 
                               t2)) 

  #round 4
  data$t4 <- with(data, ifelse(rowSums(adjMatrix) > 0, 
                               0.75 * t3 + (1-0.75) * (adjMatrix %*% t3 / rowSums(adjMatrix)), 
                               t3)) 

  #round 5
  data$t5 <- with(data, ifelse(rowSums(adjMatrix) > 0,  
                               0.75 * t4 + (1-0.75) * (adjMatrix %*% t4 / rowSums(adjMatrix)), 
                               t4)) 


  #calculate measures of interest
  colResponses <- colMeans(data[2:7])
  colErrorSq <- (colResponses-1)^2
  variance <- as.vector(sapply(data[2:7], function(i)
    var(i)))
  data2 <- data[2:7] 
  data2 <- (data2-1)^2
  avgIndErrSq <- colMeans(data2)
  rm(data2)

  #bind together output 
  Output <- data.frame("N" = N,
                       "StDv" = StDv,
                       "Time" = c("t0", "t1", "t2", "t3", "t4", "t5"),
                       "Collective.Response" = colResponses,
                       "Collective.Error.Squared" = colErrorSq,
                       "Variance" = variance,
                       "Avg.Ind.Error.Squared" = avgIndErrSq)

}

#### Simulate my model by running the function 100 times and saving the results as "myResults" ####
myResults <- ldply(1:100, function(i) data.frame(Iteration = i, myModel())) 

我拥有要在向量中浏览的所有N值:N_values <- c(10, 20, 40, 80)

以及我想在向量中浏览的所有StDv值:StDv_values <- c(0.05, 0.1, 0.25, 0.5)

是否有办法遍历NStDv的每种组合,运行模拟,并将结果保存在单个数据框中?

r loops automation parameter-passing simulation
1个回答
0
投票

我建议使用for循环浏览您的选项。嵌套的for循环应在这些向量的所有值和组合之间循环。

#Loop through all N values in vector
for (i in 1:length(N_values)) {

    #Loop through all StDev values in vector for each 
    #iteration of all N values
    for (i in 1:length(StDv_values) {

        MyModel <- insert your model here... etc...
    }
}

如果可以的话,你在哪里有#bind together output和代码:

Output <- data.frame("N" = N,
                 "StDv" = StDv,
                 "Time" = c("t0", "t1", "t2", "t3", "t4", "t5"),
                 "Collective.Response" = colResponses,
                 "Collective.Error.Squared" = colErrorSq,
                 "Variance" = variance,
                 "Avg.Ind.Error.Squared" = avgIndErrSq)

您正在创建一个数据框,但是我看不到它绑定了任何东西。

为了汇编您的所有数据,我建议如下所示:

1)在for循环之外初始化NULL变量2)每次迭代将所有新的Output data.frame值插入CompiledDF变量。

CompiledDF = NULL

#Loop through all N values in vector
for (i in 1:length(N_values)) {

    #Loop through all StDev values in vector for each 
    #iteration of all N values
    for (i in 1:length(StDv_values) {

        MyModel <- insert your model here... etc...


        Output <- data.frame(etc...
                                   )

        CompiledDF <- rbind(CompiledDF, Output)


    }
}
© www.soinside.com 2019 - 2024. All rights reserved.