lundi 25 mai 2020

sampling random values each iteration

I have some simulated data, on top of the data I add some noise to see how the noise affects my data for further analyses. I created the following function

create.noise <- function(n, amount_needed, mean, sd){
      set.seed(25)
      values <- rnorm(n, mean, sd)

      returned.values <- sample(values, size=amount_needed)    
}

I call this function in the following loop:

dataframe.noises <- as.data.frame(noises) #i create here a dataframe dim 1x45 containing zeros

for(i in 1:100){         
    noises <- as.matrix(create.noise(100,45,0,1))
    dataframe.noises[,i] <- noises
    data_w_noise <- df.data_responses+noises
    Estimators <- solve(transposed_schema %*% df.data_schema) %*% (transposed_schema %*% data_w_noise)
    df.calculated_estimators[,i] <-Estimators
}

The code above always returns the same values, one solution I tried is sending i as parameter(which i think isn't correct) for each iteration I add i to the set.seed(25+i) This gives me a unique value for each iteration, butas mentioned I don't think that this is the correct way to go with it.




Aucun commentaire:

Enregistrer un commentaire