Eric Kim
Eric Kim

Reputation: 2698

Multiple linear regression with fixed coefficient for a feature

Linear regression with two features can be described by the following equation:

y = a1x1 + a2x2 + intercept

Fitting multiple linear regression will solve for the coefficients a1, and a2. Consider the following code:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model

file = 'https://aegis4048.github.io/downloads/notebooks/sample_data/unconv_MV_v5.csv'
df = pd.read_csv(file)[['Por', 'Perm', 'Prod']]

features = df[['Por', 'Perm']].values.reshape(-1,2)
target = df['Prod']

ols = linear_model.LinearRegression()
model = ols.fit(features, target)
predicted = model.predict(features)

coef = model.coef_

pd.DataFrame(coef, index=['Por', 'Perm'], columns=['Regression Coef']).round(2)

>>>         Regression Coef
    Por              244.47
    Perm              97.75

The two features are Por and Perm. I want to fix the values of the regression coefficient of Perm to some fixed value, and solve only for the coefficient of Por. How can I do this in Python?

Upvotes: 1

Views: 902

Answers (1)

adrianp
adrianp

Reputation: 1019

Say Por is a2. Once you set the value of a2 to a fixed value A2, then your linear regression would be reduced to y(a1) = a1x1 + (A2x2 + intercept). Therefore, you can simply solve the simple linear regression y(a1) = a1x1 + intercept_new, where intercept_new would already take into account setting Por to a constant value.

Upvotes: 2

Related Questions