R Code for Simple Linear Regression
R Code for Simple Linear Regression
(Part 2)
• setwd(’path..’) ... to change the directory for data loading and saving
• object = lm(y ∼ x)... to call “lm” to estimate a model and stored the calculation
results in ”object”
Example 1.1 Suppose we have 10 observations for (X, Y ): (1.2, 1.91), (2.3, 4.50), (3.5,
2.13), (4.9, 5.77), (5.9, 7.40), (7.1, 6.56), (8.3, 8.79), (9.2, 6.56), (10.5, 11.14), (11.5, 9.88).
They are stored in file (data010201.dat). We hope to fit a linear regression model
Yi = β0 + β1 Xi + εi , i = 1, ..., n
1
X = mydata$V1 # select X
Y = mydata$V2 # select Y
plot(X, Y) # plot the observations (data)
myreg = lm(Y ∼ X) # do the linear regression
summary(myreg) # output the estimation
Coefficients:
Estimate Std. Error t value P r(> |t|)
(Intercept) 1.3931 0.9726 1.432 0.189932
X 0.7874 0.1343 5.862 0.000378 ***
—
Sign. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
6
4
2
2 4 6 8 10
Figure 1: (R code)
2
The fitted regression line/model is
Ŷ = 1.3931 + 0.7874X
Ŷ = b0 + b1 X .
Ȳ = β0 + β1 X̄ + ε̄
and
n
(X − X̄)(Yi − Ȳ )
b1 = n i
i=1
(Xi − X̄)2
n i=1
i=1 (Xi − X̄)(β
0 + β1 Xi + εi − β0 − β1 X̄ − ε̄)
= n 2
i=1 (Xi − X̄)
n
(X − X̄)(εi − ε̄)
= β1 + i=1 n i
(Xi − X̄)2
n i=1
(Xi − X̄)εi
= β1 + i=1n 2
i=1 (Xi − X̄)
Eb1 = β1 .
3
For b0 ,
= β0
σ2 1 2 X̄ 2
V ar(b1 ) = n 2
, V ar(b0 ) = σ + n 2
σ2
i=1 (Xi − X̄) n i=1 (Xi − X̄)
[Proof:
n
(Xi − X̄)εi
V ar(b1 ) = V ar( i=1
n 2
)
i=1 (Xi − X̄)
n n
2 −2
= { (Xi − X̄) } V ar{ (Xi − X̄)εi }
i=1 i=1
n
n
= { (Xi − X̄)2 }−2 (Xi − X̄)2 σ 2
i=1 i=1
σ2
= n .
i=1 (Xi − X̄)2
[Proof:
E(Ŷi ) = E(b0 + b1 Xi ) = E(b0 ) + E(b1 )Xi = β0 + β1 Xi = EYi
4
and ei = Yi − Ŷi = Yi − b0 − b1 Xi . It follows
n
ei = 0
i=1
n
Xi ei = 0
i=1
n
n
• Yi = Ŷi
i=1 i=1
n
• e2i = minb0 ,b1 {Q}
i=1
n
• Xi ei = 0
i=1
n
• Ŷi ei = 0
i=1
sY
b1 = rX,Y
sX
where
n n
(Xi − X̄)2 (Yi − Ȳ )2
i=1
sX = , sY = i=1
n−1 n−1
n
(Xi − X̄)(Yi − Ȳ )
rX,Y = i=1 n
n 2 2
i=1 (Xi − X̄) i=1 (Yi − Ȳ )
5
• Estimate σ 2 by
n
n
(Yi − Ŷi )2 e2i
i=1 i=1
s2 = =
n−2 n−2
called mean squared error (MSE), i.e.
n
e2i
i=1
M SE =
n−2
or denoted by σ̂ 2 .
[Proof: For any ξ1 , ..., ξn IID with mean μ and variance σ 2 , we have
n
n
E ¯2=E
(ξi − ξ) [(ξi − μ) − (ξ̄ − μ)]2
i=1 i=1
n
= E{ (ξi − μ)2 − n(ξ̄ − μ)2 }
i=1
n
= V ar(ξ) − nV ar(ξ̄)
i=1
= nσ 2 − σ 2
= (n − 1)σ 2
6
similarly, for any i,
1 2
V ar(ξi − ξ̄) = (1 − )σ .
n
Thus
E(s2 ) = σ 2
or
√
σ̂ = M SE = 1.405702
7
3 Regression Without Predictors
At first glance, it doesn’t seem that studying regression without predictors would be very
useful. Certainly, we are not suggesting that using regression without predictors is a major
data analysis tool. We do think that it is worthwhile to look at regression models without
predictors to see what they can tell us about the nature of the constant. Understanding the
regression constant in these simpler models will help us to understand both the constant
and the other regression coefficients in later more complex models.
Model
Yi = β0 + εi , i = 1, 2, ..., n.
Note that
n
dQ
= −2 {Yi − b0 }
db0
i=1
b0 = Ȳ .
8
• Can you prove the estimator is unbiased, i.e Eb0 = β0 ?
• How to estimate σ 2 ?
n
1 2
σ̂ 2 = ei
n−1
i=1
Why it is divided by n − 1?
4 Inference in regression
Y1 = β0 + β1 X1 + ε1
Y2 = β0 + β1 X2 + ε2
..
. (1)
Yn = β0 + β1 Xn + εn
E(Yi ) = β0 + β1 Xi
and
V ar(Yi ) = σ 2
4.1 Inference of β1
We need to check whether β1 = 0 (or any other specified value, say -1.5), why
9
• To see whether the model can be simplified (if β1 = 0, the model becomes Yi = β0 +εi ,
a regression model without predictors.) For example, Hypotheses H0 : β1 = 0 v.s.
Ha : β1 = 0
Proof Recall the fact that any linear combination of independent normal distributed random
variables is still normal. To find its distribution, we only need to find its mean and variance.
Since Yi are normal and independent, thus b1 is normal, and
Eb1 = β1
s(b1 ) is the Standard Error (or S.E.) of b1 , (or called Standard deviation)
sample distribution of (b1 − β1 )/s(b1 )
b1 − β1
follows t(n − 2) for model (1)
s(b1 )
Confidence interval for β1 . Let t1−α/2 (n−2) or t(1−α/2, n−2) the 1−α/2−quantile
of t(n − 2).
10
By symmetry of the distribution, we have
i.e.
b1 − t(1 − α/2, n − 2) ∗ s(b1 ) ≤ β1 ≤ b1 + t(1 − α/2, n − 2) ∗ s(b1 )
Example 4.2 For the example above, find the 95% confidence interval for β1 ?
solution: since n = 10, we have t(1 − 0.05/2, 8) = 2.306; the SE for b1 is s(b1 ) = 0.1343.
Thus the confidence interval is
Test of β1
H0 : β0 = 0, Ha : β1 = 0
b1
t= ∼ t(n − 2)
s(b1 )
The test can also be done based on the p-value, defined as p = P (|t| > |t∗ |). It is
easy to see that
p-value < α ⇐⇒ |t∗ | > t(1 − α/2; n − 2)
Thus
11
– If p-value ≥ α, then accept H0 .
H0 : β1 ≥ 0, Ha : β1 < 0
Under H0 , we have
b1 b1 − β1 β1
t= = + ∼ t(n − 2) + a positive term
s(b1 ) s(b1 ) s(b1 )
Suppose the significance level is α (usually, 0.05, 0.01). Calculate t, say t∗
Sample distribution of b0
b0 = Ȳ − b1 X̄
Let ki = n(Xi −X̄) 2 , then (see the proof at the beginning of this part)
i=1 (Xi −X̄)
n
b1 = β1 + ki εi .
i=1
Thus
n n n
1 1
b0 = β0 + εi − ki εi = β0 + [ − ki X̄]εi
n n
i=1 i=1 i=1
The variance is
n
1 1 X̄ 2
V ar(b0 ) = [ − ki X̄]2 σ 2 = [ + n 2
]σ 2
n
i=1
n i=1 (X i − X̄)
12
Therefore the Theorem follows.]
Estimated Variance of b0 (by replacing σ 2 with MSE).
1 X̄ 2
s2 (b0 ) = M SE + n 2
n i=1 (Xi − X̄)
s(b0 ) is the Standard Error (or S.E.) of b0 , (or called Standard deviation)
Sample distribution of (b0 − β0 )/s(b0 )
b0 − β0
follows t(n − 2) for model (1)
s(b0 )
Test of β0
H0 : β0 = 0, Ha : β0 = 0
Under H0 , we have
b0
t= ∼ t(n − 2)
s(b0 )
Suppose the significance level is α (usually, 0.05, 0.01). If the calculated t, say t∗
Similarly, the test can also be done based on the p-value, defined as p = P (|t| > |t∗ |).
It is easy to see that
Thus
13
– If p-value < α, then reject H0 .
H0 : β0 ≤ 0, Ha : β0 > 0
Example 4.4 For the example above, with significance level 0.05,
1. Test H0 : β0 = 0 versus H1 : β0 = 0
Answer:
1. since n = 10, t(0.975, 8) = 2.306. |t∗ | = 1.432 < 2.306. Thus, we accept H0
2. The t-value is |t∗ | = 5.862 > 2.306, thus we reject H0 , i.e. b1 is significantly different
from 0.
14