jeudi 16 mars 2017

randomly select and execute a function from a list. Repeat the process and save the outputs

I have multiple functions that generate random numbers. All these functions describe the same prozess. I want to choose a function 100 times at random, execute it and save the outcomes (100 rows per function execution) in a table.

For the functions I have written the following code:

# AKBAS u.a. (2009)
akbas <- function(n){
  x.m=se.m=x.st=se.st=NULL 
  for(i in 1:n){
    print(x.m[i] <- runif(n = 1, min = 0, max = 1))
    se.m[i] <- 0.17 * (x.m[i]^2) - 0.03 * x.m[i]
    print(x.st[i] <- runif(n = 1, min = 1, max = 5))
    se.st[i] <- 0.17 * (x.st[i]^2) - 0.03 * x.st[i]
  }
  return(list(se.m, se.st))
}
se.akbas <- matrix(unlist(akbas(100)),100, 2)

# FUCHS u.a.(2007)
fuchs07 <- function(n){
  x.m=se.m=x.st=se.st=NULL #solves indexing problem
  for(i in 1:n){
    print(x.m[i] <- runif(n = 1, min = 0, max = 1))
    se.m[i] <- 0.11 * (x.m[i]^2) - 0.02 * x.m[i]
    print(x.st[i] <- runif(n = 1, min = 1, max = 5))
    se.st[i] <- 0.11 * (x.st[i]^2) - 0.02 * x.st[i]
  }
  return(list(se.m, se.st))
}
se.fuchs07 <- matrix(unlist(fuchs07(100)),100, 2)

# BELL AND GLADE (2004)
bell.glade <- function(n){
  x.m=se.m=x.st=se.st=NULL 
  for(i in 1:n){
    se.m[i] <- runif(n = 1, min = 0, max = 1)
    se.m[i] <- 0.2
    se.st[i] <- runif(n = 1, min = 1, max = 5)
    se.st[i] <- 0.5
  }
  return(list(se.m, se.st))
}
se.bell.glade <- matrix(unlist(bell.glade(100)),100, 2)

# BORTER (1999b,a)
borter <- function(n){
  x.m=se.m=x.st=se.st=NULL 
  for(i in 1:n){
    se.m[i] <- runif(n = 1, min = 0, max = 1)
    se.m[i] <- 0.1
    se.st[i] <- runif(n = 1, min = 1, max = 5)
    se.st[i] <- 0.5
  }
  return(list(se.m, se.st))
}
se.borter <- matrix(unlist(borter(100)),100, 2)

# FELL UND HARTFORD (1997)
fell.hartford <- function(n){
  x.m=se.m=x.st=se.st=NULL 
  for(i in 1:n){
    se.m[i] <- ifelse (runif(n = 1, min = 0, max = 1) < 0.25, 0.1, 0.4)
    se.st[i] <- ifelse (runif(n = 1, min = 1, max = 5) < 1.5, 0.4, 0.7)
  }
  return(list(se.m, se.st))
}
se.fell.hartford <- matrix(unlist(fell.hartford(100)),100, 2)

funktionen <- list(se.akbas, se.bell.glade, se.borter, se.fell.hartford, se.fuchs07)
murgang <- do.call(rbind, funktionen)

The final output should be in a long format - se.m and se.st stacked. I include an indicator column with values 1 for se.m and 2 for se.st. Here a small part of the output:

495           1  0.0228
496           1  0.0494
497           1  0.0158
498           1  0.0169
499           1  0.0357
500           1  0.0436
501           2  0.3459
502           2  2.7998
503           2  3.6571
504           2  3.1501
505           2  2.7418
506           2  0.5286

However I am stuck on the prozess of random function selection get.f. I was thinking of something like:

get.f <- function(m){
  sample(funktionen, 100, replace = TRUE)
} 

How do I get to save the results?

Any suggestion for simplyfying this prozess? I am sure there is a more efficient way, but I just haven't found it yet.

I am new to R and will highly appriciate any constructive comments and/or critique.




Aucun commentaire:

Enregistrer un commentaire