how to use predict() in r in r

predict() is a commonly used function in R for making predictions from fitted models. Here is an example of how to use predict() with a linear regression model:

main.r
# Fit a linear regression model
model <- lm(mpg ~ wt, data = mtcars)

# Use predict() to make predictions on new data
new_data <- data.frame(wt = c(2.8, 3.5, 4.2))
predictions <- predict(model, newdata = new_data)

# View the predictions
predictions
250 chars
10 lines

In this example, we first create a linear regression model model using the lm() function. We then create a new data frame new_data with values for the predictor variable wt and use predict() to generate predictions for the response variable mpg. The predictions are stored in the predictions variable, and we can view them by printing the variable to the console.

Note that predict() can be used with many types of models, not just linear regression. It is a useful tool for making predictions in a variety of machine learning and data analysis applications.

gistlibby LogSnag