0

我试图只预测两个值,模型是

data(mtcars) #is already available in R

m1 = loess(mtcars$mpg ~ mtcars$cyl + mtcars$disp)
# the prdedict function works well
y.predict <- predict(m1, data.frame(mtcars$cyl, mtcars$disp))

但它通过使用两个预测变量的所有存在值来估计模型 m1

mtcars$cyl 和 mtcars$disp,我只想估计 mtcars$mpg 的一个值。

我试过了

new.data= data.frame(mtcars$cyl=c(2,3), mtcars$disp=c(200,1000))
y.predict <- predict(m1, new.data)

但它给了我以下警告消息:'newdata' has 2 rows but variables found have 32 rows

感谢你的帮助

4

1 回答 1

0

这是符号中的细微差别,请尝试以下方式:

data(mtcars) #is already available in R
attach(mtcars) # attach it to the environment so you can use column names directly
m1 = loess(mpg ~ cyl + disp)
# the prdedict function works well
y.predict <- predict(m1, data.frame(cyl, disp))

# add new data
new.data = data.frame(cyl = c(6,8), disp = c(150,100))
y.predict <- predict(m1, new.data)
于 2016-11-10T23:12:03.940 回答