8.2 Use the ore.predict Function
These examples demonstrate the use of the ore.predict
function.
Example 8-1 Using the ore.predict Function on a Linear Regression Model
This example builds a linear regression model, irisModel
, using the lm
function on the iris
data.frame
. It pushes the data set to the database as the temporary table IRIS and the corresponding ore.frame
proxy, IRIS
. The example scores the model by invoking ore.predict
on it and then combines the prediction with IRIS
ore.frame
object. Finally, it displays the first six rows of the resulting object.
IRISModel <- lm(Sepal.Length ~ ., data = iris) IRIS <- ore.push(iris) IRIS_pred <- ore.predict(IRISModel, IRIS, se.fit = TRUE, interval = "prediction") IRIS <- cbind(IRIS, IRIS_pred) head(IRIS)
Listing for This Example
R> IRISModel <- lm(Sepal.Length ~ ., data = iris) R> IRIS <- ore.push(iris) R> IRIS_pred <- ore.predict(IRISModel, IRIS, se.fit = TRUE, + interval = "prediction") R> IRIS <- cbind(IRIS, IRIS_pred) R> head(IRIS) Sepal.Length Sepal.Width Petal.Length Petal.Width Species PRED SE.PRED 1 5.1 3.5 1.4 0.2 setosa 5.004788 0.04479188 2 4.9 3.0 1.4 0.2 setosa 4.756844 0.05514933 3 4.7 3.2 1.3 0.2 setosa 4.773097 0.04690495 4 4.6 3.1 1.5 0.2 setosa 4.889357 0.05135928 5 5.0 3.6 1.4 0.2 setosa 5.054377 0.04736842 6 5.4 3.9 1.7 0.4 setosa 5.388886 0.05592364 LOWER.PRED UPPER.PRED 1 4.391895 5.617681 2 4.140660 5.373027 3 4.159587 5.386607 4 4.274454 5.504259 5 4.440727 5.668026 6 4.772430 6.005342 R> head(IRIS) Sepal.Length Sepal.Width Petal.Length Petal.Width Species PRED SE.PRED LOWER.PRED UPPER.PRED 1 5.1 3.5 1.4 0.2 setosa 5.004788 0.04479188 4.391895 5.617681 2 4.9 3.0 1.4 0.2 setosa 4.756844 0.05514933 4.140660 5.373027 3 4.7 3.2 1.3 0.2 setosa 4.773097 0.04690495 4.159587 5.386607 4 4.6 3.1 1.5 0.2 setosa 4.889357 0.05135928 4.274454 5.504259 5 5.0 3.6 1.4 0.2 setosa 5.054377 0.04736842 4.440727 5.668026 6 5.4 3.9 1.7 0.4 setosa 5.388886 0.05592364 4.772430 6.005342
Example 8-2 Using the ore.predict Function on a Generalized Linear Regression Model
This example builds a generalized linear model using the infert
data set and then calls the ore.predict
function on the model.
infertModel <- glm(case ~ age + parity + education + spontaneous + induced, data = infert, family = binomial()) INFERT <- ore.push(infert) INFERTpred <- ore.predict(infertModel, INFERT, type = "response", se.fit = TRUE) INFERT <- cbind(INFERT, INFERTpred) head(INFERT)
Listing for This Example
R> infertModel <- + glm(case ~ age + parity + education + spontaneous + induced, + data = infert, family = binomial()) R> INFERT <- ore.push(infert) R> INFERTpred <- ore.predict(infertModel, INFERT, type = "response", + se.fit = TRUE) R> INFERT <- cbind(INFERT, INFERTpred) R> head(INFERT) education age parity induced case spontaneous stratum pooled.stratum 1 0-5yrs 26 6 1 1 2 1 3 2 0-5yrs 42 1 1 1 0 2 1 3 0-5yrs 39 6 2 1 0 3 4 4 0-5yrs 34 4 2 1 0 4 2 5 6-11yrs 35 3 1 1 1 5 32 6 6-11yrs 36 4 2 1 1 6 36 PRED SE.PRED 1 0.5721916 0.20630954 2 0.7258539 0.17196245 3 0.1194459 0.08617462 4 0.3684102 0.17295285 5 0.5104285 0.06944005 6 0.6322269 0.10117919
Example 8-3 Using the ore.predict Function on an ore.model Model
This example pushes the iris
data set to the database as the temporary table IRIS and the corresponding ore.frame
proxy, IRIS
. The example builds a linear regression model, IRISModel2
, using the ore.lm
function. It scores the model and adds a column to IRIS
.
IRIS <- ore.push(iris) IRISModel2 <- ore.lm(Sepal.Length ~ ., data = IRIS) IRIS$PRED <- ore.predict(IRISModel2, IRIS) head(IRIS, 3)
Listing for This Example
R> IRIS <- ore.push(iris) R> IRISModel2 <- ore.odmGLM(Sepal.Length ~ ., data = IRIS) R> IRIS$PRED <- ore.predict(IRISModel, IRIS) R> head(IRIS, 3) Sepal.Length Sepal.Width Petal.Length Petal.Width Species PRED 1 5.1 3.5 1.4 0.2 setosa 5.004788 2 4.9 3.0 1.4 0.2 setosa 4.756844 3 4.7 3.2 1.3 0.2 setosa 4.773097
Parent topic: Prediction With R Models