Simulation Study 1
library(magrittr)
library(tidyverse)
Registered S3 methods overwritten by 'dbplyr':
method from
print.tbl_lazy
print.tbl_sql
── Attaching packages ────────────────────────────────────────────────── tidyverse 1.3.2 ──✔ ggplot2 3.4.0 ✔ purrr 1.0.0
✔ tibble 3.1.8 ✔ dplyr 1.0.10
✔ tidyr 1.2.1 ✔ stringr 1.5.0
✔ readr 2.1.3 ✔ forcats 0.5.2 ── Conflicts ───────────────────────────────────────────────────── tidyverse_conflicts() ──
✖ tidyr::extract() masks magrittr::extract()
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
✖ purrr::set_names() masks magrittr::set_names()
sim1 <- function(i){
x1 <- rnorm(1000, 2)
x2 <- rnorm(1000, -4)
y <- 3 + 2 * x1 + 4 * x2 + rnorm(1000, sd = sqrt(2))
df <- tibble(x1, x2, y)
res <- df %$% lm(y ~ x1 + x2)
return(c(coef(res),sigma(res)))
}
sim2 <- function(i){
x1 <- rnorm(1000, 2)
x2 <- rnorm(1000, -4)
y <- 3 + 2 * x1 + 4 * x2 + rnorm(1000, sd = sqrt(2))
df <- tibble(x1, x2, y)
res <- df %$% lm(y ~ x1)
return(c(coef(res),sigma(res)))
}
# sim1()
print("Correct Model")
[1] "Correct Model"
print("Average estimate")
[1] "Average estimate"
results1 <- sapply(1:1000, sim1)
rowMeans(results1)
(Intercept) x1 x2
3.000889 1.996928 3.998753 1.413139
print("Theoretical Standard Error Estimates")
[1] "Theoretical Standard Error Estimates"
apply(results1, 1, sd)
(Intercept) x1 x2
0.20244465 0.04521681 0.04406489 0.03066468
print("Incorrect Model")
[1] "Incorrect Model"
print("Average estimate")
[1] "Average estimate"
results2 <- sapply(1:1000, sim2)
rowMeans(results2)
(Intercept) x1
-12.994301 1.998836 4.247681
print("Theoretical Standard Error Estimates")
[1] "Theoretical Standard Error Estimates"
apply(results2, 1, sd)
(Intercept) x1
0.28824676 0.13099029 0.09396826
Simulation Study 2
sim3 <- function(i){
x1 <- rnorm(1000, 8)
# x2 <- rnorm(1000, -4)
y <- 3 + 2 * log(x1) + rnorm(1000, sd = sqrt(2))
df <- tibble(x1, y)
res <- df %$% lm(y ~ log(x1))
return(c(coef(res),sigma(res)))
}
sim4 <- function(i){
x1 <- rnorm(1000, 8)
# x2 <- rnorm(1000, -4)
y <- 3 + 2 * log(x1) + rnorm(1000, sd = sqrt(2))
df <- tibble(x1, y)
res <- df %$% lm(y ~ x1)
return(c(coef(res),sigma(res)))
}
print("Correct Model")
[1] "Correct Model"
print("Average estimate")
[1] "Average estimate"
results1 <- sapply(1:1000, sim3)
rowMeans(results1)
(Intercept) log(x1)
3.020812 1.989513 1.412930
print("Theoretical Standard Error Estimates")
[1] "Theoretical Standard Error Estimates"
apply(results1, 1, sd)
(Intercept) log(x1)
0.73301536 0.35310754 0.03167802
print("Incorrect Model")
[1] "Incorrect Model"
print("Average estimate")
[1] "Average estimate"
results2 <- sapply(1:1000, sim4)
rowMeans(results2)
(Intercept) x1
5.1104472 0.2539425 1.4129081
print("Theoretical Standard Error Estimates")
[1] "Theoretical Standard Error Estimates"
apply(results2, 1, sd)
(Intercept) x1
0.36313921 0.04505431 0.03127642
LS0tCnRpdGxlOiAiQ2xhc3MgMTMiCmF1dGhvcjogIklzYWFjIFF1aW50YW5pbGxhIFNhbGluYXMiCmRhdGU6ICJgciBmb3JtYXQoU3lzLnRpbWUoKSwnJW0tJWQtJVknKWAiCm91dHB1dDogCiAgaHRtbF9ub3RlYm9vazoKICAgIHRvYzogdHJ1ZQogICAgdG9jX2Zsb2F0OiB0cnVlCiAgICBjb2RlX2ZvbGRpbmc6IGhpZGUKLS0tCgojIyBTaW11bGF0aW9uIFN0dWR5IDEKCmBgYHtyfQpsaWJyYXJ5KG1hZ3JpdHRyKQpsaWJyYXJ5KHRpZHl2ZXJzZSkKc2ltMSA8LSBmdW5jdGlvbihpKXsKICB4MSA8LSBybm9ybSgxMDAwLCAyKQogIHgyIDwtIHJub3JtKDEwMDAsIC00KQogIHkgPC0gM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