Question

I have one regression function, g1(x) = 5x - 1 for one data point. I have another regression function, g2(x) = 3x + 4.

I want to add these two models to create a final regression model, G(x).

That means:

G(x) = g1(x) + g2(x) 
=> 5x - 1 + 3x + 4
=> 8x +3 

My question is, how can this be done in python? If my dataset is X, I'm using statsmodels like this:

import statsmodels.api as sm
from statsmodels.sandbox.regression.predstd import wls_prediction_std
import numpy as np

mod_wls = sm.WLS(y, X)
res_wls = mod_wls.fit()
print res_wls.params

And that gives me the coefficients for the regression function that fits the data X. To add the functions, I can easily grab the coefficients for each, and sum them up to get the coefficients for a new regression function such as G(x). But now that I've got my own coefficients, how can I convert them into a regression function and use them to predict a new data? Because as far as I know, models have to be "fitted" to data before they can be used for prediction.

Or is there any way to directly add regression functions? I'm going to be adding functions iteratively in my algorithm.

Was it helpful?

Solution

The prediction generated by this model should be exactly

np.dot(X_test, res_wls.params)

Thus, if you want to sum several models, e.g.

summed_params = np.array([res_wls.params for res_wls in all_my_res_wls]).sum(axis=0)

your prediction should be

np.dot(X_test, summed_params)

In this case there would be no need to use the built-in functions of the estimator.

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