-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrunSim.R
More file actions
43 lines (35 loc) · 925 Bytes
/
runSim.R
File metadata and controls
43 lines (35 loc) · 925 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
args = commandArgs(trailingOnly=TRUE)
n = as.integer(args[1])
reps = as.integer(args[2])
s = as.integer(args[3])
if(length(args) < 4){
d = "rnorm(n)"
} else {
d = args[4]
}
oFile <- paste("simresults/", "n_", n, "d_", d, ".txt", sep="")
## check if a given integer is prime
isPrime = function(n) {
if (n <= 3) {
return (TRUE)
}
if (any((n %% 2:floor(sqrt(n))) == 0)) {
return (FALSE)
}
return (TRUE)
}
## estimate mean only using observation with prime indices
estMeanPrimes = function (x) {
n = length(x)
ind = sapply(1:n, isPrime)
return (mean(x[ind]))
}
# Simulate `reps` replicates of sample size `n` from distribution `d` using seed `s`
simres = data.frame(est_mean_avg = double(reps), est_mean_prime = double(reps))
set.seed(s)
for (r in 1:reps){
x = eval(parse(text = d))
simres[r, 1] = mean(x)
simres[r, 2] = estMeanPrimes(x)
}
write.csv(simres, file = oFile, row.names = F)