简体   繁体   中英

Input in a lm (linear regression) function in R

I am doing a lm in R using vectors

Y<-c(1,0,1,2,5,1,4,6,2,3,5,4,6,8,4,5,7,9,7,6)
X<-c(60,63,65,70,70,70,80,80,80,80,85,89,90,90,90,90,94,100,100,100)

rg<-lm(Y ~ X)
summary(rg)


X1<-c(61,64,69,73,76,77,82,84,87,90,95,99,100,101,102,103,104,107,110,120)

When I use X1 as input for predict

Y1 <- predict(rg,X1 )

I have an error:

"Error in eval(predvars, data, env) : numeric 'envir' arg not of length one"

This error doesn't occur if I use a data.frame object as input...I dob't understand why I need a data.frame and a vector doesn't work

am I doing something wrong??

Thanks in advance

I agree with Gregor. X1, the "newdata" should be a data.frame.
Y1 <- predict(rg, as.data.frame(X1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM