Skip to content

Instantly share code, notes, and snippets.

@garcia556
Created November 22, 2017 04:21
Show Gist options
  • Save garcia556/b427bc011d7ec0587c0fc96218f06218 to your computer and use it in GitHub Desktop.
Save garcia556/b427bc011d7ec0587c0fc96218f06218 to your computer and use it in GitHub Desktop.
Creates a primitive linear regression model and exports it to PMML format
#!/usr/bin/env Rscript
# quick-and-dirty run method: docker run -it -v $(pwd):/app:ro -w /app r-base bash -c "apt-get update; apt-get install -y libxml2-dev; R -e 'install.packages(\"pmml\")'; ./script.R"
library(pmml)
types <- c("pr", "yes_no", "score")
cat("Enter model type of 1) Probability 2) Yes/No or 3) Score: ")
option <- readLines(file("stdin"),1)
option <- as.integer(option)
# data sets
data <- c()
# 1 - probability
text <- "predictor,pr
x,0.5
x,0.5
y,0.75
y,0
x,0
y,0"
data <- append(data, text)
# 2 - yes/no
text <- "predictor,yes_no
x,1
x,1
y,0
y,
x,
y,"
data <- append(data, text)
# 3 - score
text <- "predictor,score
x,400
x,400
y,450
y,0
x,0
y,0"
data <- append(data, text)
# choose selected dataset
data <- data[option]
data <- read.csv(text = data, header = TRUE, sep = ",", stringsAsFactors = TRUE)
# split samples 50/50
train <- head(data, nrow(data) / 2)
test <- tail(data, nrow(data) / 2)
# linear regression
cols <- paste(types[option], "~", ".")
model <- lm(as.formula(cols), data = train)
str(train)
str(test)
summary(model)
predict(model, test)
pmml(model)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment