Hemodynamic 3D Linear Modeling Surface map:
# 1. Fit the linear regression model
fit <- lm(BOLD ~ Stimulus + Attention, data = neuro_data)
# 2. Extract sequence grids using bracket syntax
axis_x <- seq(min(neuro_data[["Stimulus"]]), max(neuro_data[["Stimulus"]]), length.out = 20)
axis_y <- seq(min(neuro_data[["Attention"]]), max(neuro_data[["Attention"]]), length.out = 20)
# 3. Create the mathematical surface matrix
surface_matrix <- t(outer(axis_x, axis_y, function(x, y) {
predict(fit, newdata = data.frame(Stimulus = x, Attention = y))
}))
# 4. Build and print the interactive plot directly
plot_ly(neuro_data, x = ~Stimulus, y = ~Attention, z = ~BOLD, height = 350, width = 800) %>%
add_markers(marker = list(color = '#8C1D40', size = 4, opacity = 0.6)) %>%
add_surface(x = ~axis_x, y = ~axis_y, z = ~surface_matrix, opacity = 0.5)