我试图只预测两个值,模型是
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'有两行,但找到的变量有32行
感谢您的帮助
答案 0 :(得分: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)