首页 > 解决方案 > 如何在r中将平面回归变为曲面?

问题描述

我正在尝试将平面更改为曲面,但这很困难。

查看散点图,表面应该是弯曲的而不是平坦的。如何修改我的代码?

代码和图片如下。

由于数据文件太多,无法附上,非常抱歉。我认为 lm 函数可能会创建该平面。有想法吗?

谢谢!

library(scatterplot3d)
library(plot3D)
library(readxl)

RoomA <- read_excel("Data_without_outlier_RoomA.xlsx")


# x, y, z variables
x <- RoomA$Ammonia
y <- RoomA$Ventilation
z <- RoomA$Emissions

# Compute the linear regression (z = ax + by + d)
fit <- lm(z ~ x + y)
# predict values on regular xy grid
grid.lines = 25
x.pred <- seq(min(x), max(x), length.out = grid.lines)
y.pred <- seq(min(y), max(y), length.out = grid.lines)
xy <- expand.grid( x = x.pred, y = y.pred)
z.pred <- matrix(predict(fit, newdata = xy), 
                 nrow = grid.lines, ncol = grid.lines)
# fitted points for droplines to surface
fitpoints <- predict(fit)
# scatter plot with regression plane
scatter3D(x, y, z, pch = 20, cex = 1.5,
          theta = 320, phi = 25, ticktype = "detailed",
          xlab = "", ylab = "", zlab = "",  
          surf = list(x = x.pred, y = y.pred, z = z.pred,  
                      facets = NA), main = "Room A")

在此处输入图像描述

标签: rplotregressioncurvescatter

解决方案


推荐阅读