Monday, September 21, 2020

Mod 5: Random Variables & Probability Distributions

Mod-5.utf8


Set environment

env <- c("dplyr")
lapply(env, library, character.only = 1)


Part A


Create dataset

data1 <- c(8,14,16,10,11) ; data1
## [1]  8 14 16 10 11


  1. Compute population mean & standard deviation
pop_mu <- mean(data1)
pop_sd <- sd(data1)

Mean is 11.8 and the standard deviation is 3.1937439

fun_calc <- function(trials){

    
# ---------- get samples
    
# sampling and replicate
s1_rs <- replicate( trials, sample(data1,2,replace=1) )
# mean & sd
s1_xbar <- apply(s1_rs,2,mean)
s1_sd <- apply(s1_rs,2,sd)
# function for standard error of the mean
fun_sem <- function(x) sd(x)/sqrt(length(x))
s1_sem<- apply(s1_rs, 2, fun_sem)
# format table
s1_tbl <- as.data.frame( rbind(s1_rs, s1_xbar, s1_sd, s1_sem) )
colnames(s1_tbl) <- c( paste("Trial ", seq(trials) ) )
rownames(s1_tbl) <- c("observation 1","observation 2","xbar", "std dev",
                      "std error")
# view filter
if(trials <= 4){
    viewTbl <- s1_tbl
} else {
    test <- data.frame("-----")
    colnames(test)<-   "-----"
    viewTbl <- bind_cols(s1_tbl[, 1:2], test, s1_tbl[, (trials-1):trials] )
}
# view table
print(viewTbl)


# ---------- roll up

# calculate sampling values - population std error mean is NA
pop_sem <- NA
rs_xbar <- mean(s1_rs)
rs_sd <- sd(s1_rs)
rs_sem <- sd(s1_rs) / sqrt(length(s1_rs))
# assemble table
pop_rollUp <- as.data.frame( rbind(pop_mu, pop_sd, pop_sem) )
rs_rollUp <- as.data.frame( rbind(rs_xbar, rs_sd, rs_sem) )
rollup <- bind_cols(pop_rollUp, rs_rollUp  )
colnames(rollup) <- c("Population","Samples")
rownames(rollup) <- c("mean/xbar","std dev","std err")
# view table
print(rollup)
}
  1. Random sample(s) of 2 from the population - 1 trial(s)
  2. Compute the mean & standard deviations
  3. Comparing the population and the samples
fun_calc(trials)
##               Trial  1
## observation 1 11.00000
## observation 2 14.00000
## xbar          12.50000
## std dev        2.12132
## std error      1.50000
##           Population  Samples
## mean/xbar  11.800000 12.50000
## std dev     3.193744  2.12132
## std err           NA  1.50000


  1. Random sample(s) of 2 from the population - 100 trial(s)
  2. Compute the mean & standard deviations
  3. Comparing the population and the samples
fun_calc(trials)
##                Trial  1  Trial  2 ----- Trial  99 Trial  100
## observation 1 10.000000 11.000000 ----- 14.000000   11.00000
## observation 2  8.000000 16.000000 ----- 16.000000   14.00000
## xbar           9.000000 13.500000 ----- 15.000000   12.50000
## std dev        1.414214  3.535534 -----  1.414214    2.12132
## std error      1.000000  2.500000 -----  1.000000    1.50000
##           Population    Samples
## mean/xbar  11.800000 11.8000000
## std dev     3.193744  2.8106045
## std err           NA  0.1987397


Part B


Suppose that the sample size n = 100 and the population proportion p = 0.95.


  1. Does the sample proportion p have approximately a normal distribution? Explain.

No.

One rule states: "the sample size is considered large enough if the expected number of successes and failures are both at least 10: \(np \ge 10\) and \(n(1-p) \ge 10\) .

Given the data : \(np \ge 10\) = TRUE and \(n(1-p) \ge 10\) = FALSE

  1. What is the smallest value of n for which the sampling distribution of p is approximately normal?

200.

Solving each sub-rule for n means n \(\ge\) 10.5263158 and n \(\ge\) 200 .



GitHub

Related file(s) can be found at Git Me

No comments:

Post a Comment