Advertisement
Guest User

Untitled

a guest
May 29th, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. #! /bin/Rscript
  2.  
  3. run_em <- function(data, cutoff = 1.50, theta_init = 0.5, iter = 100, tolerance = 1e-4) {
  4. missing = data[data == cutoff]
  5. observed = data[data != cutoff]
  6. n1 = length(observed)
  7. n2 = length(missing)
  8. n = n1 + n2
  9. theta_current = theta_init
  10. theta_new = 0
  11. for (i in 1:iter) {
  12. theta_current = theta_new
  13. x_bar = mean(observed)
  14. theta_new = n1/n * x_bar +
  15. n2/n * theta_current +
  16. n2/n *
  17. dnorm(cutoff - theta_current)/(1 - pnorm(cutoff - theta_current))
  18. if(theta_new - theta_current <= tolerance) {
  19. break()
  20. }
  21. }
  22. cat("theta is ", theta_new)
  23. }
  24.  
  25. data <- c(2.01,0.74,0.68,1.50,1.47,1.50,1.50,1.52,0.07,-0.04,-0.21,0.05,-0.09,0.67,0.14)
  26. run_em(data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement