Search code examples
pythonlinear-regressionstatsmodelscoefficients

Is there possibility to specify a linear regression using a OLS from statsmodels in way that some coefficients are declare manually?


I want to build linear regression by using OLS from statsmodels. I have a question about manually declaration of coefficients for some explanatory variables.

Is there possibility to parameterize a model in way that for 4 from 10 variables I will put manually coefficients and for rest the fit method will count the values of them?

Or maybe you know a different way haw to do it?

Many thanks for all answers!

MP


Solution

  • Yes, in two steps. First you take those manual coefficients, multiply them with the corresponding ('manual') variables to get vectors, and subtract them from the target. Then, you can take a normal OLS and get the coefficients of the remaining variables.

    Say you have two variables, x1 and x2, and want to set the weight for w1. You can simply infer

    w2 =(x2**T * x2) ** (-1) * x2 ** T * (y - w1 * x1)