Hi all I am new to R and I need to know how to use the predict function with tables.
I have a first table with some weather variables ( wind, temperature, pressure
) and with the consumption
of a building. I have created a lineal model doing this:
mymodel<-lm(energyConsum ~ temperature + pressure+ wind, data=mytable)
And I have a second table with the weather prediction, with the values temperature, wind and pressure
variables, and I want to predict the consumption
I know that I have to use the function predict()
, and set mymodel
, but I don't really know how to do it to create a new column with all the predictions (by line).
cheers
I don't have your data, but here's an example with mtcars
.
You can use predict.lm
's second argument, newdata
, to make predictions on a data.frame. You can then assign these results to a new column.
Example:
train <- mtcars[1:20, ]
test <- mtcars[21:32,]
mymodel <- lm(mpg ~ cyl + disp + hp, data=train)
test$pred <- predict(mymodel, test)
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.