ISLR;R语言; 机器学习 ;线性回归
一些专业词汇只知道英语的,中文可能不标准,请轻喷
12.没有截距的简单线性回归
a)观察3.38式可发现
当x^2之和与y^2之和相等时,具有相同的参数估计。
b)
set.seed(1)
x=rnorm(100)
y=2*x
lm.fit=lm(y~x+0)
lm.fit2=lm(x~y+0)
summary(lm.fit)
输出结果:
Call:
lm(formula = y ~ x + 0)
Residuals:
Min 1Q Median 3Q Max
-3.776e-16 -3.378e-17 2.680e-18 6.113e-17 5.105e-16
Coefficients:
Estimate Std. Error t value Pr(>|t|)
x 2.000e+00 1.296e-17 1.543e+17 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 1.167e-16 on 99 degrees of freedom
Multiple R-squared: 1, Adjusted R-squared: 1
F-statistic: 2.382e+34 on 1 and 99 DF, p-value: < 2.2e-16
线性回归2:
summary(lm.fit2)
输出结果:
Call:
lm(formula = x ~ y + 0)
Residuals:
Min 1Q Median 3Q Max
-1.888e-16 -1.689e-17 1.339e-18 3.057e-17 2.552e-16
Coefficients:
Estimate Std. Error t value Pr(>|t|)
y 5.00e-01 3.24e-18 1.543e+17 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 5.833e-17 on 99 degrees of freedom
Multiple R-squared: 1, Adjusted R-squared: 1
F-statistic: 2.382e+34 on 1 and 99 DF, p-value: < 2.2e-16
实验发现回归参数不同
c)
sample()函数能够从指定的特定对象集合中随机取样,通过指定某类对象的向量x,然后从中取样size。
例如,从整数1到10中取样,并从中不放回地抽取4个数字使用sample(1:10, 4)
,得到3、4、5、7。如果再做一遍得到的是3、9、8、5。因为选择不放回取样,所以不会得到重复的数字。
> set.seed(1)
> x=rnorm(100)
> y=sample(x,100)
> sum(x^2)
[1] 81.05509
> sum(y^2)
[1] 81.05509
> lm.fit=lm(y~x+0)
> lm.fit2=lm(x~y+0)
> summary(lm.fit)
输出结果:
Call:
lm(formula = y ~ x + 0)
Residuals:
Min 1Q Median 3Q Max
-2.2315 -0.5124 0.1027 0.6877 2.3926
Coefficients:
Estimate Std. Error t value Pr(>|t|)
x 0.02148 0.10048 0.214 0.831
Residual standard error: 0.9046 on 99 degrees of freedom
Multiple R-squared: 0.0004614, Adjusted R-squared: -0.009635
F-statistic: 0.0457 on 1 and 99 DF, p-value: 0.8312
线性回归2:
Call:
lm(formula = x ~ y + 0)
Residuals:
Min 1Q Median 3Q Max
-2.2400 -0.5154 0.1213 0.6788 2.3959
Coefficients:
Estimate Std. Error t value Pr(>|t|)
y 0.02148 0.10048 0.214 0.831
Residua