我正在尝试对 R 中的 iris 数据集执行定期验证,以发现 MSE、二次 MSE 和三次 MSE。
# install.packages("class")
# install.packages("boot")
library("class")
library ("boot")
iris <- iris
train = sample(150, 75)
lm.fit = lm(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, data=iris, subset=train)
mean((iris -predict(lm.fit, iris))[-train ]^2)
# The estimated test MSE for the linear regression fit is...
lm.fit2 = lm(iris ~ poly(Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, 2), data=iris, subset=train)
mean((Species - predict(lm.fit2, df))[- train]^2)
# The estimated test MSE for the quadratic regression fit is...
lm.fit3 = lm(iris ~ poly(Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, 3), data=iris, subset=train)
mean((Species - predict(lm.fit3, df))[- train]^2)
# The estimated test MSE for the cubic regression fit is...
这是我不断遇到的错误。如何更正此问题,以免出现此错误?我也尝试更改为矢量无济于事,但似乎它需要为 lm 保持 df?
Warning messages:
1: In model.response(mf, "numeric") :
using type = "numeric" with a factor response will be ignored
2: In Ops.factor(y, z$residuals) : ‘-’ not meaningful for factors