Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. plot_fit <- function(sobj, mthd, ncomp, grid_size = 25) {
  2. require(plot3D)
  3. x <- with(sobj, c(X[, 1]))
  4. y <- with(sobj, c(X[, 2]))
  5. z <- with(sobj, c(Y[, 1]))
  6. method <- get(mthd)(z ~ x + y)
  7. grid_size <- grid_size
  8. x_pred <- seq(min(x), max(x), length.out = grid_size)
  9. y_pred <- seq(min(y), max(y), length.out = grid_size)
  10. xy <- expand.grid(x = x_pred, y = y_pred)
  11. z_pred <- matrix(predict(method, newdata = xy, ncomp = ncomp), grid_size, grid_size)
  12. fitpoints <- predict(method, ncomp = ncomp)
  13. par(family = "mono")
  14. scatter3D(x, y, z, ticktype = "detailed", pch = 21, bty = "g", col = "grey50",
  15. cex = 1, theta = 55, phi = 0, colkey = FALSE, bg = viridis::viridis(length(z)),
  16. main = paste("Method:", toupper(mthd), "|", ncomp, "Components"),
  17. xlab = "Predictor 1", ylab = "Predictor 2", zlab = "Response",
  18. surf = list(x = x_pred, y = y_pred, z = z_pred, shade = 0.1,
  19. fit = fitpoints, facets = NA, col = viridis::viridis(length(z)),
  20. alpha = 0.2, lcol = viridis::viridis(length(z))))
  21. scatter3D(x, y, fitpoints, add = TRUE, colkey = FALSE, pch = 16,
  22. col = viridis::viridis(length(fitpoints)),
  23. cex = 0.7)
  24. }
  25. plotly_fit <- function(sobj, mthd, ncomp, grid_size = 25) {
  26. require(plotly)
  27. x <- with(sobj, c(X[, 1]))
  28. y <- with(sobj, c(X[, 2]))
  29. z <- with(sobj, c(Y[, 1]))
  30. method <- get(mthd)(z ~ x + y)
  31. grid_size <- grid_size
  32. x_pred <- seq(min(x), max(x), length.out = grid_size)
  33. y_pred <- seq(min(y), max(y), length.out = grid_size)
  34. xy <- expand.grid(x = x_pred, y = y_pred)
  35. z_pred <- matrix(predict(method, newdata = xy, ncomp = ncomp), grid_size, grid_size)
  36. fitpoints <- predict(method, ncomp = ncomp)
  37.  
  38. # custom grid style
  39. axx <- list(
  40. gridcolor='rgb(255, 255, 255)',
  41. zerolinecolor='rgb(255, 255, 255)',
  42. showbackground=TRUE,
  43. backgroundcolor='rgb(230, 230,230)'
  44. )
  45. # The Plot
  46. plot_ly() %>%
  47. add_markers(
  48. x = x, y = y, z = z,
  49. showlegend = FALSE,
  50. marker = list(
  51. color = "CadetBlue",
  52. size = 5,
  53. line = list(
  54. color = "black",
  55. width = 0.5
  56. )
  57. )) %>%
  58. add_surface(
  59. x = ~x_pred,
  60. y = ~y_pred,
  61. z = ~z_pred,
  62. showscale = FALSE,
  63. inherit = FALSE,
  64. opacity = 0.7
  65. ) %>%
  66. layout(
  67. title = paste("Method:", toupper(mthd), "; ", ncomp, "Components"),
  68. scene = list(
  69. xaxis = append(axx, list(title = "Predictor 1", range = c(-2, 2))),
  70. yaxis = append(axx, list(title = "Predictor 2", range = c(-2, 2))),
  71. zaxis = append(axx, list(title = "Response", range = c(-4, 4))),
  72. camera=list(
  73. eye = list(x=-1.8, y=1.8, z=0.8)
  74. )
  75. )
  76. )
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement