lundi 20 mars 2017

writing empirical distribution function in R - simulation

I am trying to write the function in R, but keep getting an error. Within 1 simulation run I generate random values from 2 intevals - in order to generate 2 different output values.

  • se.m if the input parameter lies within [0, 1]
  • se.st if the input parameter lies wiothin [1, 5]

(floating point in neglegible)

Then, these randomly generated values are used as input in the following function:

enter image description here

This is the code I have used:

fuchs08 <- function(n){
  x.m=se.m=x.st=se.st=NULL 
  for(i in 1:n){
    x.m[i] <- ifelse (runif(n = 1, min = 0, max = 1) < 0.33, 0, 0.12 * (x.m[i]^2) - 0.04 * x.m[i])
    x.st[i] <- ifelse (runif(n = 1, min = 1, max = 5) < 3.06, 0.12 * (x.m[i]^2) - 0.04 * x.m[i], 1))
  }
  return(cbind(se.m, se.st))
}

I dont get any results. I believe the error is in the ifelse statement, but cannot find a solution to it.

> fuchs08(1)
NULL

Any suggestions how to fix this?

Aucun commentaire:

Enregistrer un commentaire