section3
Simulating the regression model
#What does it mean that the estimates are unbiased? #try different types of noisel rt(df=10),rchisq(2) and
so on.
n=
50
x=
runif
(
50
)
##the x s stay constant
howmany=
1000
b0vals=
1
:
1000
b1vals=
1
:
1000
for
(i
in
1
:
howmany) {
y=
20
+
2
*
x
+
rnorm
(n,
0
,.
1
)
fit=
lm
(y
~
x)
b0vals[i] =
coef
(fit)[
1
]
b1vals[i] =
coef
(fit)[
2
]
}
#what should the mean equal?
mean
(b1vals)
## [1] 2.000419
hist
(b1vals)
Histogram of b1vals
b1vals
Frequency
1.8
1.9
2.0
2.1
2.2
0
100
200
300
1

** Subscribe** to view the full document.

Model Assumptions
Recall the simple linear regression model statement,
Y
i
=
β
0
+
β
1
x
i
1
+ +
i
where
i
∼
N
(0
, σ
2
)
.
Often, the
assumptions of linear regression
, are stated as,
•
L
inearity: the response can be written as a linear combination of the predictors. (With noise about
this true linear relationship.)
•
I
ndependence: the errors are independent.
•
N
ormality: the distribution of the errors should follow a normal distribution.
•
E
qual Variance: the error variance is the same at any set of predictor values.
The linearity assumption is encoded as
β
0
+
β
1
x
i
1
,
while the remaining three, are all encoded in
i
∼
N
(0
, σ
2
)
,
since the
i
are
iid
normal random variables with constant variance. If these assumptions are met, great! We
can perform inference,
and it is valid
. If these assumptions are
not
met, we can still “perform” a
t
-test
using
R
, but the results are
not valid
. The distributions of the parameter estimates will not be what we
expect. Hypothesis tests will then accept or reject incorrectly. Essentially,
garbage in, garbage out.
Checking Assumptions
We’ll now look at a number of tools for checking the assumptions of a linear model. To test these tools, we’ll
use data simulated from three models:
Model 1:
Y
= 3 + 5
x
+
,
∼
N
(0
,
1)
Model 2:
Y
= 3 + 5
x
+
,
∼
N
(0
, x
2
)
Model 3:
Y
= 3 + 5
x
2
+
,
∼
N
(0
,
25)
sim_
1
=
function
(
sample_size =
500
) {
x =
runif
(
n =
sample_size)
*
5
y =
3
+
5
*
x
+
rnorm
(
n =
sample_size,
mean =
0
,
sd =
1
)
data.frame
(x, y)
}
sim_
2
=
function
(
sample_size =
500
) {
x =
runif
(
n =
sample_size)
*
5
y =
3
+
5
*
x
+
rnorm
(
n =
sample_size,
mean =
0
,
sd =
x)
data.frame
(x, y)
}
sim_
3
=
function
(
sample_size =
500
) {
x =
runif
(
n =
sample_size)
*
5
y =
3
+
5
*
x
^
2
+
rnorm
(
n =
sample_size,
mean =
0
,
sd =
5
)
data.frame
(x, y)
}
2

Fitted versus Residuals Plot
Probably our most useful tool will be a
Fitted versus Residuals Plot
. It will be useful for checking both
the
linearity
and
constant variance
assumptions.
Data generated from Model 1 above should not show any signs of violating assumptions, so we’ll use this
to see what a good fitted versus residuals plot should look like. First, we’ll simulate observations from this
model.
set.seed
(
42
)
sim_data_
1
=
sim_1
()
#head(sim_data_1)
We then fit the model and add the fitted line to a scatterplot.
plot
(y
~
x,
data =
sim_data_
1
,
col =
"grey"
,
pch =
20
,
main =
"Data from Model 1"
)
fit_
1
=
lm
(y
~
x,
data =
sim_data_
1
)
abline
(fit_
1
,
col =
"darkorange"
,
lwd =
3
)
0
1
2
3
4
5
5
10
15
20
25
30
Data from Model 1
x
y
We now plot a fitted versus residuals plot. Note, this is residuals on the
y
-axis despite the ordering in the
name. Sometimes you will see this called a residuals versus fitted, or residuals versus predicted plot.

** Subscribe** to view the full document.

- Spring '11
- Grandal