predicting using trained model #280
-
Is there way to use the model I trained to test with separate test dataset? for example train the model with data from one hospital and test it with different hospital data? I am not sure whether the group option in run_ml() is relevant on this. |
Beta Was this translation helpful? Give feedback.
Answered by
kelly-sovacool
Jul 8, 2022
Replies: 1 comment 1 reply
-
Yes, you can do this! library(dplyr)
library(mikropml)
# in this example, otu_mini_bin is your original dataset used for training the model
results <- run_ml(otu_mini_bin,
'glmnet',
outcome_colname = 'dx',
seed = 2019)
# for the sake of this example, let's use `test_data` and remove the outcome column.
# in the real world, this would be a separate dataset such as from a different hospital.
new_data <- results$test_data %>% select(-dx)
predictions <- predict(results$trained_model, new_data, type = 'prob')
head(predictions)
#> cancer normal
#> 1 0.4705888 0.5294112
#> 2 0.4886979 0.5113021
#> 3 0.5130865 0.4869135
#> 4 0.5291845 0.4708155
#> 5 0.5013173 0.4986827
#> 6 0.5000077 0.4999923
# note that the new data will need to have the same features (columns) as the
# training data, otherwise you will get an error:
predictions <- predict(results$trained_model, new_data %>% select(-Otu00009), type = 'prob')
#> Error in eval(predvars, data, env): object 'Otu00009' not found p.s. sorry it took so long to answer your question, I didn't realize we had GitHub Discussions enabled. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
kelly-sovacool
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, you can do this!