-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoading and using a model.R
More file actions
29 lines (22 loc) · 1003 Bytes
/
Copy pathLoading and using a model.R
File metadata and controls
29 lines (22 loc) · 1003 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# ------------------------------------------------------------
# Purpose: Load a saved model and use it to make predictions
# ------------------------------------------------------------
#---------------------------------------------------------
## 1. Loading the saved model
loaded_model <- readRDS("example_model.rds")
#---------------------------------------------------------
## 2. Preparing new data for prediction
# Replace this with your own new / unseen data
new_data <- data.frame(x1 = c(0.1, 0.5), x2 = c(-1.2, 0.3))
#---------------------------------------------------------
## 3. Making predictions
# Some models (e.g., xgboost) require a matrix, others (glm, randomForest, caret) require a data frame.
if (inherits(loaded_model, "xgb.Booster")) {
# XGBoost requires a numeric matrix
pred_input <- as.matrix(new_data)
} else {
# Most other R models work with a data frame
pred_input <- new_data
}
predictions <- predict(loaded_model, newdata = pred_input)
print(predictions)