R与数据分析旧笔记(十)非线性模型

时间:2022-03-07 20:11:25

非线性模型

非线性模型

例子:销售额x与流通费率y

> x=c(1.5,2.8,4.5,7.5,10.5,13.5,15.1,16.5,19.5,22.5,24.5,26.5)
> y=c(7.0,5.5,4.6,3.6,2.9,2.7,2.5,2.4,2.2,2.1,1.9,1.8)
> plot(x,y)

R与数据分析旧笔记(十)非线性模型

1.直线回归

> lm.1=lm(y~x)
> summary(lm.1)

Call:
lm(formula = y ~ x)

Residuals:
Min 1Q Median 3Q Max
-0.9179 -0.5537 -0.1628 0.3953 1.6519

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 5.60316 0.43474 12.889 1.49e-07 ***
x -0.17003 0.02719 -6.254 9.46e-05 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.7701 on 10 degrees of freedom
Multiple R-squared: 0.7964, Adjusted R-squared: 0.776
F-statistic: 39.11 on 1 and 10 DF, p-value: 9.456e-05

R与数据分析旧笔记(十)非线性模型,不理想

2.对数法,R与数据分析旧笔记(十)非线性模型

> lm.log=lm(y~log(x))
> summary(lm.log)

Call:
lm(formula = y ~ log(x))

Residuals:
Min 1Q Median 3Q Max
-0.33291 -0.10133 -0.04693 0.16512 0.34844

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 7.3639 0.1688 43.64 9.60e-13 ***
log(x) -1.7568 0.0677 -25.95 1.66e-10 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.2064 on 10 degrees of freedom
Multiple R-squared: 0.9854, Adjusted R-squared: 0.9839
F-statistic: 673.5 on 1 and 10 DF, p-value: 1.66e-10
> plot(x,y);lines(x,fitted(lm.log))

R与数据分析旧笔记(十)非线性模型

比直线回归拟合效果要好,但还是有不足

3.指数法,R与数据分析旧笔记(十)非线性模型

> lm.exp=lm(log(y)~x)
> summary(lm.exp)

Call:
lm(formula = log(y) ~ x)

Residuals:
Min 1Q Median 3Q Max
-0.18246 -0.10664 -0.01670 0.08079 0.25946

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.759664 0.075101 23.43 4.54e-10 ***
x -0.048809 0.004697 -10.39 1.12e-06 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.133 on 10 degrees of freedom
Multiple R-squared: 0.9153, Adjusted R-squared: 0.9068
F-statistic: 108 on 1 and 10 DF, p-value: 1.116e-06

> plot(x,y);lines(x,exp(fitted(lm.exp)))

R与数据分析旧笔记(十)非线性模型

效果并无太大变化,反而更糟

4.幂函数法,R与数据分析旧笔记(十)非线性模型

> lm.pow=lm(log(y)~log(x))
> summary(lm.pow)

Call:
lm(formula = log(y) ~ log(x))

Residuals:
Min 1Q Median 3Q Max
-0.054727 -0.020805 0.004548 0.024617 0.045896

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.19073 0.02951 74.23 4.81e-15 ***
log(x) -0.47243 0.01184 -39.90 2.34e-12 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.0361 on 10 degrees of freedom
Multiple R-squared: 0.9938, Adjusted R-squared: 0.9931
F-statistic: 1592 on 1 and 10 DF, p-value: 2.337e-12

> plot(x,y);lines(x,exp(fitted(lm.pow)))

R与数据分析旧笔记(十)非线性模型

检验结果来看R与数据分析旧笔记(十)非线性模型上升了,也通过了假设检验,从图上来看拟合效果非常好