9
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.api as sm
# Sample data: Housing Prices and Square Footage
data = {
'sqft': [1500, 1700, 1900, 2000, 2100, 2300, 2500],
'price': [200000, 220000, 240000, 260000, 290000, 320000,
350000]
}
df = pd.DataFrame(data)
# Add the spline feature: (sqft - 2000)+
df['spline'] = np.where(df['sqft'] > 2000, df['sqft'] -
2000, 0)
# Design matrix
X = sm.add_constant(df[['sqft', 'spline']]) # Intercept + sqft + spline term
y = df['price']
# Fit the model
model = sm.OLS(y, X).fit()
# Output the results
print(model.summary())
Comments
Post a Comment