-
Notifications
You must be signed in to change notification settings - Fork 1
/
swissLMs.R
45 lines (43 loc) · 1.34 KB
/
swissLMs.R
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
makelms <- function(){
# Store the coefficient of linear models with different independent variables
cf <- c(coef(lm(Fertility ~ Agriculture, swiss))[2],
coef(lm(Fertility ~ Agriculture + Catholic,swiss))[2],
coef(lm(Fertility ~ Agriculture + Catholic + Education,swiss))[2],
coef(lm(Fertility ~ Agriculture + Catholic + Education + Examination,swiss))[2],
coef(lm(Fertility ~ Agriculture + Catholic + Education + Examination +Infant.Mortality, swiss))[2])
print(cf)
}
# Regressor generation process 1.
rgp1 <- function(){
print("Processing. Please wait.")
# number of samples per simulation
n <- 100
# number of simulations
nosim <- 1000
# set seed for reproducability
set.seed(4321)
# Point A:
x1 <- rnorm(n)
x2 <- rnorm(n)
x3 <- rnorm(n)
# Point B:
betas <- sapply(1 : nosim, function(i)makelms(x1, x2, x3))
round(apply(betas, 1, var), 5)
}
# Regressor generation process 2.
rgp2 <- function(){
print("Processing. Please wait.")
# number of samples per simulation
n <- 100
# number of simulations
nosim <- 1000
# set seed for reproducability
set.seed(4321)
# Point C:
x1 <- rnorm(n)
x2 <- x1/sqrt(2) + rnorm(n) /sqrt(2)
x3 <- x1 * 0.95 + rnorm(n) * sqrt(1 - 0.95^2)
# Point D:
betas <- sapply(1 : nosim, function(i)makelms(x1, x2, x3))
round(apply(betas, 1, var), 5)
}