0
Follow
2
View

Linear regression in R: Warning: 'newdata' had 1 row but variables found have 392 rows

damofs0a 注册会员
2023-01-25 12:32

It would help to make the example reproducible but I did some work and was able to reproduce the error:

library(ISLR)
auto_df = Auto

my_acc<-auto_df$acceleration
my_horse<-auto_df$horsepower
mydata <- data.frame(my_acc, my_horse )
car_linear_regression <- lm(my_acc ~ my_horse, mydata )

predict(car_linear_regression,newdata = data.frame(horsepower = 93.5))

The problem is in your model there is no variable called horsepower. You called it my_horse, so the following works:

predict(car_linear_regression,newdata = data.frame(my_horse = 93.5))

As a side note, instead of creating separate variables I would just call the regression model as:

car_linear_regression <- lm(acceleration ~ horsepower, data=auto_df )

Then your original prediction would have worked.

About the Author

Question Info

Publish Time
2023-01-25 12:32
Update Time
2023-01-25 12:32

Related Question