2

I'm trying to display a plane of best fit within a 3D scatter plot using the library plot3D. When the code below is run everything seems fine enough, but if I replace the fit with the second fit I get strange behaviour, the plane is no longer a flat plane. I expect both versions to produce the same picture. What's going on?

enter image description here

library(plot3D)

df <- structure(list(X = 1:10, TV = c(230.1, 44.5, 17.2, 151.5, 180.8, 
8.7, 57.5, 120.2, 8.6, 199.8), radio = c(37.8, 39.3, 45.9, 41.3, 
10.8, 48.9, 32.8, 19.6, 2.1, 2.6), newspaper = c(69.2, 45.1, 
69.3, 58.5, 58.4, 75, 23.5, 11.6, 1, 21.2), sales = c(22.1, 10.4, 
9.3, 18.5, 12.9, 7.2, 11.8, 13.2, 4.8, 10.6)), .Names = c("X", 
"TV", "radio", "newspaper", "sales"), row.names = c(NA, 10L), class = "data.frame")


x<-df$TV
y<-df$radio
z<-df$sales

fit <- lm(z ~ x + y)
# fit <- lm(df$sales ~ df$TV + df$radio)

x.pred <- seq(min(x), max(x), length.out = 5)
y.pred <- seq(min(y), max(y), length.out = 5)
xy <- expand.grid( x = x.pred, y = y.pred)

z.pred <- matrix(predict(fit, newdata = xy), nrow = 5, ncol = 5)

scatter3D(x, y, z,
    surf = list(x = x.pred, y = y.pred, z = z.pred)
    )
4

1 回答 1

1

简短的回答是:两种拟合都是正确的。然而,第二个predict是没有找到正确的列名来预测。


如果您希望第二次适合工作,请使用:

fit <- lm(sales ~ TV + radio, data=df)
...
xy <- expand.grid(TV = x.pred, radio = y.pred)

为什么?因为predict总是搜索它训练的列名newdata

您可能注意到上面代码中的第一行也发生了变化,我们不再使用df$var格式,而是使用data参数。发生这种情况是因为使用此格式时fit$model等于:

   df$sales df$TV df$radio
1      22.1 230.1     37.8
2      10.4  44.5     39.3
3       9.3  17.2     45.9
...

而且我们不能用“$”美元符号命名列名。换句话说,我们不能这样做:

fit <- lm(df$sales ~ df$TV + df$radio)
...
xy <- expand.grid(df$TV = x.pred, df$radio = y.pred)

因为它会抛出一个错误。


如上所述,这两种拟合确实是正确的。如果你跑,

fit <- lm(z ~ x + y)
fit

你会得到,

系数:(截距)xy
2.08052 0.05598 0.15282

与,

fit <- lm(df$sales ~ df$TV + df$radio)
fit

你会得到,

系数:(截距)xy
2.08052 0.05598 0.15282

也是。


最后,请注意,当predictwithnewdata找不到正确的变量名称时,您将收到如下警告消息:

'newdata' had 25 rows but variables found have 10 rows

我认为这应该是一个错误。但它可能会在下一个版本中得到修复。关于这个问题的其他一些来源是:

于 2020-12-20T23:04:59.520 回答