Skip to content

Instantly share code, notes, and snippets.

@neurotroph
Created September 14, 2020 14:20
Show Gist options
  • Save neurotroph/c1d98e5551690955ecaf5181794d1c54 to your computer and use it in GitHub Desktop.
Save neurotroph/c1d98e5551690955ecaf5181794d1c54 to your computer and use it in GitHub Desktop.
Predict MPG via Command Line
#
# Predicting mpg for your car using a pre-trained BRMS model.
# Read more about this at https://skopos-elements.de/journal/
#
# (c) by SKOPOS ELEMENTS, 2020
#
suppressPackageStartupMessages(library(tidyverse))
suppressPackageStartupMessages(library(cli))
suppressPackageStartupMessages(library(brms))
suppressPackageStartupMessages(library(optparse))
cli_options <- list(
make_option(c("--wt"), type = "integer", dest = "wt",
default = NA, metavar = "WEIGHT", action = "store",
help = "The car's weight (in 1000 lbs)."),
make_option(c("--hp"), type = "integer", dest = "hp",
default = NA, metavar = "HORSEPOWER", action = "store",
help = "The car's gross horsepower."),
make_option(c("--cyl"), type = "integer", dest = "cyl",
default = NA, metavar = "CYLINDERS", action = "store",
help = "Number of cylinders."),
make_option(c("--gear"), type = "integer", dest = "gear",
default = NA, metavar = "GEARS", action = "store",
help = "Number of forward gears.")
)
args <- parse_args(OptionParser(option_list = cli_options))
# Angegebene Parameter überprüfen
if (sum(is.na(args)) > 0) {
cli_alert_danger("Alle Angaben zum Fahrzeug müssen angegeben werden.")
quit()
}
new_df <- tibble(wt = args$wt,
hp = args$hp,
cyl = args$cyl,
gear = args$gear)
bm_cars <- readRDS("./bm_cars.Rds")
df_predictions <- predictive_interval(bm_cars, newdata = new_df, prob = .8,
allow_new_levels = TRUE)
new_df$mpg_predict <- round(predict(bm_cars, newdata = new_df,
allow_new_levels = TRUE)[,1], 2)
new_df$mpg_predict_lower <- round(df_predictions[,1], 2)
new_df$mpg_predict_upper <- round(df_predictions[,2], 2)
print(new_df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment