首先,如果您提供示例数据集(或您的实际数据,这会更好)和可重现的代码示例,您更有可能获得帮助。否则你是在强迫我们这样做——我认为这就是为什么你的问题被忽略了 6 个小时。详情请见this link。
其次,像这样使用lm(...) 和formula=log(z)~x*y 是一个非常糟糕的主意。线性建模基于响应中的误差(在您的情况下为log(z))正态分布且方差恒定的假设。如果您的z-data 具有具有恒定方差的正态分布误差,那么log(z) 肯定不会。这是一个典型的错误;正确的方法是使用广义线性建模(参见glm 包等)和family=poisson。
最后,回答你的问题。下面的代码创建了一个覆盖在响应面上的 3D 散点图。它使用rgl 包,可生成可旋转的 3D 图。
在这里,我对点进行了着色,使表面下方的点为红色,上方的点为绿色,并添加了从每个点到表面的垂线。
棘手的一点是,在surface3d(...) 中,x 和 y 参数是对应于网格的向量,而 z 是一个矩阵,每个 x 值对应一行,每个 y 值对应一列。
对于您的真实数据,您可能需要调整 open3d(...) 中的 scale=... 参数。
# create sample dataset - you have this already,,,
set.seed(1) # for reproducible example
df <- data.frame(x=sample(1:50,50)/50,y=sample(1:50,50)/50)
df$z <- with(df,exp(4*x + 2*y - 6*x*y + 6)+rnorm(50,sd=500))
fit <- lm(log(z) ~ x*y,df) # bad, bad, bad - don't do this!!!
# you start here...
df$pred <- predict(fit)
# set up matrix of z-values
x <- seq(min(df$x),max(df$x),len=100)
y <- seq(min(df$y),max(df$y),len=100)
plot.df <- expand.grid(x=x,y=y)
plot.df$z <- predict(fit,newdata=plot.df)
library(reshape2)
z <- dcast(plot.df,x~y,value.var="z")[-1]
# plot the points, the fitted surface, and droplines
library(rgl)
colors <- 2.5+0.5*sign(residuals(fit))
open3d(scale=c(1,1,0.2))
points3d(df$x,df$y,log(df$z),col=colors)
surface3d(x,y,as.matrix(z),col="blue",alpha=.2)
apply(df,1,function(row)lines3d(rep(row[1],2),rep(row[2],2),c(log(row[3]),row[4]),col=colors))
axes3d()
title3d(xlab="X",ylab="Y",zlab="log(Z)")