Home > database >  Create m samples with size n Normal Distribution in R
Create m samples with size n Normal Distribution in R

Time:12-15

i have to create m=1450 samples of size n where n{100,200,300...5000} of an X population with normal distribution of mean 9,22 and variance=1.62^2. For each sample i need to create confidence intervals for the mean with the assumption that variance is unknown (significance level=0.98). Then for each n i have to calculate the mean of amplitud of the confidence intervals.

For the first part (create m samples of size n) i tried the following, without success:

sizes <- seq(100,5000,by=100)
m<-1450
normal=for (i in sizes){
replicate(1450, rnorm(i,9.22,sqrt(1.62^2), simplify=FALSE)
}

For the confidence intervals can i use something like the following?:

get.conf.int = function(x) t.test(x, conf.level = 0.98)$conf.int
conf.int = apply(normal, 2, get.conf.int)

Thanks

CodePudding user response:

assigning a for loop doesn't make sense in R terms, instead you can use lapply which'll return the entire output as a list or sapply which will try to convert to a array if possible. Here returning a list makes more sense. Note below I use lambda function \(x) = function(x) which were introduced in R 4.1.0

m <- 1450
sizes <- 1:50
size_factor <- 100
alpha <- 0.02
# Simulate outcomes
sims <- lapply(sizes * size_factor, \(x){
  replicate(m, rnorm(x, 9.22, 1.62))
})
# Find 1 - alpha confidence intervals
lapply(sims, \(x){
  # Find simulated means
  mus <- colMeans(x)
  # Find S.E of the simulated means (assuming same variance)
  s <- sd(mus)
  # Make confidence interval
  mean(mus)   c(-1, 1) * s * qnorm(1 - alpha/2)
})

CodePudding user response:

This strategy should do the trick (example simplified for better display).

sizes <- seq(5, 7, by=1)
samples <- lapply(sizes, \(n) replicate(10, rnorm(n, 9.22, sqrt(1.62^2)), 
                                        simplify=FALSE)) |>
  setNames(paste0('N', sizes))

Result

str(samples)
# List of 3
# $ N5:List of 10
# ..$ : num [1:5] 7.6 8.12 9.14 8.59 7.82
# ..$ : num [1:5] 9.76 7.07 8.02 9.14 9.53
# ..$ : num [1:5] 11.95 9.42 10.03 6.64 7.56
# ..$ : num [1:5] 8.03 7.98 10.19 11.83 7.42
# ..$ : num [1:5] 8.95 8.07 11.49 11.58 9.19
# ..$ : num [1:5] 8.26 8.7 11.18 6.11 7.67
# ..$ : num [1:5] 13.4 8.9 10.21 10.16 5.88
# ..$ : num [1:5] 10.75 8.47 4.86 9.44 8.05
# ..$ : num [1:5] 8.45 7.03 7.92 7.85 8.01
# ..$ : num [1:5] 10.93 10.33 10.34 8.18 6.62
# $ N6:List of 10
# ..$ : num [1:6] 7.52 9.3 8.18 7.14 8.86 ...
# ..$ : num [1:6] 6.92 11.06 7.51 8.98 9.37 ...
# ..$ : num [1:6] 10.28 10.14 7.43 11.91 11.92 ...
# ..$ : num [1:6] 9.5 8.62 9.62 7.71 9.73 ...
# ..$ : num [1:6] 10.12 10.05 10.8 9.38 7.75 ...
# ..$ : num [1:6] 10.07 9.97 10.58 8.52 9.67 ...
# ..$ : num [1:6] 5 8.59 9.74 8.86 8.05 ...
# ..$ : num [1:6] 10.04 9.8 10.33 8.36 6.83 ...
# ..$ : num [1:6] 8.37 6.94 10.02 8.78 8.85 ...
# ..$ : num [1:6] 11.52 9.14 9.96 10.61 10.54 ...
# $ N7:List of 10
# ..$ : num [1:7] 5.61 12.26 9 8.73 9.86 ...
# ..$ : num [1:7] 12.36 6.24 9.42 11.76 8.12 ...
# ..$ : num [1:7] 7.23 9.22 9.53 8.95 10.08 ...
# ..$ : num [1:7] 7.96 9.21 7.32 10.54 10.66 ...
# ..$ : num [1:7] 8.14 11.38 7.75 7.98 9.88 ...
# ..$ : num [1:7] 11.81 9.53 10.21 8.02 9.79 ...
# ..$ : num [1:7] 10.59 9.15 9.96 9.7 8.63 ...
# ..$ : num [1:7] 6.54 9.06 9.44 11.61 11.63 ...
# ..$ : num [1:7] 9.87 9.46 9.52 7.65 9.54 ...
# ..$ : num [1:7] 9.3 9.32 7.09 9.25 12.52 ...

Note:

>  R.version.string
[1] "R version 4.1.2 (2021-11-01)"
  • Related