library(plotly)
miles <- c(204, 312, 144, 266, 180, 192, 366, 165)
gallons <- c(10, 15, 7, 12, 9, 10, 17, 8)
weight <- c(3000, 3500, 2800, 3200, 2900, 3100, 3800, 2600)
mileage_data <- data.frame(Miles = miles, Weight = weight,
Gallons = gallons)
# Fit the linear regression model
model <- lm(Gallons ~ Miles + Weight, data = mileage_data)
grid <- expand.grid(Miles = seq(min(miles), max(miles), length.out = 10),
Weight = seq(min(weight), max(weight), length.out = 10))
grid$Gallons <- predict(model, newdata = grid)
# Creating the 3D scatter plot
p <- plot_ly(mileage_data, x = ~Miles, y = ~Weight, z = ~Gallons,
type = "scatter3d")
# Adding the regression plane
p <- add_trace(p, x = ~Miles, y = ~Weight, z = ~Gallons, data = grid,
type = "mesh3d", opacity = 0.5)
# Print
p