Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Making Predictions with Models | Statistical Modeling in R
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
R for Data Scientists

bookMaking Predictions with Models

Once you have fitted a statistical model in R, such as a linear regression with lm() or a logistic regression with glm(), you often want to use this model to predict outcomes for new observations. The predict() function in R allows you to generate these predictions by applying your fitted model to a new data frame containing the relevant predictor variables. This is an essential step in using your model for real-world decision making, whether you are forecasting values or classifying new cases.

1234567891011121314151617
# Fit a linear regression model model_lm <- lm(mpg ~ wt + hp, data = mtcars) # Fit a logistic regression model (predicting automatic vs. manual transmission) model_glm <- glm(am ~ mpg + wt, data = mtcars, family = binomial) # Create a new data frame with predictor values new_data <- data.frame(wt = c(2.5, 3.2), hp = c(110, 150), mpg = c(21, 18)) # Predict with the linear model predicted_mpg <- predict(model_lm, newdata = new_data) # Predict with the logistic model (probabilities) predicted_prob <- predict(model_glm, newdata = new_data, type = "response") print(predicted_mpg) print(predicted_prob)
copy

The predict() function takes your fitted model object as its first argument and a newdata data frame containing the same predictor variables used in model fitting. For example, when using lm(), the predictors in newdata must match those in the formula. For glm() models, you can specify the type argument. Setting type = "response" returns predicted probabilities for classification tasks, while the default returns log-odds. The function returns a vector of predicted values, aligned with the rows in your newdata frame. This allows you to quickly see what your model expects for new cases, given their predictor values.

Note
Note

A common mistake is providing a newdata frame that does not match the variable names or structure used to fit the model.

  • If column names are misspelled or required predictors are missing, predict() will return an error or produce incorrect results;
  • Always double-check that your newdata includes all necessary variables with the correct names and types before making predictions.
question mark

Which statement is true about using the predict() function with fitted models in R?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 3

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookMaking Predictions with Models

Swipe um das Menü anzuzeigen

Once you have fitted a statistical model in R, such as a linear regression with lm() or a logistic regression with glm(), you often want to use this model to predict outcomes for new observations. The predict() function in R allows you to generate these predictions by applying your fitted model to a new data frame containing the relevant predictor variables. This is an essential step in using your model for real-world decision making, whether you are forecasting values or classifying new cases.

1234567891011121314151617
# Fit a linear regression model model_lm <- lm(mpg ~ wt + hp, data = mtcars) # Fit a logistic regression model (predicting automatic vs. manual transmission) model_glm <- glm(am ~ mpg + wt, data = mtcars, family = binomial) # Create a new data frame with predictor values new_data <- data.frame(wt = c(2.5, 3.2), hp = c(110, 150), mpg = c(21, 18)) # Predict with the linear model predicted_mpg <- predict(model_lm, newdata = new_data) # Predict with the logistic model (probabilities) predicted_prob <- predict(model_glm, newdata = new_data, type = "response") print(predicted_mpg) print(predicted_prob)
copy

The predict() function takes your fitted model object as its first argument and a newdata data frame containing the same predictor variables used in model fitting. For example, when using lm(), the predictors in newdata must match those in the formula. For glm() models, you can specify the type argument. Setting type = "response" returns predicted probabilities for classification tasks, while the default returns log-odds. The function returns a vector of predicted values, aligned with the rows in your newdata frame. This allows you to quickly see what your model expects for new cases, given their predictor values.

Note
Note

A common mistake is providing a newdata frame that does not match the variable names or structure used to fit the model.

  • If column names are misspelled or required predictors are missing, predict() will return an error or produce incorrect results;
  • Always double-check that your newdata includes all necessary variables with the correct names and types before making predictions.
question mark

Which statement is true about using the predict() function with fitted models in R?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 3
some-alt