Amanda R.
Amanda R.

Reputation: 297

Need Help Understanding R Code

I'm trying to find if (a) fewer than 62% or more than 74% of the sample means within one standard deviation of the expected value, or (b) fewer than 92% or more than 98% of the sample means within two standard deviations of the expected value.

Given that we have already set mu and sigma, and Finv is a quantile function. I was given the last two lines of code. Can someone please explain to me what they mean and what kind of output I should be getting? (Currently my only output is 0)

n.iterations <- 100000
n <- 10
xbar <- numeric(n.iterations)

for (i in 1:n.iterations){
  x <- sapply(runif(n), Finv)
   xbar[i] <- mean(x)
 }


mean((mu-1*sigma/sqrt(n) <= xbar) & (xbar <= mu+1*sigma/sqrt(n)))

mean((mu-2*sigma/sqrt(n) <= xbar) & (xbar <= mu+2*sigma/sqrt(n)))

Upvotes: 0

Views: 107

Answers (1)

Drey
Drey

Reputation: 3364

I'm a little bit puzzled by your question, because it askes about data "within standard deviation" but also asks about quantiles - which seems odd... and here is why

enter image description here

Consider the upper picture generated from the following code:

mymean <- 5
mysd   <- 2

curve(dnorm(x, mean = mymean, sd = mysd), from = -2, to = 12)
abline(v = mymean, col = "red", lwd = 2)

xtimessd = 1
abline(v = c(mymean - mysd*xtimessd, mymean + mysd*xtimessd), col = "blue", lwd = 1, lty = 2)
xtimessd = 2
abline(v = c(mymean - mysd*xtimessd, mymean + mysd*xtimessd), col = "cyan", lwd = 1, lty = 2)
xtimessd = 3
abline(v = c(mymean - mysd*xtimessd, mymean + mysd*xtimessd), col = "green", lwd = 1, lty = 2)

# 62th and 74th quantile
targetQunatiles <- qnorm(c(0.62, 0.75), mean = mymean, sd = mysd)
abline(v = targetQunatiles, col = "orange", lwd = 2, lty = 1)

Given your population mean and standard deviation the figure about shows the probability density function of a normal distribution.

The dotted lines are the "xtimes within sd" values. (There is really no magic, but it is related to the 68–95–99.7 rule).

On the other hand, if we look into the quantile function, i.e., in your example we are looking into values 62% and 74%, that can be computed by qnorm.

As you can see, based on your question "fewer than 62% or more than 74% of the sample means", you will exclude values between 5.610962 and 6.348980.

So, still, from your question I don't know what you are asking about the relation between the statement of "within sd" and "looking for quantiles" as both are independen of each other.

Upvotes: 2

Related Questions