Created
September 16, 2021 07:35
-
-
Save b-rodrigues/538ae9e45fba1eee596f3de38f83ea1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(broom) | |
library(purrr) | |
library(dplyr) | |
library(ggplot2) | |
run_spurious_regression <- function(nvars = 6, nobs = 1000){ | |
dataset <- as.data.frame(replicate(nvars, rnorm(nobs))) | |
tidy(lm(V1 ~ ., data = dataset)) | |
} | |
many_spurious_regressions <- rerun(1, run_spurious_regression()) %>% | |
map2(.x = ., .y = seq(1, 1), ~mutate(.x, run = paste0("Run number ", .y))) %>% | |
bind_rows() | |
many_spurious_regressions %>% | |
mutate(is_significant = ifelse(p.value < .05, "Yes", "No")) %>% | |
ggplot() + | |
geom_point(aes(y = estimate, x = term, colour = is_significant)) + | |
labs(title = "Significative results in totally randomly generated data", | |
subtitle = "6 normally distributed variables of size 1000 were created, and the first was regressed on the 5 others. | |
The plots show the coefficients from one particularly favorable regression. The colour of the dots show if the coefficients were significant at the 5% level.") + | |
theme_minimal() + | |
theme(legend.position = "bottom") | |
many_spurious_regressions <- rerun(100, run_spurious_regression()) %>% | |
map2(.x = ., .y = seq(1, 100), ~mutate(.x, run = paste0("Run number ", .y))) %>% | |
bind_rows() | |
many_spurious_regressions %>% | |
mutate(is_significant = ifelse(p.value < .05, "Yes", "No")) %>% | |
ggplot() + | |
geom_point(aes(y = estimate, x = term, colour = is_significant)) + | |
labs(title = "Significative results in totally randomly generated data", | |
subtitle = "6 normally distributed variables of size 1000 were created, and the first was regressed on the 5 others. | |
The plots show the coefficients from 100 runs and their colours show if these coefficients were significant at the 5% level.") + | |
theme_minimal() + | |
theme(legend.position = "bottom") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment