回归分析是一种预测性的建模技术,它研究的是因变量(目标)和自变量(预测器)之间的关系。本文简单的介绍一下多元线性回归。
Multiple Linear Regression
Data Preprocessing
1 读入数据集
# 导入数据
代码语言:javascript复制setwd("C:\Users\****\Documents\ML\day3\")dataset = read.csv('50_Startups.csv')
head(dataset)
R.D.Spend Administration Marketing.Spend State Profit
1 165349.2 136897.80 471784.1 New York 192261.8
2 162597.7 151377.59 443898.5 California 191792.1
3 153441.5 101145.55 407934.5 Florida 191050.4
4 144372.4 118671.85 383199.6 New York 182902.0
5 142107.3 91391.77 366168.4 Florida 166187.9
6 131876.9 99814.71 362861.4 New York 156991.1
2 数据预处理
代码语言:javascript复制# 虚拟变量
dataset$State = factor(dataset$State,
levels = c('New York', 'California', 'Florida'),
labels = c(1, 2, 3))
3 训练集和测试集
将数据按照4:1拆分,每一组分别包含自变量和因变量
代码语言:javascript复制# install.packages('caTools')
library(caTools)
set.seed(123)
split = sample.split(dataset$Profit, SplitRatio = 0.8)
training_set = subset(dataset, split == TRUE) # 多自变量
test_set = subset(dataset, split == FALSE) # 单因变量dim(training_set)
[1] 40 5
dim(test_set)
[1] 10 5
4 模型拟合及预测
通过训练集进行模型拟合得到曲线,然后将测试集的X_test带入曲线中,得到预测结果y_pred,最后将预测结果y_pred与测试集中的y_test进行比较,确定预测是否准确。
4.1 多重线性回归
代码语言:javascript复制regres = lm(formula = Profit ~ R.D.Spend Administration Marketing.Spend State,
data = training_set)summary(regres)Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 4.965e 04 7.637e 03 6.501 1.94e-07 ***
R.D.Spend 7.986e-01 5.604e-02 14.251 6.70e-16 ***
Administration -2.942e-02 5.828e-02 -0.505 0.617
Marketing.Spend 3.268e-02 2.127e-02 1.537 0.134
State2 1.213e 02 3.751e 03 0.032 0.974
State3 2.376e 02 4.127e 03 0.058 0.954
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
4.2 进行逐步回归分析
代码语言:javascript复制regres.step <- step(regres)
Step: AIC=735.89
Profit ~ R.D.Spend Marketing.Spend Df Sum of Sq RSS AIC
<none> 3.3627e 09 735.89
- Marketing.Spend 1 3.1338e 08 3.6761e 09 737.45
- R.D.Spend 1 2.3344e 10 2.6706e 10 816.77regres2 = lm(formula = Profit ~ R.D.Spend Marketing.Spend ,
data = training_set)
4.3 预测结果
代码语言:javascript复制y_pred = predict(regres2, newdata = test_set)y_pred
4 5 8 11 16 20 21 24
173687.21 171299.96 160499.08 134783.16 145873.04 114467.75 117025.30 110369.71
31 32
98447.39 97668.22test_set$Profit
[1] 182901.99 166187.94 155752.60 146121.95 129917.04 122776.86 118474.03 108733.99
[9] 99937.59 97483.56
4.4 结果可视化
代码语言:javascript复制plot(test_set$Profit,col="red")points(y_pred,col="blue")
5 参考资料
https://github.com/Avik-Jain/100-Days-Of-ML-Code