so have 10 parameters, 7 fixed , 3 varying using seq. each varying parameter has 10 possibilities. right create empty data frame , fill after going through bunch of functions , generating output each combination of parameters. there 1000 (10*10*10) possibilities. right use nested loops. lets m,g, , x varying parameters. here example.
m.c <- seq(1,10, by=1) m.i <- seq(1,10, by=1) * 0.5 <- .5 b <- 1 c <- .5 gg <- seq(.02,.2, by=.02) n <- 7 r <- .25 alpha <- 2 dt <- 1 x <- seq(.01,.1, by=.01) intervention.data <- data.frame(intervention = numeric()) parameter.data <- data.frame(m=numeric(), g=numeric(), x=numeric()) a.c = function(m = m.c,a,b,c,g,n,r,alpha,dt,x) { 1 - exp(-dt*(1/(alpha*dt)*log(1+(alpha*b*dt*m*a^2*c*x*exp(-g*n))/(a*c*x+g)))) } a.i = function(m = m.i,a,b,c,g,n,r,alpha,dt,x) { 1 - exp(-dt*(1/(alpha*dt)*log(1+(alpha*b*dt*m*a^2*c*x*exp(-g*n))/(a*c*x+g)))) } (i in 1:length(mm)) { m = mm[i] (ii in 1:length(gg)) { g = gg[ii] (iii in 1:length(xx)) { x = xx[iii] parameter.data = rbind(parameter.data, data.frame(m=m, g=g, x=x)) a.c = a.c(m = m.c,a,b,c,g,n,r,alpha,dt,x) a.i = a.i(m = m.i,a,b,c,g,n,r,alpha,dt,x) intervention.effect= a.i/a.c intervention.data = rbind(intervention.data, data.frame( intervention = intervention.effect)) } } } all.intervention.data = cbind(parameter.data, intervention.data) what have works seems pretty inefficient have been trying find how use sapply or lapply have not been successful in understanding use them combos. made. appreciated.
you seem have lost mm in data, can not follow perfectly, better way vectorize:
all.data <- expand.grid(m.c = m.c,gg = gg,x = x) all.data$m.i <- all.data$m.c * 0.5 all.data$a.c <- a.c(m = all.data$m.c,a,b,c,all.data$gg,n,r,alpha,dt,all.data$x) all.data$a.i <- a.i(m = all.data$m.i,a,b,c,all.data$gg,n,r,alpha,dt,all.data$x)
Comments
Post a Comment