In regression analysis, if there are two or more independent variables, it is called multiple regression. In fact, a phenomenon is often associated with multiple factors. It is more effective and practical to predict or estimate the dependent variable by the optimal combination of multiple independent variables than to predict or estimate only one independent variable. Therefore, multivariate linear regression is more practical than univariate linear regression.
Multivariate linear regression is similar to univariate linear regression. The least square method can be used to estimate the model parameters, and the model and model parameters need to be statistically tested.
Data centralization is different from standardization. Centralization is to subtract the average from the original data, while standardization is to subtract the average from the original data and then divide it by the standard deviation. The data obtained is the data with 0 as the average and 1 as the standard deviation. The purpose of data centralization is to unify the scale of data of different variables.
raw data
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 5 10:06:41 2021
@author: Machi
"""
import pandas as pd
import statsmodels.formula.api as smf
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from statsmodels.stats.api import anova_lm
# Read the data in CSV
df = pd.read_csv('data.csv')
print(df)
# We use Pearson coefficient to calculate the correlation between columns
c_matrix = df.corr(method='pearson')
print(c_matrix)
# Draw a three-dimensional scatter diagram
fig=plt.figure()
ax1 = Axes3D(fig)
ax1.scatter3D(df['x1'],df['x2'],df['y'], cmap='Blues')
plt.show()
# Substitute the data for fitting
result = smf.ols('y~x1+x2-1',data = df).fit()
print(result.summary())
y_fitted = result.fittedvalues
# Analysis of variance was performed for each coefficient
table = anova_lm(result, typ=3)
print(table)
# Centralization and standardization
dfcenter = df-df.mean()
dfnorm = (df-df.mean())/df.std()
print(dfnorm)
output



