Advertisement
Guest User

Untitled

a guest
May 25th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. ---
  2. title: "Assignment 1"
  3. author: "Jaymon Veldkamp"
  4. date: "15 May 2018"
  5. output: html_document
  6. ---
  7.  
  8. ```{r setup, include=FALSE}
  9. knitr::opts_chunk$set(echo = TRUE)
  10. ```
  11.  
  12. #Part 1: Properties of the estimators of the standard error of the difference in means
  13.  
  14. ##1
  15. The population standard error of the difference in means
  16.  
  17. ##2
  18. ###a
  19. As $s_1^2$ and $s_2^2$ are unbiased variance estimates, $\sigma_1^2$ and $\sigma_2^2$ can be filled in for them.
  20.  
  21. Student's t-test statistic:
  22. ```{r}
  23. Sp <- sqrt(((10-1)*1+(200-1)*1)/(10+200-2))
  24.  
  25. Sp * sqrt(1/10+1/200)
  26. ```
  27.  
  28.  
  29. Welch's t-test statistic:
  30. ```{r cars}
  31.  
  32. sqrt(1/10 + 1/200)
  33.  
  34. ```
  35.  
  36. So the population parameter in the condition with $n_1=10$ and $\sigma_2^2 = 1$ is 0.324037.
  37.  
  38. ###b
  39.  
  40. MC_ttest_welch($S$, $n_1$, $n_2$, $\mu_1$, $\mu_2$, $\sigma_1^2$, $\sigma_2^2$)
  41.  
  42. Input:
  43. - $S$: integer defining the number of independent datasets to generate
  44. - $n_1$: the sample size of dataset 1
  45. - $n_2$: the sample size of dataset 2
  46. - $\mu_1$, $\sigma_1^2$: Values for the population parameters of population 1
  47. - $\mu_2$, $\sigma_2^2$: Values for the population parameters of population 2
  48.  
  49. Output: The bias, variance, MSE and RE for both standard errors using welch - and student t-test.
  50.  
  51. 1. Initialize output SE_welch and SE_student both as a vector of length S.
  52. 2. for s in 1:S
  53. A. generate $data1$ with sample size $n_1$ from $N(\mu_1,\sigma_1^2)$
  54. B. Generate $data2$ with sample size $n_2$ from $N(\mu_2, \sigma_2^2)$
  55. C. Obtain the sample standard error sample_welch using the Welch test
  56. D. Obtain the sample standard error sample_student using the Student t-test
  57. E. Store the sample standard errors:
  58. a. SE_welch[s] = sample_welch
  59. b. SE_student[s] = sample_student
  60. 3. Obtain the bias of SE_welch and SE_student and store it as welch_bias and student_bias
  61. 4. Obtain the variances of SE_welch and SE_student and store it as welch_var and student_var
  62. 5. Obtain the MSE of SE_welch and SE_student and store it as welch_MSE and student_MSE
  63. 6. Obtain the relative efficiency for SE_welch and SE_student and store it as RE
  64. 7. Return welch_bias, student_bias, welch_var, student_var, welch_MSE, student_MSE and RE.
  65.  
  66. ###c
  67. ```{r}
  68. set.seed(200)
  69.  
  70. true_SE = 0.324037 # See 2a
  71.  
  72. MC_ttest_welch = function(S=10000, n1, n2=200, mu1=0, mu2=1, var1=1, var2){
  73.  
  74. SE_welch <- c()
  75. SE_student <- c()
  76. #2
  77. for (i in 1:S){
  78.  
  79. #A
  80. data1 <- rnorm(n1, mu1, sqrt(var1))
  81.  
  82. #B
  83. data2 <- rnorm(n2, mu2, sqrt(var2))
  84.  
  85. #C
  86. sample_welch <- sqrt(var(data1)/n1 + var(data2)/n2)
  87.  
  88. #D
  89. Sp <- sqrt(((n1-1)*var(data1)+(n2-1)*var(data2))/(n1+n2-2))
  90. sample_student <- Sp * sqrt(1/n1 + 1/n2)
  91.  
  92. #E
  93. #a
  94. SE_welch <- c(SE_welch, sample_welch)
  95. #b
  96. SE_student <- c(SE_student, sample_student)}
  97.  
  98.  
  99.  
  100. #3
  101. true_SE <- sqrt(var1/n1 + var2/n2)
  102. welch_bias <- true_SE - mean(SE_welch)
  103. student_bias <- true_SE - mean(SE_student)
  104.  
  105. #4
  106. welch_var <- var(SE_welch)
  107. student_var <- var(SE_student)
  108.  
  109. #5
  110. welch_MSE <- welch_bias^2 + welch_var
  111. student_MSE <- student_bias^2 + student_var
  112.  
  113. #6
  114. RE <- welch_MSE/student_MSE
  115.  
  116. #7
  117. return(c(welch_bias, student_bias, welch_var, student_var, welch_MSE, student_MSE, RE))}
  118.  
  119. matrix = rbind(
  120. MC_ttest_welch(n1=10, var2=1), MC_ttest_welch(n1=100, var2=1), MC_ttest_welch(n1=200, var2=1),
  121. MC_ttest_welch(n1=10, var2=2), MC_ttest_welch(n1=100, var2=2), MC_ttest_welch(n1=200, var2=2),
  122. MC_ttest_welch(n1=10, var2=10), MC_ttest_welch(n1=100, var2=10), MC_ttest_welch(n1=200, var2=10))
  123.  
  124. dimnames(matrix) <- list(c("n1=10 var2=1", "n1=100 var2=1", "n1=200 var2=1",
  125. "n1=10 var2=2","n1=100 var2=2","n1=200 var2=2",
  126. "n1=10 var2=10","n1=100 var2=10","n1=200 var2=10"),
  127. c("bias welch", "bias student", "variance welch", "variance student", "MSE welch", "MSE student", "RE"))
  128.  
  129. matrix
  130.  
  131. ```
  132.  
  133. ###d
  134. The fact that the variances of the two populations should be the same.
  135.  
  136. ##3
  137. 1.
  138. - Sample size: low; variances: unequal; student t-test (more) biased
  139. - Sample size: low; variances: equal; welch test (more) biased
  140. - Sample size: large (with regard to the variance); no difference between welch and student.
  141.  
  142. 2.
  143. - Larger sample sizes decrease the variances.
  144. - Larger difference in variances (larger var2), increases the variance in both tests
  145.  
  146. 3.
  147. Student t-test when sample size is low and variances the same, welch test when the sample size is low and the variances are unequal. When the sample size is large ($n_1 \geq 200$) there is no difference.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement