EXPPRED
Updated 2023-10-31 16:02:46.370000
Syntax
SELECT [westclintech].[wct].[EXPPRED](
<@Coef, nvarchar(max),>
,<@New_x, nvarchar(max),>
,<@UseNames, bit,>)
Description
Use the SQL Server scalar function EXPPRED to evaluate a dependent variable from the independent variables using the regression coefficients from the output of the LOGEST or LOGEST_q function. You can use the following equation to calculate the dependent value, y, from the independent variables, x.
y = e^{\ln m_0 + (\ln m_1)x_1 + (\ln m_2)x_2... +(\ln m_{n-1})x_{n-1}+(\ln m_n)x_n}
The m values are included in the output from the XLeratorDB linear regression functions LOGEST and LOGEST_q and have a stat_name of 'm'. The subscripts for m are in the idx column of the regression output.
EXPPRED matches the coefficients of regression (m) to the new independent variables in one of two ways. You can specify that they be matched using the names of the coefficients and the column names of the independent variables or by using the idx associated with m and the relative position of the new independent variables.
Arguments
@UseNames
A bit value indicating whether or not use the independent variable names to match up the coefficients with the independent variables. When True the coefficients are match to the independent variables using the names. Then False, the coefficients are match to the independent variables using the column position in the resultant table (or string) returned by @New_x.
@Coef
A string which contains a SELECT statement which returns a resultant table consisting of the idx, stat_val and col_name from the LOGEST or LOGEST_q table-valued functions.
@New_x
A string which contains either a SELECT statement which returns a single row consisting of the independent variables to be evaluated or a comma separated string of new independent variables. When @New_x is not a SELECT statement @UseNames is ignored and coefficients are match to independent variables based on their position in the string.
Return Type
float
Remarks
There will be 1 more coefficient than independent variable.
When @UseNames is 1, EXPPRED matches the col_name from the regression to the column names in @New_x.
EXPPRED does not calculate the regression coefficients.
Examples
Example #1
--Put some data in a table
SELECT IDENTITY(int, 1, 1) as recno,
*
INTO #E
FROM
(
VALUES
(508.78, 0.34, 3.95, 1.75, 4.41, 49),
(276.59, 0.39, 3.72, 1.17, 3.48, 46.72),
(82.07, 0.41, 2.35, 1.64, 3.98, 13.03),
(225.66, 0.54, 5.08, 1.57, 3.21, 16.32),
(176.03, 0.35, 3.31, 1.19, 3.73, 34.16),
(148.2, 0.37, 4.46, 1.4, 4.36, 14.62),
(357.56, 0.38, 5.51, 1.63, 3.74, 27.97),
(470.35, 0.38, 3.74, 1.72, 3.94, 48.8),
(308.7, 0.48, 4.76, 1.55, 3.37, 25.84),
(219.6, 0.37, 5.03, 1.53, 4.37, 17.6)
) n (y, x0, x1, x2, x3, x4);
--Run the exponential regression and store the results
SELECT *
INTO #em
FROM wct.LOGEST('#E', 'y,x0,x1,x2,x3,x4', '', NULL, 1, 1);
The regression coefficients are stored in the temp table #em and we can select them using the following SQL.
SELECT idx,
stat_val,
col_name
FROM #em
WHERE stat_name = 'm';
This produces the following result.
{"columns":[{"field":"idx","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"stat_val","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"col_name"}],"rows":[{"idx":"0","stat_val":"4.21565610446703","col_name":"Intercept"},{"idx":"1","stat_val":"2.85133142337479","col_name":"x0"},{"idx":"2","stat_val":"1.38181447807267","col_name":"x1"},{"idx":"3","stat_val":"1.88972141616803","col_name":"x2"},{"idx":"4","stat_val":"1.08094701123002","col_name":"x3"},{"idx":"5","stat_val":"1.03536022300628","col_name":"x4"}]}
As you can see from the resultant table, the regression coefficients can be referenced by the idx column or the col_name column. We can use the EXPPRED function with the @UseNames = 1 and the input data to calculate a predicted y. Note that we have passed all the column name from the input table into the @New_x variable not just the independent variable. By using @UseNames = 1, EXPPRED matched the regression coefficient names to the column names returned by the @New_x SQL.
SELECT recno,
x1,
x2,
x3,
x4,
y,
wct.EXPPRED(
'SELECT
idx
,stat_val
,col_name
FROM
#em
WHERE
stat_name = ''m'''
,
--@Coef
'SELECT
*
FROM
#E
WHERE recno = ' + cast(recno as varchar(max)), --@New_x
1 --@UseNames
) as yhat
FROM #E;
This produces the following result.
{"columns":[{"field":"recno","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"x1","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"x2","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"x3","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"x4","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"y","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"yhat","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"recno":"1","x1":"3.95","x2":"1.75","x3":"4.41","x4":"49.00","y":"508.78","yhat":"508.878071578415"},{"recno":"2","x1":"3.72","x2":"1.17","x3":"3.48","x4":"46.72","y":"276.59","yhat":"295.73675593137"},{"recno":"3","x1":"2.35","x2":"1.64","x3":"3.98","x4":"13.03","y":"82.07","yhat":"84.3271554575853"},{"recno":"4","x1":"5.08","x2":"1.57","x3":"3.21","x4":"16.32","y":"225.66","yhat":"235.952812223621"},{"recno":"5","x1":"3.31","x2":"1.19","x3":"3.73","x4":"34.16","y":"176.03","yhat":"165.786484274664"},{"recno":"6","x1":"4.46","x2":"1.40","x3":"4.36","x4":"14.62","y":"148.20","yhat":"149.492754447672"},{"recno":"7","x1":"5.51","x2":"1.63","x3":"3.74","x4":"27.97","y":"357.56","yhat":"372.157530219639"},{"recno":"8","x1":"3.74","x2":"1.72","x3":"3.94","x4":"48.80","y":"470.35","yhat":"465.717867884682"},{"recno":"9","x1":"4.76","x2":"1.55","x3":"3.37","x4":"25.84","y":"308.70","yhat":"278.054533462922"},{"recno":"10","x1":"5.03","x2":"1.53","x3":"4.37","x4":"17.60","y":"219.60","yhat":"216.729909517558"}]}
Example #2
Using the same regression coefficients as in the previous example, we set @UseNames = 0. This means that EXPPRED will not use the regression coefficient names to match to the independent variables but will use the idx values instead which refer to the relative column position in the resultant table from the @New_x SQL. In other words, idx = 1 means the first column, idx = 2 means the second column, etc.
SELECT recno,
wct.EXPPRED(
'SELECT idx,stat_val FROM #em WHERE stat_name = ''m''',
'SELECT x0,x1,x2,x3,x4 FROM #E WHERE recno = ' + cast(recno
as varchar(max)),
0
) as Yhat
FROM #E;
This produces the following result.
{"columns":[{"field":"recno","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"Yhat","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"recno":"1","Yhat":"508.878071578415"},{"recno":"2","Yhat":"295.73675593137"},{"recno":"3","Yhat":"84.3271554575853"},{"recno":"4","Yhat":"235.952812223621"},{"recno":"5","Yhat":"165.786484274664"},{"recno":"6","Yhat":"149.492754447672"},{"recno":"7","Yhat":"372.157530219639"},{"recno":"8","Yhat":"465.717867884682"},{"recno":"9","Yhat":"278.054533462922"},{"recno":"10","Yhat":"216.729909517558"}]}
Example #3
Using the same regression coefficients as Example #1 this example shows how to calculate the predicted value when the new x-values are passed in as a comma separated string rather than as a SELECT statement. We use the built-in CONCAT function to build the string a derived table and then pass that value into EXPPRED .
SELECT wct.EXPPRED('SELECT idx,stat_val FROM #em WHERE stat_name = ''m''', newx,
0) as Yhat
FROM
(
SELECT CONCAT(x0, ',', x1, ',', x2, ',', x3, ',', x4) as newx
FROM #E
) n;
This produces the following result.
{"columns":[{"field":"Yhat","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"Yhat":"508.878071578415"},{"Yhat":"295.73675593137"},{"Yhat":"84.3271554575853"},{"Yhat":"235.952812223621"},{"Yhat":"165.786484274664"},{"Yhat":"149.492754447672"},{"Yhat":"372.157530219639"},{"Yhat":"465.717867884682"},{"Yhat":"278.054533462922"},{"Yhat":"216.729909517558"}]}
See Also
LOGEST - Logarithmic regression
LOGEST_Q - Exponential curve that best fits a series of x- and y-values
GROWTH - calculate predicted exponential growth using existing values