Replicate simulations and For Loop in R

时间:2019-04-16 23:07:21

标签: r

Hi guys sorry I’m a total beginner in R.

I have an Excel table:

Fund count
1 1961
2 2086
3 453
...

What I’m trying to do is: For each fund I would like to run 1000 simulations where I draw random numbers from range 1001:24999 as many times as the count for that fund. Then, work out how many random numbers are less than 6000. Last, obtain the average across the 1000 simulations.

After importing the file into RStudio, I have written so far:

#count for the first fund in the table
z <- replicate(1000, {1001:24449, replace=T, size=1961})
count<-length(which(z>6000))
mean(count)

I seem to be able to use the above for one fund and I know Im supposed to use a for loop for all funds.

I’m completely novice any feedback would be appreciated!!!

1 个答案:

答案 0 :(得分:1)

You can do the following:

For every count value in your data.frame, draw as many samples from 1001:24999 with replacement, and return the number of sampled values that are < 6000; repeat Nsim = 1000 times.

Nsim <- 1000
lst <- replicate(Nsim, lapply(
    apply(df, 1, function(x) sample(1001:24999, x["count"], replace = T)),
    function(x) sum(x < 6000)))

Store in matrix with Nsim columns and as many rows as there are rows in your original data.frame.

mat <- matrix(unlist(lst), ncol = Nsim)

Calculate the mean for every row.

rowMeans(mat)
#[1] 409.215 433.868  93.973

Sample data

df <- read.table(text =
    "Fund count
1 1961
2 2086
3 453", header = T)