I want to generate all samples of size n = 2, with replacement, from the following data and then print the sampling distribution of sample means. I want to know that is there any standard R library for this?
This is what I have tried.
library(graphics)
data <- c(10, 12, 14, 16); n <- length(data)
mp <- vector(mode="numeric", length = 100)
for(i in data) {
for(j in data) {
mu <- mean(c(i, j))
mp[mu] <- mp[mu] + 1
tot <- tot + 1
}
}
X_bar <- vector()
P_Xbar <- vector()
for(i in 1:length(mp)) {
if(mp[i]) {
X_bar <- c(X_bar, i)
P_Xbar <- c(P_Xbar, mp[i] / tot)
}
}
tab <- data.frame(x <- X_bar, y <- P_Xbar); tab
barplot(tab$y * tot, names.arg=tab$x)
I think that I am doing lot of unnecessary stuff, can we do this using any standard R library ?
To get samples of size n with replacement
dataset = 1:100
sample(dataset, size = 2, rep=T)
To get means for N samples
N = 1000
means = replicate(N, mean(sample(dataset, 2, rep=T)))
To plot means
hist(means)
Ok, I see from your comment you want all possible n=2 permutations of the data. This can be achieved with:
library(gtools)
x = permutations(n=3, r=2, v=1:3, repeats.allowed=T)
# n = size of sampling vector
# r = size of samples
# v = vector to sample from
This gives you a matrix with each possible permutation including repeats:
[,1] [,2]
[1,] 1 1
[2,] 1 2
[3,] 1 3
[4,] 2 1
[5,] 2 2
[6,] 2 3
[7,] 3 1
[8,] 3 2
[9,] 3 3
To calculate means of this vector you can use:
rowMeans(x)