fork download
  1. # https://w...content-available-to-author-only...t.cc/bbs/R_Language/M.1540859231.A.848.html
  2.  
  3. theta <- 8 # 本例以 theta = 8 為例
  4. n.delta <- 500 # 設定方格在 x 和 y 的格數
  5. x.sim <- seq(-theta, theta, length = n.delta)
  6. y.sim <- seq(-14, 14, length = n.delta) # 太窄或太寬可以調整
  7. f.xy <-
  8. function(x, y, th) {
  9. 1 / (2 * th * sqrt(2 * pi)) * exp(-1 / 2 * (y - x) ^ 2)
  10. }
  11. # 求 Pxy(x,y): z.sim 為 200 * 200 方格
  12. z.sim <- outer(x.sim, y.sim, FUN = f.xy, th = theta)
  13.  
  14. # 求 Py(y),已知 Px 是均勻分配故可簡化算法
  15. Py <- apply(z.sim, 2, sum) / n.delta * (2 * theta)
  16.  
  17. # trapezoidal integration 檢查 Py dy 積分
  18. sum((Py[1:(length(Py) - 1)] + Py[2:(length(Py))]) * diff(y.sim)[1] / 2)
  19.  
  20. # Pxy 視覺化
  21. windows(5, 5)
  22. surf3D(
  23. matrix(rep(x.sim, n.delta), ncol = n.delta),
  24. matrix(rep(y.sim, each = n.delta), ncol = n.delta),
  25. z.sim,
  26. bty = "f",
  27. theta = 110,
  28. phi = 30,
  29. r = 3,
  30. ticktype = "detailed"
  31. )
  32.  
  33. # Py 視覺化
  34. windows(5, 5)
  35. plot(y.sim,
  36. Py,
  37. xlab = "y",
  38. ylab = "P_y(y)",
  39. type = "l")
  40.  
Success #stdin #stdout #stderr 0.23s 188480KB
stdin
Standard input is empty
stdout
[1] 1
stderr
Error: could not find function "windows"
Execution halted