forked from mca91/EconometricsWithR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex9_2.html
123 lines (89 loc) · 3.3 KB
/
ex9_2.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'/>
<script async='async' src='https://cdn.datacamp.com/dcl-react-dev.js.gz'></script>
<style>
.DCexercise .datacamp-exercise {
border: 2px solid #3D678D;
border-radius: 10px 10px 10px 10px !important;
}
*[class*="lm_"], .ace_gutter, textarea[class*="ace_"], .ace_scroller {
background-color: #F0EFF0 !important;
}
div[class*="Editor-module__editor"], div[class*="dcl__Footer"] {
background-color: #3D678D !important;
}
*[class*="lm_"] {
border-radius: 10px 10px 0px 0px !important;
}
div[class*="dcl__Footer"] {
border-radius: 0px 0px 10px 10px !important;
}
.lm_content {
border-radius: 0px !important;
}
.lm_splitter {
background-color: #3D678D !important;
width: 3px !important;
}
.lm_drag_handle {
background: transparent !important;
}
div[class*="dcl__index-module"] {
outline-color: transparent !important;
border-radius: 10px !important;
}
button[class*="secondary-light"] {
background-color: #FF0000;
}
</style>
</head>
<body>
<div data-datacamp-exercise data-lang="r" data-height="auto">
<code data-type="pre-exercise-code">
</code>
<code data-type="sample-code">
# set seed for reproducibility
set.seed(123)
# attach the package `mvtnorm`
# set the number of sampling iterations
N <-
# initialize the vector `beta_hats`
beta_hats <-
# conduct the simulation using `for()`
for(i in 1:N) {
}
# compute the sample mean of the estimates
</code>
<code data-type="solution">
# set seed for reproducibility
set.seed(123)
# attach the package `mvtnorm`
library(mvtnorm)
# set number of sampling iterations
N <- 1000
# initialize the vector `beta_hats`
beta_hats <- c()
# loop estimation
for (i in 1:N) {
# simulate the dataset and set the column names
d <- data.frame(rmvnorm(1000, c(50, 100), sigma = cbind(c(10, 5), c(5, 10))))
colnames(d) <- c("X", "Y")
# add the measurement error to `X`
d[,1] <- d[,1] + rnorm(100,0,sqrt(10))
# estimate the simple linear regression model
ms_mod <- lm(Y ~ X, data = d)
# save the estimate
beta_hats[i] <- ms_mod$coefficients[2]
}
# compute the sample mean of the estimates
mean(beta_hats)
</code>
<code data-type="sct">
test_object("beta_hats")
test_output_contains("mean(beta_hats)")
success_msg("Well done! The sample mean should be close to 0.75 but it is about 0.25 due to the downward bias implied by the measurement error in X. Increase the sample size does not alleviate the bias (check this!) because OLS is inconsistent here.")
</code>
</div></body>
</html>