Advertisement
st110036

StatComp_2020Q1Q2

Sep 25th, 2022
999
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.75 KB | None | 0 0
  1. #2020-Q1------------------------------------------------------------------------------------
  2. temp <- sum(1/c(3:12))
  3. c <- 1/temp  # c = 0.6237483
  4. p <- c/c(3:12)
  5. theta <- -sum(p*log(p)) # theta = 2.200209
  6.  
  7. sim_n <- 10000; n1 <- 20; n2 <- 50
  8.  
  9. set.seed(1234)
  10. N20 <- rmultinom(n = sim_n, size = n1, prob = p)
  11. # row-i means Xi & column-j means j-th simulation with n = 20
  12.  
  13.  
  14. g <- function(x, n){-(x/n)*log(x/n)}
  15. theta1_n1 <- apply(N20, 2, function(x) sum(g(x, n1), na.rm = TRUE) )
  16. # theta11<-rep(0,sim_n)
  17. # for (j in 1:sim_n) {
  18. #   for(i in 1:10){
  19. #     if(N20[i,j] != 0){
  20. #       theta11[j]<-[j] +g(N20[i,j], n1)
  21. #     }
  22. #   }
  23. # }
  24. # all.equal(theta11, theta1_n1)
  25. # # Note: all.equal vs identical
  26. # # the former one can tolerate small difference,
  27. # # the latter one must be exactly identical, including datatype and numerical error.  
  28. theta2_n1 <- theta1_n1 + 9/(2*n1)
  29.  
  30. theta1_bar_n1 <- mean(theta1_n1)
  31. theta2_bar_n1 <- mean(theta2_n1)
  32. bias1_n1 <- theta1_bar_n1 - theta
  33. bias2_n1 <- theta2_bar_n1 - theta
  34. SE1_n1<-sqrt(sum((theta1_n1 - theta1_bar_n1)^2)/sim_n)
  35. SE2_n1<-sqrt(sum((theta2_n1 - theta2_bar_n1)^2)/sim_n)
  36. R_mse1_n1<-sqrt(sum((theta1_n1 - theta)^2)/sim_n)
  37. R_mse2_n1<-sqrt(sum((theta2_n1 - theta)^2)/sim_n)
  38.  
  39. N50 <- rmultinom(n = sim_n,size = n2,prob = p)
  40. theta1_n2 <- apply(N50, 2, function(x) sum(g(x, n2), na.rm = TRUE) )
  41. # row-i means Xi & column-j means jth simulation with n = 50
  42.  
  43. # theta12<-rep(0,sim_n)
  44. # for (j in 1:sim_n) {
  45. #   for(i in 1:10){
  46. #     if(N50[i,j] != 0){
  47. #       theta12[j]<-theta12[j] +g(N50[i,j], n2)
  48. #     }
  49. #   }
  50. # }
  51. theta2_n2 <- theta1_n2 + 9/(2*n2)
  52. theta1_bar_n2 <- mean(theta1_n2)
  53. theta2_bar_n2 <- mean(theta2_n2)
  54. bias1_n2 <- theta1_bar_n2 - theta
  55. bias2_n2 <- theta2_bar_n2 - theta
  56. SE1_n2<-sqrt(sum((theta1_n2 - theta1_bar_n2)^2)/sim_n)
  57. SE2_n2<-sqrt(sum((theta2_n2 - theta2_bar_n2)^2)/sim_n)
  58. R_mse1_n2<-sqrt(sum((theta1_n2 - theta)^2)/sim_n)
  59. R_mse2_n2<-sqrt(sum((theta2_n2 - theta)^2)/sim_n)
  60.  
  61. result<-{data.frame("n" = c(n1," "," ",n2," "," "),
  62.                     "Mean estimate" = c(" ",theta1_bar_n1,theta2_bar_n1," ",theta1_bar_n2,theta2_bar_n2),
  63.                     "Bias" = c(" ",bias1_n1,bias2_n1," ",bias1_n2,bias2_n2),
  64.                     "Sample SE" = c(" ",SE1_n1,SE2_n1," ",SE1_n2,SE2_n2),
  65.                     "Root MSE" = c(" ",R_mse1_n1,R_mse2_n1," ",R_mse1_n2,R_mse2_n2),
  66.                     row.names = c(" ","theta1_hat","theta2_hat","  ","theta1-hat","theta2-hat"))
  67. }
  68. result
  69. # write.csv(result,"C:/Users/**Username**/Desktop/result.csv")
  70.  
  71. #2020-Q2------------------------------------------------------------------------------------
  72. replicate <- 5000
  73. n <- 30
  74. set.seed(1234)
  75. X <- matrix(rnorm(replicate*n), ncol=n, nrow=replicate)
  76. # Theoretically, row and col should switch due to the matrix generating order in r.
  77. # But this way(nrow is replicate) is more easily to understand.
  78. Y <- X^2
  79. a <- rep(0, 6)
  80. # Q2-(1) #######
  81. temp <- rowSums(X[,19:20]) # alternative: apply(X[,19:20],1,sum)
  82. a[1]<- length(temp[temp < 11/29])/replicate # 0.616000
  83. # Q2-(2) #######
  84. temp <- apply(Y[,1:11],1, function(x) sum(x))
  85. temp <- sqrt(temp)
  86. a[2]<-quantile(temp, prob= 0.9) # 4.14688
  87. # Q2-(3) #######
  88. temp1 <- apply(Y[,1:11], 1, function(x) sum(x))
  89. temp2 <- apply(Y[,1:29], 1, function(x) sum(x))
  90. temp <- temp1/temp2
  91. a[3]<-median(temp) # 0.3645801
  92. # Q2-(4) #######
  93. qnorm(0.75) # 0.6744898
  94. temp <- apply(X, 1, function(x) quantile(x, prob= 0.75))
  95. a[4]<- mean(temp) # 0.6439604
  96. # Q2-(5) #######
  97. qf(0.95,1,1) # 161.4476
  98. qbeta(0.95, shape1 = 1/2, shape2 =1/2)
  99. temp <- (Y[,1])/(Y[,2])
  100. a[5]<- quantile(temp, prob = .95) # 177.0137
  101. # Q2-(6)) #######
  102. temp1 <- X[,1]
  103. temp2 <- apply(X[,2:17], 1, function(x) sqrt(sum(x^2)))
  104. temp <- temp1<temp2
  105. a[6]<- length(temp)/replicate # 1
  106.  
  107.  
  108. data.frame(Q = c(1:6), Ans = a)
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement