Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. strata.samp <- function(df, n)
  2. {
  3. # strata.samp as function name
  4. # df and n are passed arguments
  5. # df is the data frame having the k columns as strata and
  6. # n is the sample size
  7.  
  8. # writing function for the stratified sampling
  9. # where we need to pass the strata as data frame and
  10. # sample size as arguments to
  11. # find the population mean, srs mean, stratified mean,
  12. # variance of srs mean,
  13. # variance of stratified sampling mean,
  14. # standard error of stratified sampling mean and
  15. # confidence interval of stratified sampling mean
  16.  
  17. # finding the number of strata
  18. k <- ncol(df)
  19.  
  20. st.size <- array() # making array of the strata sizes
  21. pop <- vector() # making a vector of all population
  22.  
  23. for (i in 1:k)
  24. {
  25. # finding the length of ith stratum
  26. st.size[i] <- length(na.omit(df[, i]))
  27. pop <- c(pop, na.omit(df[, i])) # merging the strata
  28. }
  29.  
  30. pop.size <- length(pop) # population size
  31.  
  32. samp.st.size <- array() # making array for sample strata sizes
  33. samp.mean.st <- array() # array of sample means of the strata
  34. var.samp.st <- array() # array of sample variances of strata
  35.  
  36. for (i in 1:k)
  37. {
  38. # finding sample size of ith stratum
  39. samp.st.size[i] <- round(n * st.size[i] / pop.size)
  40. # taking sample from ith stratum & storing them to temp
  41. set.seed(0000)
  42. temp = sample(na.omit(df[, i]), samp.st.size[i])
  43.  
  44. samp.mean.st[i] <- mean(temp) # sample mean of ith stratum
  45. var.samp.st[i] <- var(temp) # sample variance of ith stratum
  46. }
  47.  
  48. pop.mean <- mean(pop) # population mean
  49.  
  50. set.seed(0000)
  51. srs <- sample(pop, n) # simple random sampling without replacement
  52. mean.srs <- mean(srs) # finding srs mean
  53.  
  54. # finding the variance of srswor mean using formula
  55. var.srs.mean <- (1 - n / pop.size) * var(srs) / n
  56.  
  57. # finding stratified mean
  58. final.samp.mean.st <- weighted.mean(x = samp.mean.st, w = samp.st.size)
  59.  
  60. # finding the variance of stratified sampling mean
  61. # proportions of the sizes of strata and population size
  62. w <- st.size / pop.size
  63. # proportions of sample unit and population unit under strata
  64. f <- samp.st.size / st.size
  65.  
  66. # initialization because we need to
  67. # increment the variable var.st.mean
  68. var.st.mean <- 0
  69.  
  70. # loop for finding the variance of stratified
  71. # sampling mean to maintain the iterative sum
  72. # i is iterative variable
  73.  
  74. for (i in 1:k)
  75. {
  76. # formula to find the variance of
  77. # stratified sampling mean
  78.  
  79. # passing iterative integer variable i
  80. # within square brackets
  81. # which lies between 1 to k
  82. # to index the arrays
  83.  
  84. var.st.mean <- var.st.mean +
  85. w[i] ^ 2 * (1 - f[i]) * var.samp.st[i] / samp.st.size[i]
  86. }
  87.  
  88. se.st.mean <- sqrt(var.st.mean) # standard error
  89. margin.err.st.mean <- 1.96 * se.st.mean # margin of error
  90.  
  91. # lower value at 95% confidence interval
  92. lower.confi <- final.samp.mean.st - margin.err.st.mean
  93.  
  94. # higher value at 95% confidence interval
  95. higher.confi <- final.samp.mean.st + margin.err.st.mean
  96.  
  97. # 95% confidence interval
  98. confi.interv <- list("at 95% Lower" = lower.confi,
  99. "at 95% Higher" = higher.confi)
  100.  
  101. # making the list of all expected answers
  102. list.results <- list("Population Mean" = pop.mean,
  103. "SRS Mean" = mean.srs,
  104. "Variance of SRS Mean" = var.srs.mean,
  105. "Stratified Sampling Mean" = final.samp.mean.st,
  106. "Variance of Stratified Sampling Mean" =
  107. var.st.mean,
  108. "Standard Error of Stratified Sampling Mean" =
  109. se.st.mean,
  110. "Confidence Interval of Stratified Sampling Mean" =
  111. confi.interv)
  112.  
  113. # returning the result as list
  114. return(list.results)
  115. }
  116.  
  117. # reading the given csv data as data frame
  118. # dat <- read.csv(file.choose())
  119. dat <- read.csv(file = "hk_sir_stratified_data_25_03_2019.csv")
  120. # dat # printing the data frame
  121.  
  122. # calling the function by it's name & passing the arguments
  123. strata.samp(dat, 10)
  124.  
  125. # reference
  126. cat("Coded by\nSFT\n1711024137\nHappy Codding!\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement