Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- plot_fit <- function(sobj, mthd, ncomp, grid_size = 25) {
- require(plot3D)
- x <- with(sobj, c(X[, 1]))
- y <- with(sobj, c(X[, 2]))
- z <- with(sobj, c(Y[, 1]))
- method <- get(mthd)(z ~ x + y)
- grid_size <- grid_size
- x_pred <- seq(min(x), max(x), length.out = grid_size)
- y_pred <- seq(min(y), max(y), length.out = grid_size)
- xy <- expand.grid(x = x_pred, y = y_pred)
- z_pred <- matrix(predict(method, newdata = xy, ncomp = ncomp), grid_size, grid_size)
- fitpoints <- predict(method, ncomp = ncomp)
- par(family = "mono")
- scatter3D(x, y, z, ticktype = "detailed", pch = 21, bty = "g", col = "grey50",
- cex = 1, theta = 55, phi = 0, colkey = FALSE, bg = viridis::viridis(length(z)),
- main = paste("Method:", toupper(mthd), "|", ncomp, "Components"),
- xlab = "Predictor 1", ylab = "Predictor 2", zlab = "Response",
- surf = list(x = x_pred, y = y_pred, z = z_pred, shade = 0.1,
- fit = fitpoints, facets = NA, col = viridis::viridis(length(z)),
- alpha = 0.2, lcol = viridis::viridis(length(z))))
- scatter3D(x, y, fitpoints, add = TRUE, colkey = FALSE, pch = 16,
- col = viridis::viridis(length(fitpoints)),
- cex = 0.7)
- }
- plotly_fit <- function(sobj, mthd, ncomp, grid_size = 25) {
- require(plotly)
- x <- with(sobj, c(X[, 1]))
- y <- with(sobj, c(X[, 2]))
- z <- with(sobj, c(Y[, 1]))
- method <- get(mthd)(z ~ x + y)
- grid_size <- grid_size
- x_pred <- seq(min(x), max(x), length.out = grid_size)
- y_pred <- seq(min(y), max(y), length.out = grid_size)
- xy <- expand.grid(x = x_pred, y = y_pred)
- z_pred <- matrix(predict(method, newdata = xy, ncomp = ncomp), grid_size, grid_size)
- fitpoints <- predict(method, ncomp = ncomp)
- # custom grid style
- axx <- list(
- gridcolor='rgb(255, 255, 255)',
- zerolinecolor='rgb(255, 255, 255)',
- showbackground=TRUE,
- backgroundcolor='rgb(230, 230,230)'
- )
- # The Plot
- plot_ly() %>%
- add_markers(
- x = x, y = y, z = z,
- showlegend = FALSE,
- marker = list(
- color = "CadetBlue",
- size = 5,
- line = list(
- color = "black",
- width = 0.5
- )
- )) %>%
- add_surface(
- x = ~x_pred,
- y = ~y_pred,
- z = ~z_pred,
- showscale = FALSE,
- inherit = FALSE,
- opacity = 0.7
- ) %>%
- layout(
- title = paste("Method:", toupper(mthd), "; ", ncomp, "Components"),
- scene = list(
- xaxis = append(axx, list(title = "Predictor 1", range = c(-2, 2))),
- yaxis = append(axx, list(title = "Predictor 2", range = c(-2, 2))),
- zaxis = append(axx, list(title = "Response", range = c(-4, 4))),
- camera=list(
- eye = list(x=-1.8, y=1.8, z=0.8)
- )
- )
- )
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement