First lets make so data to experiment with
set.seed(100)
example <- matrix(runif(47*30), nrow = 47)
example[sample(length(example), 250)] <- NA
Now we can calculate our means. The apply
function samples a random value from each row (!is.na
excludes NA
values), mean
gets the mean, and replicate
repeats this 10000 times.
exmeans <- replicate(10000, mean(apply(example, 1,
function(x) sample(x[!is.na(x)], 1))))
Confidence intervals can be computed two different ways. The first uses the example means as an empirical distribution and calculates the mean from that, the second uses a normal distribution to calculate the probablilities.
confint <- quantile(exmeans, c(0.025, 0.975))
confint <- qnorm(c(0.025, 0.975), mean = mean(exmeans), sd = sd(exmeans))
Next the plots you wanted
hist(exmeans)
abline(v = confint, col = "red")
Finally the p-value information. Once again we can eigther use an empirical distribution or a normal distribution. These provide p-values for the lower tails of the distribution, use 1 - result
for upper tails.
newvalue > confint[1] & newvalue < confint[2]
ecdf(exmeans)(newvalue)
pnorm(newvalue, mean = mean(exmeans), sd = sd(exmeans))
solved Take one sample per row at a time, compute mean, loop [closed]