Question

I am new to R and stuck with one problem. I will explain it by an example.

I am using R with php. I have one R script that calculates the linear regression:

reg_result <- lm( Y ~ A1 + A2 + A3, data=query_result)

And I am using the regression result reg_result for prediction like:

predict(reg_result, another_dataframe, interval="predict", level = 0.20)

It works very fine.

But my problem is that I need to use predict with reg_result in some time intervals, let say each 1 hour.

So is there any ways that I can persist the result of regression reg_result to use it later.

I have used RMySQL, but I dont know how to store the result of regression in mysql.

any help would be greatly appreciated.

And I can provide more details if required.

EDIT: Is it possible to store the R object in MySQL blob with RMySQL ? As I have googled and found Currently there are no facilities to import/export BLOBs. in RMySQL

Was it helpful?

Solution

What I did (using RODBC, but that is a minor detail) is extract the regression coefficients from the lm object, something like this (for y~x):

intercept = lm$coefficients[["(Intercept)"]]
slope = lm$coefficients[["x"]])

Then store these coefficients in a database table, and later just use a linear function with the values from the table to predict, instead of the predict method (like f <- function(x) { intercept + slope*x }).

OTHER TIPS

The solution provided by ROLO is very fine and working same as I required.

But I have gone with other way to make it less complex.

1) Stored the result in binary file

reg_result <- lm( Y ~ A1 + A2 + A3, data=query_result)
saveRDS(reg_result,"example.dat");

2) And loaded when required

reg_result <- readRDS("example.dat", refhook = NULL);
predict(reg_result, another_dataframe, interval="predict", level = 0.20)

Hope it will help someone else on how to store the regression result in binary files and retrieve when required.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top