R 如何循环参数值、运行函数和保存结果
我将模拟写入函数,以便手动设置参数值,并使用这些参数值多次运行模拟。为了了解不同设置如何影响模拟结果,我一直在手动更改参数值、运行模拟并保存输出。我已经多次这样做,并将输出数据绑定在一起进行分析/可视化,但如果我可以自动化此过程,则会更加方便 如何循环参数值、运行模拟并将所有结果保存在单个数据帧中R 如何循环参数值、运行函数和保存结果,r,loops,automation,parameter-passing,simulation,R,Loops,Automation,Parameter Passing,Simulation,我将模拟写入函数,以便手动设置参数值,并使用这些参数值多次运行模拟。为了了解不同设置如何影响模拟结果,我一直在手动更改参数值、运行模拟并保存输出。我已经多次这样做,并将输出数据绑定在一起进行分析/可视化,但如果我可以自动化此过程,则会更加方便 如何循环参数值、运行模拟并将所有结果保存在单个数据帧中 以下是我的代码的一种感觉: #### load libraries #### library(plyr) library(igraph) #### set parameters N and St
以下是我的代码的一种感觉:
#### 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()))
#####加载库####
图书馆(plyr)
图书馆(igraph)
####设置参数N和StDv####
N我建议使用for循环遍历您的选项。嵌套for循环应循环遍历这些向量的所有值和组合
#Loop through all N values in vector
for (i in 1:length(N_values)) {
N = N_values[i]
#Loop through all StDev values in vector for each
#iteration of all N values
for (j in 1:length(StDv_values) {
StDv = StDv_values[j]
MyModel <- insert your model here... etc...
}
}
您正在创建一个数据帧,但我看不到它与任何东西绑定
对于所有数据的汇编,我建议如下:
1) 在for循环外部初始化空变量
2) 为每次迭代将所有新的Output data.frame值插入CompiledDF变量
CompiledDF = NULL
#Loop through all N values in vector
for (i in 1:length(N_values)) {
N = N_values[i]
#Loop through all StDev values in vector for each
#iteration of all N values
for (j in 1:length(StDv_values) {
StDv = StDv_values[j]
MyModel <- insert your model here... etc...
Output <- data.frame(etc...
)
CompiledDF <- rbind(CompiledDF, Output)
}
}
CompiledDF=NULL
#循环遍历向量中的所有N个值
对于(1中的i:长度(N_值)){
N=N_值[i]
#循环遍历每个节点的向量中的所有StDev值
#所有N值的迭代
对于(j in 1:长度(StDv_值){
StDv=StDv_值[j]
MyModel我建议使用for循环遍历您的选项。嵌套的for循环应该遍历这些向量的所有值和组合
#Loop through all N values in vector
for (i in 1:length(N_values)) {
N = N_values[i]
#Loop through all StDev values in vector for each
#iteration of all N values
for (j in 1:length(StDv_values) {
StDv = StDv_values[j]
MyModel <- insert your model here... etc...
}
}
您正在创建一个数据帧,但我看不到它与任何东西绑定
对于所有数据的汇编,我建议如下:
1) 在for循环外部初始化空变量
2) 为每次迭代将所有新的Output data.frame值插入CompiledDF变量
CompiledDF = NULL
#Loop through all N values in vector
for (i in 1:length(N_values)) {
N = N_values[i]
#Loop through all StDev values in vector for each
#iteration of all N values
for (j in 1:length(StDv_values) {
StDv = StDv_values[j]
MyModel <- insert your model here... etc...
Output <- data.frame(etc...
)
CompiledDF <- rbind(CompiledDF, Output)
}
}
CompiledDF=NULL
#循环遍历向量中的所有N个值
对于(1中的i:长度(N_值)){
N=N_值[i]
#循环遍历每个节点的向量中的所有StDev值
#所有N值的迭代
对于(j in 1:长度(StDv_值){
StDv=StDv_值[j]
MyModel我将编写一个辅助函数,用适当的值组合处理调用MyModel
的重复细节
runAll <- function(N_vec, StDv_vec){
f <- function(N, StDv){
ldply(1:100, function(i) data.frame(Iteration = i, myModel(N, StDv)))
}
vals <- expand.grid(N = N_vec, StDv = StDv_vec)
res <- Map(function(.N, .StDv){f(.N, .StDv)}, vals$N, vals$StDv)
res <- do.call(rbind, res)
row.names(res) <- NULL
res
}
N_values <- c(10, 20, 40, 80)
StDv_values <- c(0.05, 0.1, 0.25, 0.5)
res <- runAll(N_values, StDv_values)
dim(res)
#[1] 9600 8
我将编写一个辅助函数来处理使用适当的值组合调用myModel
的重复细节
runAll <- function(N_vec, StDv_vec){
f <- function(N, StDv){
ldply(1:100, function(i) data.frame(Iteration = i, myModel(N, StDv)))
}
vals <- expand.grid(N = N_vec, StDv = StDv_vec)
res <- Map(function(.N, .StDv){f(.N, .StDv)}, vals$N, vals$StDv)
res <- do.call(rbind, res)
row.names(res) <- NULL
res
}
N_values <- c(10, 20, 40, 80)
StDv_values <- c(0.05, 0.1, 0.25, 0.5)
res <- runAll(N_values, StDv_values)
dim(res)
#[1] 9600 8