Quadratic data in Oracle R Enterprise and Oracle Data Mining
By Jeff Taylor-Oracle on Feb 26, 2014
I was working with some data which was stored in an Oracle database on a SPARC T4 server. I thought that the data had a quadratic component and I wanted to analyze the data using SQL Developer and Oracle Data Mining, a component of the Oracle Advanced Analytics Option. When I reviewed the initial analysis, I wasn't getting results that I had expected, and the fit of the model wasn't very good. I decided to feed some simple, synthetic quad data into Oracle Data Miner to ensure that I was using the tool properly.
Oracle R Enterprise was used as the tool to create and view the synthetic data.
From an R session that has the Oracle R Enterprise package installed, it is easy to access an Oracle Database:
The following R function, quad1(), is used to calculate "y=ax^2 + bx + c",
- the data frame that is passed in has a column of x values.
- a is in coefficients[feature, 1]
- b is in coefficients[feature, 2]
- c is in coefficients[feature, 3]
The function will simply calculate points along a parabolic line and is more complicated than it needs to be. I will leave it in this complicated format so that I can extend it to work with more interesting functions, such as a parabolic surface, later.
The following function, genData(), creates random "x" data points and uses func() to calculate the y values that correspond to the random x values.
The following function, quadGraph(), is used for graphing. The points in dframe are displayed in a scatter plot. The coefficients for the known synthetic data is passed in and the corresponding line is sketched in blue. (Obviously, if you aren't working with synthetic data, it is unlikely that you will know the "true" coefficients.) The R model that is the best estimate of the data based on regression is passed in and sketched in blue.
Here are the settings that will be used.
Here, synthetic data is created that matches the quadratic function and the random coefficients.
We can make this exercise at least slightly more realistic by adding some irreducible error for the regression algorithm to deal with. Add noise.
Great. At this point I have good quadratic synthetic data which can be analyzed. Feed the synthetic data to the Oracle Database.
The Oracle R Enterprise function to fit the linear model works as expected.
Notice that the "true" coefficients, that were used to create the synthetic data are close to the values from the regression. For example, the true "a" is stored in coefficients[1,1] = -0.8969 and is close to the model's I(x1 * x1) = -0.8392. Not bad given that the model was created from only 20 data points.
The 20 data points, which were calculated from the "true" equation, but with noisy irreducible error added, are shown in the graph. The model, estimated by ore.lm() from the 20 noisy data points, is close to true.
At this point, my job is either complete, or ready to start, depending on your perspective. I'm happy that ore.lm() does a nice job of fitting, so maybe I'm done. But if you remember that my initial goal was to validate that SQL Developer and Oracle Data Miner work with quadratic data, my job has just begun. Now that I have known good quadratic synthetic data in the database, I'll use SQL Developer and the Oracle Data Mining to verify that everything is fine.
One more step in R. Create a second Oracle Database table that will be used to test the regression model.
Here is the SQL Developer workflow that will be used. The synthetic data is in the Oracle Database table "QuadraticSample_20_1". The "Regress Build" node will run linear regression on the synthetic data. The test data which was generated using R in the previous paragraph, is stored in a Oracle Database table named "QuadraticTest_20_1". The Apply node will use the regression model that has been created and use the "x1" values from the test data, storing the y values in an Oracle Database table named "QUADTESTRESULTS".
So how did it work? A PhD in statistics would quickly tell you, "not well", and might look at you like you're an idiot if you don't know that a Model F Value Statistic of 3.25 isn't good. My more pedestrian approach is to plot the results of applying the model to the test data.
The predictive confidence of the model that was created is poor:
Pull the test result data into R for viewing:
Hmm, that doesn't look parabolic to me:
Now that I'm quite sure that SQL Developer and Oracle Data Mining isn't giving an expected fit, check through the advanced settings:
There it is!!
Set the feature generation to use quadratic candidates, re-run the model.
The predictive confidence has improved.
Bring the new results back into R:
Also, your statistician friends will be happy because the new model has a Model F Value Statistic of 124. Exciting, right?
Now, off to work on parabolic surfaces...