
David B. answered 04/26/21
Math and Statistics need not be scary
Sounds like a homework problem or an exam. Well, here are the parts needed to answer it. The student can then use these parts to get the final answer.
- a) format for random normal function is: rnorm(n, mean = 0, sd = 1) where n is number of numbers to be generated. mean is the mean for the sample, and sd = standard deviation
b) basic format for histogram is: hist(x) where x = vector with numbers in it
the following example would generate 1000 samples from a population of mean 20 and standard deviation of 5 then plot a histogram
x<-rnorm(1000,20,5)
hist(x)
2,a) the 'for loop' has the following format : for ( i in b:e) { executable code }
where i is the index which increments from b to e
b) the sample function will pull samples from a population and put them in a named variable. its format is:
sample(x, size, replace = FALSE). Where x is name of variable/vector with values to sample. size is the number of samples to take, replace = indicates if the sampling will be done with or without replacement.
c). the mean function will generate the mean of the values in a vector variable. The format is:
mean(x)
d) to create a vector of a series of means, you will use the concatenate function c(x1,x2,...x4)
an example of code that would use these to put together a vector with the mean of 25 random samples is:
(note: since it wasn't specified , we do sampling with replacement to maintain population)
x1 = NULL
for (i in 1:25) {
s = sample(x,40, replace=TRUE)
m = mean(s)
x1<- c(x1,m)
}
hist(x1)
3 For this question the new function you will need will be standard deveation. sd().
sd(x). where x is vector variable. calculates standard deviation of the values in x
Remember , if you followed the above examples you would have two vectors of numbers. X. is a vector of 1500 random numbers. X1 is a vector of the MEANS of samples of X. The standard deviation of X would be the standard deviation of the population. The standard deviation of X1 would be the standard error of the sample means. They will vary. an example of how to examine the mean and standard deviation and distribution of these two different vectors is as follows
mean(x)
sd(x)
mean(x1)
sd(x1)
hist(x)
hist(x1)

David B.
Thank you for your response, I hope my breakdown of the individual functions helped you understand the code. The only tricky thing is the loop. I find many current courses give examples but don't explain the logic behind the loops. Some teachers like to show off and give code that they don't break down. For example, the entire for loop could be done in one line (see below) , but I broke it down into individual lines so the reader could learn how to work within a loop. for (i in 1:25) x1<- c(x1,mean(sample(x,40, replace=TRUE)))04/26/21
Leo N.
Thank you this is a homework assignment that I was never able to understand/solve and I wanted to understand before the final exam. Thanks!04/26/21